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

#1919: Save application state to the user profile correctly #1994

Merged
merged 1 commit into from
Jul 29, 2016
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 @@ -28,11 +28,9 @@
import org.eclipse.che.ide.api.editor.EditorPartPresenter;
import org.eclipse.che.ide.api.event.SelectionChangedEvent;
import org.eclipse.che.ide.api.event.SelectionChangedHandler;
import org.eclipse.che.ide.api.event.project.ProjectUpdatedEvent;
import org.eclipse.che.ide.api.event.project.ProjectUpdatedEvent.ProjectUpdatedHandler;
import org.eclipse.che.ide.api.event.WindowActionEvent;
import org.eclipse.che.ide.api.event.WindowActionHandler;
import org.eclipse.che.ide.api.machine.DevMachine;
import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler;
import org.eclipse.che.ide.api.resources.Container;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.Resource;
Expand All @@ -43,10 +41,12 @@
import org.eclipse.che.ide.api.resources.VirtualFile;
import org.eclipse.che.ide.api.selection.Selection;
import org.eclipse.che.ide.api.workspace.WorkspaceReadyEvent;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStoppedEvent;
import org.eclipse.che.ide.project.node.SyntheticNode;
import org.eclipse.che.ide.resource.Path;
import org.eclipse.che.ide.resources.impl.ResourceDeltaImpl;
import org.eclipse.che.ide.resources.impl.ResourceManager;
import org.eclipse.che.ide.statepersistance.AppStateManager;
import org.eclipse.che.ide.util.Arrays;

import java.util.ArrayList;
Expand Down Expand Up @@ -75,9 +75,9 @@
@Singleton
public class AppContextImpl implements AppContext,
SelectionChangedHandler,
WsAgentStateHandler,
ProjectUpdatedHandler,
ResourceChangedHandler {
ResourceChangedHandler,
WindowActionHandler,
WorkspaceStoppedEvent.Handler {

private final BrowserQueryFieldRenderer browserQueryFieldRenderer;
private final List<String> projectsInImport;
Expand All @@ -100,18 +100,20 @@ public class AppContextImpl implements AppContext,
public AppContextImpl(EventBus eventBus,
BrowserQueryFieldRenderer browserQueryFieldRenderer,
ResourceManager.ResourceManagerFactory resourceManagerFactory,
Provider<EditorAgent> editorAgentProvider) {
Provider<EditorAgent> editorAgentProvider,
Provider<AppStateManager> appStateManager) {
this.eventBus = eventBus;
this.browserQueryFieldRenderer = browserQueryFieldRenderer;
this.resourceManagerFactory = resourceManagerFactory;
this.editorAgentProvider = editorAgentProvider;
this.appStateManager = appStateManager;

projectsInImport = new ArrayList<>();

eventBus.addHandler(SelectionChangedEvent.TYPE, this);
eventBus.addHandler(WsAgentStateEvent.TYPE, this);
eventBus.addHandler(ProjectUpdatedEvent.getType(), this);
eventBus.addHandler(ResourceChangedEvent.getType(), this);
eventBus.addHandler(WindowActionEvent.TYPE, this);
eventBus.addHandler(WorkspaceStoppedEvent.TYPE, this);
}

@Override
Expand Down Expand Up @@ -182,6 +184,21 @@ public DevMachine getDevMachine() {
}

public void setDevMachine(DevMachine devMachine) {
checkState(devMachine != null);

if (this.devMachine != null && this.devMachine.getId().equals(devMachine.getId())) {
return;
}

browserQueryFieldRenderer.setProjectName("");

if (projects != null) {
for (Project project : projects) {
eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(project, REMOVED)));
}
projects = null;
}

this.devMachine = devMachine;

resourceManager = resourceManagerFactory.newResourceManager(devMachine);
Expand All @@ -191,6 +208,7 @@ public void apply(Project[] projects) throws OperationException {
AppContextImpl.this.projects = projects;
java.util.Arrays.sort(AppContextImpl.this.projects, ResourcePathComparator.getInstance());
eventBus.fireEvent(new WorkspaceReadyEvent(projects));
appStateManager.get().restoreWorkspaceState(getWorkspaceId());
}
});
}
Expand Down Expand Up @@ -327,41 +345,10 @@ public void onSelectionChanged(SelectionChangedEvent event) {
currentResources = resources.toArray(new Resource[resources.size()]);
}

@Override
public void onWsAgentStarted(WsAgentStateEvent event) {
}

@Override
public void onWsAgentStopped(WsAgentStateEvent event) {
// currentProject = null;
browserQueryFieldRenderer.setProjectName("");
for (Project project : projects) {
eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(project, REMOVED)));
}

projects = null;
resourceManager = null;
}

@Override
public void onProjectUpdated(ProjectUpdatedEvent event) {
// final ProjectConfigDto updatedProjectDescriptor = event.getUpdatedProjectDescriptor();
// final String updatedProjectDescriptorPath = updatedProjectDescriptor.getPath();

// if (updatedProjectDescriptorPath.equals(currentProject.getProjectConfig().getPath())) {
// currentProject.setProjectConfig(updatedProjectDescriptor);
// eventBus.fireEvent(new CurrentProjectChangedEvent(updatedProjectDescriptor));
// }
//
// if (updatedProjectDescriptorPath.equals(currentProject.getRootProject().getPath())) {
// currentProject.setRootProject(updatedProjectDescriptor);
// browserQueryFieldRenderer.setProjectName(updatedProjectDescriptor.getName());
// }
}

private final EventBus eventBus;
private final ResourceManager.ResourceManagerFactory resourceManagerFactory;
private final Provider<EditorAgent> editorAgentProvider;
private final Provider<AppStateManager> appStateManager;

