Skip to content

Commit

Permalink
Let server handle the dirty state (#32)
Browse files Browse the repository at this point in the history
* Enable handlers returning multiple actions to a client action

This is in preparation for #6.

* Let server handle the dirty state

Resolves #6
  • Loading branch information
planger authored and tortmayr committed Jan 10, 2020
1 parent ae7c114 commit 46d078f
Show file tree
Hide file tree
Showing 26 changed files with 202 additions and 102 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
Expand Up @@ -15,9 +15,10 @@
********************************************************************************/
package org.eclipse.glsp.api.action;

import java.util.Optional;
import static org.eclipse.glsp.api.action.kind.ResponseAction.respond;

import org.eclipse.glsp.api.action.kind.ResponseAction;
import java.util.Collections;
import java.util.List;

public interface ActionProcessor {

Expand All @@ -31,11 +32,8 @@ public interface ActionProcessor {
* @param action The action to process
*/
default void process(final String clientId, final Action action) {
Optional<Action> responseOpt = dispatch(clientId, action);
if (responseOpt.isPresent()) {
// ensure request and response have same id if necessary
Action response = ResponseAction.respond(action, responseOpt.get());
send(clientId, response);
for (Action responseAction : dispatch(clientId, action)) {
send(clientId, respond(action, responseAction));
}
}

Expand All @@ -59,7 +57,7 @@ default void process(final ActionMessage message) {
* @return An optional Action to be sent to the client as the result of handling
* the received <code>action</code>
*/
Optional<Action> dispatch(String clientId, Action action);
List<Action> dispatch(String clientId, Action action);

/**
* Send the given action to the specified clientId.
Expand All @@ -72,8 +70,8 @@ default void process(final ActionMessage message) {
class NullImpl implements ActionProcessor {

@Override
public Optional<Action> dispatch(final String clientId, final Action action) {
return Optional.empty();
public List<Action> dispatch(final String clientId, final Action action) {
return Collections.emptyList();
}

@Override
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 @@ -15,12 +15,30 @@
********************************************************************************/
package org.eclipse.glsp.api.handler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

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

public interface ActionHandler extends Handler<Action> {

Optional<Action> execute(String clientId, Action action);
List<Action> execute(String clientId, Action action);

default List<Action> listOf(final Action... action) {
return Arrays.asList(action);
}

default List<Action> listOf(final Optional<Action> optionalAction) {
List<Action> actions = new ArrayList<>();
optionalAction.ifPresent(action -> actions.add(action));
return actions;
}

default List<Action> none() {
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package org.eclipse.glsp.api.handler;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.model.GraphicalModelState;
Expand All @@ -28,5 +28,5 @@ default void execute(final String commandId, final GraphicalModelState modelStat
execute(commandId, Collections.emptyMap(), modelState);
}

Optional<Action> execute(String commandId, Map<String, String> options, GraphicalModelState modelState);
List<Action> execute(String commandId, Map<String, String> options, GraphicalModelState modelState);
}
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,6 +15,7 @@
********************************************************************************/
package org.eclipse.glsp.server.actionhandler;

import java.util.List;
import java.util.Optional;

import org.eclipse.glsp.api.action.Action;
Expand All @@ -37,16 +38,16 @@ public abstract class AbstractActionHandler implements ActionHandler {
* the client. If no response to the client is need a NoOpAction is returned
*/
@Override
public Optional<Action> execute(final String clientId, final Action action) {
public List<Action> execute(final String clientId, final Action action) {
this.clientId = clientId;
Optional<GraphicalModelState> modelState = modelStateProvider.getModelState(clientId);
if (modelState.isPresent()) {
return execute(action, modelState.get());
}
ServerStatus status = new ServerStatus(Severity.FATAL,
"Could not retrieve the model state for client with id '" + clientId + "'");
return Optional.of(new ServerStatusAction(status));
return listOf(new ServerStatusAction(status));
}

protected abstract Optional<Action> execute(Action action, GraphicalModelState modelState);
protected abstract List<Action> execute(Action action, GraphicalModelState modelState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/
package org.eclipse.glsp.server.actionhandler;

import java.util.Optional;
import java.util.List;
import java.util.Set;

import org.eclipse.glsp.api.action.Action;
Expand All @@ -36,18 +36,18 @@ public boolean handles(final Action action) {
}

@Override
public Optional<Action> execute(final Action action, final GraphicalModelState modelState) {
public List<Action> execute(final Action action, final GraphicalModelState modelState) {
switch (action.getKind()) {
case Action.Kind.COLLAPSE_EXPAND:
return handleCollapseExpandAction((CollapseExpandAction) action, modelState);
case Action.Kind.COLLAPSE_EXPAND_ALL:
return handleCollapseExpandAllAction((CollapseExpandAllAction) action, modelState);
default:
return Optional.empty();
return none();
}
}

private Optional<Action> handleCollapseExpandAllAction(final CollapseExpandAllAction action,
private List<Action> handleCollapseExpandAllAction(final CollapseExpandAllAction action,
final GraphicalModelState modelState) {
Set<String> expandedElements = modelState.getExpandedElements();
expandedElements.clear();
Expand All @@ -57,10 +57,10 @@ private Optional<Action> handleCollapseExpandAllAction(final CollapseExpandAllAc
if (expansionListener != null) {
expansionListener.expansionChanged(action);
}
return Optional.empty();
return none();
}

private Optional<Action> handleCollapseExpandAction(final CollapseExpandAction action,
private List<Action> handleCollapseExpandAction(final CollapseExpandAction action,
final GraphicalModelState modelState) {
Set<String> expandedElements = modelState.getExpandedElements();
if (action.getCollapseIds() != null) {
Expand All @@ -74,8 +74,7 @@ private Optional<Action> handleCollapseExpandAction(final CollapseExpandAction a
expansionListener.expansionChanged(action);
}

return Optional.empty();

return none();
}

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

import java.util.Optional;
import java.util.List;

import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.action.kind.ComputedBoundsAction;
Expand All @@ -35,7 +35,7 @@ public boolean handles(final Action action) {
}

@Override
public Optional<Action> execute(final Action action, final GraphicalModelState modelState) {
public List<Action> execute(final Action action, final GraphicalModelState modelState) {
if (action instanceof ComputedBoundsAction) {
ComputedBoundsAction computedBoundsAction = (ComputedBoundsAction) action;

Expand All @@ -47,7 +47,7 @@ public Optional<Action> execute(final Action action, final GraphicalModelState m
}
}
}
return Optional.empty();
return none();
}

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

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

import org.eclipse.glsp.api.action.Action;
Expand All @@ -36,12 +38,12 @@ public class DIActionProcessor implements ActionProcessor {
protected ActionHandlerProvider handlerProvider;

@Override
public Optional<Action> dispatch(final String clientId, final Action action) {
public List<Action> dispatch(final String clientId, final Action action) {
Optional<ActionHandler> handler = handlerProvider.getHandler(action);
if (handler.isPresent()) {
return handler.get().execute(clientId, action);
}
return Optional.empty();
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/
package org.eclipse.glsp.server.actionhandler;

import java.util.List;
import java.util.Optional;

import org.eclipse.glsp.api.action.Action;
Expand All @@ -35,15 +36,15 @@ public boolean handles(final Action action) {
}

@Override
public Optional<Action> execute(final Action action, final GraphicalModelState modelState) {
public List<Action> execute(final Action action, final GraphicalModelState modelState) {
if (action instanceof ExecuteServerCommandAction) {
ExecuteServerCommandAction commandAction = (ExecuteServerCommandAction) action;
Optional<ServerCommandHandler> handler = commandHandlerProvider.getHandler(commandAction.getCommandId());
if (handler.isPresent()) {
return handler.get().execute(commandAction.getCommandId(), commandAction.getOptions(), modelState);
}
}
return Optional.empty();
return none();
}

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

import java.util.Optional;
import java.util.List;

import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.action.kind.LayoutAction;
Expand All @@ -41,13 +41,13 @@ public boolean handles(final Action action) {
}

@Override
protected Optional<Action> execute(final Action action, final GraphicalModelState modelState) {
protected List<Action> execute(final Action action, final GraphicalModelState modelState) {
if (serverConfiguration.getLayoutKind() == ServerLayoutKind.MANUAL) {
if (layoutEngine != null) {
layoutEngine.layout(modelState);
}
return Optional.of(new RequestBoundsAction(modelState.getRoot()));
return listOf(new RequestBoundsAction(modelState.getRoot()));
}
return Optional.empty();
return none();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
********************************************************************************/
package org.eclipse.glsp.server.actionhandler;

import java.util.Optional;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.action.kind.SetModelAction;
Expand All @@ -38,21 +40,20 @@ public class ModelSubmissionHandler {
private final Object modelLock = new Object();
private final int revision = 0;

public Optional<Action> doSubmitModel(final boolean update, final GraphicalModelState modelState) {
public List<Action> doSubmitModel(final boolean update, final GraphicalModelState modelState) {
GModelRoot newRoot = modelState.getRoot();
if (serverConfiguration.getLayoutKind() == ServerLayoutKind.AUTOMATIC) {
layoutEngine.layout(modelState);
}
synchronized (modelLock) {
if (newRoot.getRevision() == revision) {
if (update) {
return Optional.of(new UpdateModelAction(newRoot, true));
} else {
return Optional.of(new SetModelAction(newRoot));
return Arrays.asList(new UpdateModelAction(newRoot, true));
}
return Arrays.asList(new SetModelAction(newRoot));
}
}
return Optional.empty();
return Collections.emptyList();
}

public synchronized Object getModelLock() { return modelLock; }
Expand Down
Loading

0 comments on commit 46d078f

Please sign in to comment.