Skip to content

Commit

Permalink
Let server handle the dirty state
Browse files Browse the repository at this point in the history
Resolves #6
  • Loading branch information
planger committed Jan 5, 2020
1 parent d50f231 commit 3d8d5cc
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static class Kind {
public static final String EXECUTE_SERVER_COMMAND = "executeServerCommand";
public static final String REQUEST_CONTEXT_ACTIONS = "requestContextActions";
public static final String SET_CONTEXT_ACTIONS = "setContextActions";
public static final String SET_DIRTY_STATE = "setDirtyState";
public static final String REQUEST_MARKERS = "requestMarkers";
public static final String SET_MARKERS = "setMarkers";
public static final String LAYOUT = "layout";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/********************************************************************************
* Copyright (c) 2020 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
package org.eclipse.glsp.api.action.kind;

import org.eclipse.glsp.api.action.Action;

public class SetDirtyStateAction extends Action {

private boolean isDirty;

public SetDirtyStateAction() {
super(Action.Kind.SET_DIRTY_STATE);
}

public SetDirtyStateAction(final boolean isDirty) {
this();
this.isDirty = isDirty;
}

public boolean isDirty() { return isDirty; }

public void setDirty(final boolean isDirty) { this.isDirty = isDirty; }

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + (isDirty ? 1231 : 1237);
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
SetDirtyStateAction other = (SetDirtyStateAction) obj;
if (isDirty != other.isDirty) {
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public interface ModelState<T> {

void redo();

boolean isDirty();

void saveIsDone();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
********************************************************************************/
package org.eclipse.glsp.server.actionhandler;

import java.util.Collections;
import java.util.List;

import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.action.kind.AbstractOperationAction;
import org.eclipse.glsp.api.action.kind.RequestBoundsAction;
import org.eclipse.glsp.api.action.kind.SetDirtyStateAction;
import org.eclipse.glsp.api.handler.OperationHandler;
import org.eclipse.glsp.api.model.GraphicalModelState;
import org.eclipse.glsp.api.provider.OperationHandlerProvider;
Expand All @@ -43,7 +43,7 @@ public List<Action> execute(final Action action, final GraphicalModelState model
&& operationHandlerProvider.isHandled((AbstractOperationAction) action)) {
return doHandle((AbstractOperationAction) action, modelState);
}
return Collections.emptyList();
return none();
}

public List<Action> doHandle(final AbstractOperationAction action, final GraphicalModelState modelState) {
Expand All @@ -53,7 +53,7 @@ public List<Action> doHandle(final AbstractOperationAction action, final Graphic
GModelRecordingCommand command = new GModelRecordingCommand(modelState.getRoot(), label,
() -> handler.execute(action, modelState));
modelState.execute(command);
return listOf(new RequestBoundsAction(modelState.getRoot()));
return listOf(new RequestBoundsAction(modelState.getRoot()), new SetDirtyStateAction(modelState.isDirty()));
}
return none();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.log4j.Logger;
import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.action.kind.SaveModelAction;
import org.eclipse.glsp.api.action.kind.SetDirtyStateAction;
import org.eclipse.glsp.api.factory.GraphGsonConfiguratorFactory;
import org.eclipse.glsp.api.model.GraphicalModelState;
import org.eclipse.glsp.api.utils.ClientOptions;
Expand All @@ -51,14 +52,15 @@ public List<Action> execute(final Action action, final GraphicalModelState model
if (action instanceof SaveModelAction) {
saveModelState(modelState);
}
return none();
return listOf(new SetDirtyStateAction(modelState.isDirty()));
}

private void saveModelState(final GraphicalModelState modelState) {
convertToFile(modelState).ifPresent(file -> {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
Gson gson = gsonConfigurationFactory.configureGson().setPrettyPrinting().create();
gson.toJson(modelState.getRoot(), GGraph.class, writer);
modelState.saveIsDone();
} catch (IOException e) {
LOG.error(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.action.kind.RedoAction;
import org.eclipse.glsp.api.action.kind.RequestBoundsAction;
import org.eclipse.glsp.api.action.kind.SetDirtyStateAction;
import org.eclipse.glsp.api.action.kind.UndoAction;
import org.eclipse.glsp.api.model.GraphicalModelState;

Expand All @@ -36,12 +37,11 @@ public boolean handles(final Action action) {
public List<Action> execute(final Action action, final GraphicalModelState modelState) {
if (action instanceof UndoAction && modelState.canUndo()) {
modelState.undo();
return listOf(new RequestBoundsAction(modelState.getRoot()));
return listOf(new RequestBoundsAction(modelState.getRoot()), new SetDirtyStateAction(modelState.isDirty()));
} else if (action instanceof RedoAction && modelState.canRedo()) {
modelState.redo();
return listOf(new RequestBoundsAction(modelState.getRoot()));
return listOf(new RequestBoundsAction(modelState.getRoot()), new SetDirtyStateAction(modelState.isDirty()));
}

LOG.warn("Cannot undo or redo");
return none();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.glsp.api.model.GraphicalModelState;
Expand All @@ -31,7 +32,7 @@ public class ModelStateImpl implements GraphicalModelState {
private Map<String, String> options;
private String clientId;
private GModelRoot currentModel;
private CommandStack commandStack;
private BasicCommandStack commandStack;
private Set<String> expandedElements;
private Set<String> selectedElements;

Expand Down Expand Up @@ -67,7 +68,7 @@ protected void initializeCommandStack() {

public CommandStack getCommandStack() { return commandStack; }

protected void setCommandStack(final CommandStack commandStack) {
protected void setCommandStack(final BasicCommandStack commandStack) {
if (this.commandStack != null) {
this.commandStack.flush();
}
Expand Down Expand Up @@ -136,4 +137,12 @@ public void redo() {
commandStack.redo();
}

@Override
public boolean isDirty() { return commandStack.isSaveNeeded(); }

@Override
public void saveIsDone() {
commandStack.saveIsDone();
}

}

0 comments on commit 3d8d5cc

Please sign in to comment.