private ResourceManager resourceManager;
private Project[] projects;
Expand Down Expand Up @@ -438,4 +425,31 @@ public Project getRootProject() {

return root;
}

@Override
public void onWindowClosing(WindowActionEvent event) {
appStateManager.get().persistWorkspaceState(getWorkspaceId());
}

@Override
public void onWorkspaceStopped(WorkspaceStoppedEvent event) {
appStateManager.get().persistWorkspaceState(getWorkspaceId()).then(new Operation<Void>() {
@Override
public void apply(Void arg) throws OperationException {
browserQueryFieldRenderer.setProjectName("");
for (Project project : projects) {
eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(project, REMOVED)));
}

projects = null;
resourceManager = null;
}
});

devMachine = null;
}

@Override
public void onWindowClosed(WindowActionEvent event) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,14 @@
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.promises.client.PromiseError;
import org.eclipse.che.ide.api.workspace.WorkspaceReadyEvent;
import org.eclipse.che.ide.api.workspace.WorkspaceReadyEvent.WorkspaceReadyHandler;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStoppedEvent;
import org.eclipse.che.ide.api.action.Action;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.action.ActionManager;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.event.WindowActionEvent;
import org.eclipse.che.ide.api.event.WindowActionHandler;
import org.eclipse.che.ide.api.parts.PerspectiveManager;
import org.eclipse.che.ide.api.preferences.PreferencesManager;
import org.eclipse.che.ide.dto.DtoFactory;
Expand All @@ -50,17 +42,13 @@
* @author Artem Zatsarynnyi
*/
@Singleton
public class AppStateManager implements WindowActionHandler,
WorkspaceStoppedEvent.Handler,
WsAgentStateHandler,
WorkspaceReadyHandler {
public class AppStateManager {

/** The name of the property for the mappings in user preferences. */
public static final String PREFERENCE_PROPERTY_NAME = "IdeAppState";

private final Set<PersistenceComponent> persistenceComponents;
private final PreferencesManager preferencesManager;
private final AppContext appContext;
private final DtoFactory dtoFactory;
private final ActionManager actionManager;
private final PresentationFactory presentationFactory;
Expand All @@ -71,25 +59,17 @@ public class AppStateManager implements WindowActionHandler,
@Inject
public AppStateManager(Set<PersistenceComponent> persistenceComponents,
PreferencesManager preferencesManager,
AppContext appContext,
DtoFactory dtoFactory,
ActionManager actionManager,
PresentationFactory presentationFactory,
Provider<PerspectiveManager> perspectiveManagerProvider,
EventBus eventBus) {
Provider<PerspectiveManager> perspectiveManagerProvider) {
this.persistenceComponents = persistenceComponents;
this.preferencesManager = preferencesManager;
this.appContext = appContext;
this.dtoFactory = dtoFactory;
this.actionManager = actionManager;
this.presentationFactory = presentationFactory;
this.perspectiveManagerProvider = perspectiveManagerProvider;

eventBus.addHandler(WorkspaceStoppedEvent.TYPE, this);
eventBus.addHandler(WindowActionEvent.TYPE, this);
eventBus.addHandler(WsAgentStateEvent.TYPE, this);
eventBus.addHandler(WorkspaceReadyEvent.getType(), this);

readStateFromPreferences();
}

Expand All @@ -107,61 +87,35 @@ private void readStateFromPreferences() {
}
}

@Override
public void onWindowClosing(WindowActionEvent event) {
persistWorkspaceState();
}

@Override
public void onWindowClosed(WindowActionEvent event) {
}

@Override
public void onWsAgentStarted(WsAgentStateEvent event) {
}

@Override
public void onWorkspaceReady(WorkspaceReadyEvent event) {
restoreWorkspaceState();
}

@Override
public void onWsAgentStopped(WsAgentStateEvent event) {
persistWorkspaceState();
}

@Override
public void onWorkspaceStopped(WorkspaceStoppedEvent event) {
persistWorkspaceState();
}

private void persistWorkspaceState() {
appState.setRecentWorkspaceId(appContext.getDevMachine().getId());
public Promise<Void> persistWorkspaceState(String wsId) {
appState.setRecentWorkspaceId(wsId);

final WorkspaceState workspaceState = dtoFactory.createDto(WorkspaceState.class);
appState.getWorkspaces().put(appContext.getDevMachine().getId(), workspaceState);
appState.getWorkspaces().put(wsId, workspaceState);

final List<ActionDescriptor> actions = workspaceState.getActions();
for (PersistenceComponent persistenceComponent : persistenceComponents) {
actions.addAll(persistenceComponent.getActions());
}

writeStateToPreferences();
return writeStateToPreferences();
}

private void writeStateToPreferences() {
private Promise<Void> writeStateToPreferences() {
final String json = dtoFactory.toJson(appState);
Log.info(getClass(), "write: " + json);
preferencesManager.setValue(PREFERENCE_PROPERTY_NAME, json);
preferencesManager.flushPreferences().catchError(new Operation<PromiseError>() {
return preferencesManager.flushPreferences().catchError(new Operation<PromiseError>() {
@Override
public void apply(PromiseError arg) throws OperationException {
Log.error(AppStateManager.class, "Failed to store app's state to user's preferences");
}
});
}

private void restoreWorkspaceState() {
final WorkspaceState workspaceState = appState.getWorkspaces().get(appContext.getDevMachine().getId());
public void restoreWorkspaceState(String wsId) {
final WorkspaceState workspaceState = appState.getWorkspaces().get(wsId);

if (workspaceState == null) {
return;
}
Expand Down