Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix displaying Pull Request Panel #8905

Merged
merged 1 commit into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
import static org.eclipse.che.ide.api.parts.PartStackType.INFORMATION;
import static org.eclipse.che.ide.api.parts.PartStackType.NAVIGATION;
import static org.eclipse.che.ide.api.parts.PartStackType.TOOLING;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.ACTIVE_PART;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.HIDDEN_STATE;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.PART_CLASS_NAME;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.PART_STACKS;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.PART_STACK_PARTS;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.PART_STACK_SIZE;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.PART_STACK_STATE;

import com.google.inject.Provider;
import com.google.web.bindery.event.shared.EventBus;
Expand Down Expand Up @@ -332,9 +339,9 @@ public JsonObject getState() {
JsonObject state = Json.createObject();
JsonObject partStacks = Json.createObject();
if (activePart != null) {
state.put("ACTIVE_PART", activePart.getClass().getName());
state.put(ACTIVE_PART, activePart.getClass().getName());
}
state.put("PART_STACKS", partStacks);
state.put(PART_STACKS, partStacks);

partStacks.put(
PartStackType.INFORMATION.name(),
Expand All @@ -352,24 +359,23 @@ public JsonObject getState() {
private JsonObject getPartStackState(
PartStack partStack, WorkBenchPartController partController) {
JsonObject state = Json.createObject();
state.put("SIZE", partController.getSize());
state.put("STATE", partStack.getPartStackState().name());
state.put(PART_STACK_SIZE, partController.getSize());
state.put(PART_STACK_STATE, partStack.getPartStackState().name());

if (partStack.getParts().isEmpty()) {
state.put("HIDDEN", true);
state.put(HIDDEN_STATE, true);
} else {
if (partStack.getActivePart() != null) {
state.put("ACTIVE_PART", partStack.getActivePart().getClass().getName());
state.put(ACTIVE_PART, partStack.getActivePart().getClass().getName());
}

state.put("HIDDEN", partController.isHidden());
state.put(HIDDEN_STATE, partController.isHidden());

JsonArray parts = Json.createArray();
state.put("PARTS", parts);
state.put(PART_STACK_PARTS, parts);
int i = 0;
for (PartPresenter entry : partStack.getParts()) {
JsonObject presenterState = Json.createObject();
presenterState.put("CLASS", entry.getClass().getName());
presenterState.put(PART_CLASS_NAME, entry.getClass().getName());
parts.set(i++, presenterState);
}
}
Expand All @@ -378,8 +384,8 @@ private JsonObject getPartStackState(

@Override
public Promise<Void> loadState(@NotNull JsonObject state) {
if (state.hasKey("PART_STACKS")) {
JsonObject partStacksState = state.getObject("PART_STACKS");
if (state.hasKey(PART_STACKS)) {
JsonObject partStacksState = state.getObject(PART_STACKS);

// Don't restore part dimensions if perspective is maximized.
boolean perspectiveMaximized = isPerspectiveMaximized(partStacksState);
Expand Down Expand Up @@ -411,8 +417,8 @@ public Promise<Void> loadState(@NotNull JsonObject state) {
}

// restore perspective's active part
if (state.hasKey("ACTIVE_PART")) {
String activePart = state.getString("ACTIVE_PART");
if (state.hasKey(ACTIVE_PART)) {
String activePart = state.getString(ACTIVE_PART);
Provider<PartPresenter> provider = dynaProvider.getProvider(activePart);
if (provider != null) {
setActivePart(provider.get());
Expand All @@ -431,8 +437,8 @@ public Promise<Void> loadState(@NotNull JsonObject state) {
private boolean isPerspectiveMaximized(JsonObject partStacksState) {
for (String partStackType : partStacksState.keys()) {
JsonObject partStackState = partStacksState.getObject(partStackType);
if (partStackState.hasKey("STATE")
&& PartStack.State.MAXIMIZED.name().equals(partStackState.getString("STATE"))) {
if (partStackState.hasKey(PART_STACK_STATE)
&& PartStack.State.MAXIMIZED.name().equals(partStackState.getString(PART_STACK_STATE))) {
return true;
}
}
Expand All @@ -453,13 +459,14 @@ private void loadPartStackState(
WorkBenchPartController controller,
JsonObject partStackState,
boolean skipRestoreDimensions) {
if (partStackState.hasKey("PARTS")) {
JsonArray parts = partStackState.get("PARTS");

if (partStackState.hasKey(PART_STACK_PARTS)) {
JsonArray parts = partStackState.get(PART_STACK_PARTS);

for (int i = 0; i < parts.length(); i++) {
JsonObject value = parts.get(i);
if (value.hasKey("CLASS")) {
String className = value.getString("CLASS");
if (value.hasKey(PART_CLASS_NAME)) {
String className = value.getString(PART_CLASS_NAME);
Provider<PartPresenter> provider = dynaProvider.getProvider(className);
if (provider != null) {
PartPresenter partPresenter = provider.get();
Expand All @@ -472,8 +479,8 @@ private void loadPartStackState(
}

// restore part stack's active part
if (partStackState.hasKey("ACTIVE_PART")) {
String activePart = partStackState.getString("ACTIVE_PART");
if (partStackState.hasKey(ACTIVE_PART)) {
String activePart = partStackState.getString(ACTIVE_PART);
Provider<PartPresenter> provider = dynaProvider.getProvider(activePart);
if (provider != null) {
partStack.setActivePart(provider.get());
Expand All @@ -490,13 +497,15 @@ private void loadPartStackState(
return;
}

if (partStackState.hasKey("HIDDEN") && partStackState.getBoolean("HIDDEN")) {
if (partStackState.hasKey(HIDDEN_STATE) && partStackState.getBoolean(HIDDEN_STATE)) {

partStack.hide();
controller.setHidden(true);
return;
}

if (partStackState.hasKey("SIZE")) {
double size = partStackState.getNumber("SIZE");
if (partStackState.hasKey(PART_STACK_SIZE)) {
double size = partStackState.getNumber(PART_STACK_SIZE);

// Size of the part must not be less 100 pixels.
if (size <= MIN_PART_SIZE) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.ide.statepersistance;

/**
* Constants for the mappings in user preferences to store/restore app state.
*
* @author Roman Nikitenko
*/
public final class AppStateConstants {

public static final String APP_STATE = "IdeAppStates";
public static final String PERSPECTIVES = "perspectives";
public static final String PART_STACKS = "PART_STACKS";
public static final String PART_STACK_STATE = "STATE";
public static final String PART_STACK_PARTS = "PARTS";
public static final String PART_STACK_SIZE = "SIZE";
public static final String ACTIVE_PART = "ACTIVE_PART";
public static final String PART_CLASS_NAME = "CLASS";
public static final String HIDDEN_STATE = "HIDDEN";

private AppStateConstants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
*/
package org.eclipse.che.ide.statepersistance;

import static org.eclipse.che.ide.statepersistance.AppStateConstants.APP_STATE;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.PART_STACKS;
import static org.eclipse.che.ide.statepersistance.AppStateConstants.PERSPECTIVES;

import com.google.common.annotations.VisibleForTesting;
import com.google.gwt.user.client.Timer;
import com.google.inject.Inject;
Expand All @@ -24,9 +28,15 @@
import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.promises.client.PromiseProvider;
import org.eclipse.che.api.promises.client.callback.AsyncPromiseHelper;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.ide.DelayedTask;
import org.eclipse.che.ide.api.WindowActionEvent;
import org.eclipse.che.ide.api.WindowActionHandler;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.parts.PartStackStateChangedEvent;
import org.eclipse.che.ide.api.parts.PartStackType;
import org.eclipse.che.ide.api.parts.PerspectiveManager;
import org.eclipse.che.ide.api.preferences.PreferencesManager;
import org.eclipse.che.ide.api.statepersistance.StateComponent;
import org.eclipse.che.ide.api.workspace.WorkspaceReadyEvent;
Expand All @@ -48,30 +58,41 @@ public class AppStateManager {

private static final String WORKSPACE = "workspace";

private final Provider<PerspectiveManager> perspectiveManagerProvider;
private final Provider<StateComponentRegistry> stateComponentRegistry;

private final PreferencesManager preferencesManager;
private final JsonFactory jsonFactory;
private final PromiseProvider promises;
private EventBus eventBus;
private final AppContext appContext;
private JsonObject allWsState;
private final DelayedTask persistWorkspaceStateTask =
new DelayedTask() {
@Override
public void onExecute() {
persistWorkspaceState();
}
};

@Inject
public AppStateManager(
Provider<PerspectiveManager> perspectiveManagerProvider,
Provider<StateComponentRegistry> stateComponentRegistryProvider,
PreferencesManager preferencesManager,
JsonFactory jsonFactory,
PromiseProvider promises,
EventBus eventBus,
AppContext appContext) {
this.perspectiveManagerProvider = perspectiveManagerProvider;
this.stateComponentRegistry = stateComponentRegistryProvider;
this.preferencesManager = preferencesManager;
this.jsonFactory = jsonFactory;
this.promises = promises;
this.eventBus = eventBus;
this.appContext = appContext;

// delay is required because we need to wait some time while different components initialized
eventBus.addHandler(WorkspaceReadyEvent.getType(), e -> restoreWorkspaceStateWithDelay());
eventBus.addHandler(WorkspaceReadyEvent.getType(), this::onWorkspaceReady);

eventBus.addHandler(
WindowActionEvent.TYPE,
Expand All @@ -90,7 +111,7 @@ public void onWindowClosed(WindowActionEvent event) {}
}

public void readStateFromPreferences() {
final String json = preferencesManager.getValue(PREFERENCE_PROPERTY_NAME);
final String json = preferencesManager.getValue(APP_STATE);
if (json == null) {
allWsState = jsonFactory.createObject();
} else {
Expand All @@ -103,13 +124,68 @@ public void readStateFromPreferences() {
}
}

private void restoreWorkspaceStateWithDelay() {
new Timer() {
@Override
public void run() {
restoreWorkspaceState();
}
}.schedule(1000);
/**
* Gets cached state for given {@code partStackType}. Use {@link #readStateFromPreferences()}
* first to get not cached state
*/
@Nullable
public JsonObject getStateFor(PartStackType partStackType) {
String workspaceId = appContext.getWorkspace().getId();
if (!allWsState.hasKey(workspaceId)) {
return null;
}

JsonObject preferences = allWsState.getObject(workspaceId);
if (!preferences.hasKey(WORKSPACE)) {
return null;
}

JsonObject workspace = preferences.getObject(WORKSPACE);
if (!workspace.hasKey(WORKSPACE)) {
return null;
}

JsonObject workspaceState = workspace.getObject(WORKSPACE);
if (!workspaceState.hasKey(PERSPECTIVES)) {
return null;
}

String perspectiveId = perspectiveManagerProvider.get().getPerspectiveId();
JsonObject perspectives = workspaceState.getObject(PERSPECTIVES);
if (!perspectives.hasKey(perspectiveId)) {
return null;
}

JsonObject projectPerspective = perspectives.getObject(perspectiveId);
if (!projectPerspective.hasKey(PART_STACKS)) {
return null;
}

JsonObject partStacks = projectPerspective.getObject(PART_STACKS);
if (!partStacks.hasKey(partStackType.name())) {
return null;
}

return partStacks.getObject(partStackType.name());
}

private Promise<Void> restoreWorkspaceStateWithDelay() {
return AsyncPromiseHelper.createFromAsyncRequest(
callback ->
new Timer() {
@Override
public void run() {
restoreWorkspaceState()
.then(
arg -> {
callback.onSuccess(null);
})
.catchError(
arg -> {
callback.onFailure(arg.getCause());
});
}
}.schedule(1000));
}

@VisibleForTesting
Expand Down Expand Up @@ -171,7 +247,7 @@ public Promise<Void> persistWorkspaceState() {

private Promise<Void> writeStateToPreferences(JsonObject state) {
final String json = state.toJson();
preferencesManager.setValue(PREFERENCE_PROPERTY_NAME, json);
preferencesManager.setValue(APP_STATE, json);
return preferencesManager
.flushPreferences()
.catchError(
Expand All @@ -186,4 +262,14 @@ private Promise<Void> writeStateToPreferences(JsonObject state) {
public boolean hasStateForWorkspace(String wsId) {
return allWsState.hasKey(wsId);
}

private void onWorkspaceReady(WorkspaceReadyEvent workspaceReadyEvent) {
// delay is required because we need to wait some time while different components initialized
restoreWorkspaceStateWithDelay()
.then(
ignored -> {
eventBus.addHandler(
PartStackStateChangedEvent.TYPE, event -> persistWorkspaceStateTask.delay(500));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.che.ide.workspace;

import static org.eclipse.che.ide.statepersistance.AppStateConstants.PERSPECTIVES;

import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.inject.Provider;
Expand Down Expand Up @@ -168,7 +170,7 @@ public PartPresenter getActivePart() {
public JsonObject getState() {
JsonObject state = Json.createObject();
JsonObject perspectivesJs = Json.createObject();
state.put("perspectives", perspectivesJs);
state.put(PERSPECTIVES, perspectivesJs);
Map<String, Perspective> perspectives = perspectiveManagerProvider.get().getPerspectives();
for (Map.Entry<String, Perspective> entry : perspectives.entrySet()) {
// store only default perspective
Expand All @@ -181,8 +183,8 @@ public JsonObject getState() {

@Override
public Promise<Void> loadState(JsonObject state) {
if (state.hasKey("perspectives")) {
JsonObject perspectives = state.getObject("perspectives");
if (state.hasKey(PERSPECTIVES)) {
JsonObject perspectives = state.getObject(PERSPECTIVES);
Map<String, Perspective> perspectiveMap = perspectiveManagerProvider.get().getPerspectives();
ArrayOf<Promise<?>> perspectivePromises = Collections.arrayOf();
for (String key : perspectives.keys()) {
Expand Down
Loading