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

Fixes #21: Align action processor with client-side action dispatcher #66

Merged
merged 3 commits into from
Aug 3, 2020
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 @@ -36,21 +36,19 @@ public String toString() {

public static class Kind {
public static final String CENTER = "center";
public static final String COLLAPSE_EXPAND = "collapseExpand";
public static final String COLLAPSE_EXPAND_ALL = "collapseExpandAll";
public static final String CLEAR_MARKERS = "clearMarkers";
public static final String COMPUTED_BOUNDS = "computedBounds";
public static final String CUT_ACTION = "cut";
public static final String EXECUTE_OPERATION = "executeOperation";
public static final String EXECUTE_SERVER_COMMAND = "executeServerCommand";
public static final String EXPORT_SVG = "exportSvg";
public static final String FIT_TO_SCREEN = "fit";
public static final String OPEN = "open";
public static final String NAVIGATE_TO_TARGET = "navigateToTarget";
public static final String REDO = "glspRedo";
public static final String REQUEST_BOUNDS = "requestBounds";
public static final String REQUEST_CLIPBOARD_DATA = "requestClipboardData";
public static final String REQUEST_CONTEXT_ACTIONS = "requestContextActions";
public static final String REQUEST_EDIT_VALIDATION = "requestEditValidation";
public static final String REQUEST_EXPORT_SVG = "requestExportSvg";
public static final String REQUEST_MARKERS = "requestMarkers";
public static final String REQUEST_MODEL = "requestModel";
public static final String REQUEST_NAVIGATION_TARGETS = "requestNavigationTargets";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,10 @@
********************************************************************************/
package org.eclipse.glsp.api.action;

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

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

public interface ActionProcessor {

/**
* <p>
* Process the given action, dispatch to the corresponding handler, and optionally
* send the reply Action to the client.
* </p>
*
* @param clientId The client from which the action was received
* @param action The action to process
*/
default void process(final String clientId, final Action action) {
for (Action responseAction : dispatch(clientId, action)) {
send(clientId, respond(action, responseAction));
}
}

/**
* @see ActionProcessor#process(String, Action)
*
Expand All @@ -48,16 +30,25 @@ default void process(final ActionMessage message) {

/**
* <p>
* Handle the given action, received from the specified clientId, and optionally
* return a reply Action.
* Processes the given action, received from the specified clientId, by dispatching it to all registered handlers.
* </p>
*
* @param clientId The client from which the action was received
* @param action The action to dispatch
* @return An optional Action to be sent to the client as the result of handling
* the received <code>action</code>
*/
List<Action> dispatch(String clientId, Action action);
void process(String clientId, Action action);

/**
* <p>
* Processes all given actions, received from the specified clientId, to the corresponding handlers.
* </p>
*
* @param clientId
* @param actions
*/
default void processAll(final String clientId, final List<Action> actions) {
actions.forEach(action -> process(clientId, action));
}

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
* 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
Expand All @@ -15,20 +15,26 @@
********************************************************************************/
package org.eclipse.glsp.api.action.kind;

import java.util.List;

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

public class CollapseExpandAllAction extends Action {
private boolean expand = true;
public class ClearMarkersAction extends Action {

public CollapseExpandAllAction() {
super(Action.Kind.COLLAPSE_EXPAND_ALL);
private List<Marker> markers;

public ClearMarkersAction() {
super(Action.Kind.CLEAR_MARKERS);
}

public CollapseExpandAllAction(final boolean expand) {
public ClearMarkersAction(final List<Marker> markers) {
this();
this.expand = expand;
this.markers = markers;
}

public boolean isExpand() { return expand; }
public List<Marker> getMarkers() { return markers; }

public void setMarkers(final List<Marker> markers) { this.markers = markers; }

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 EclipseSource and others.
/********************************************************************************
* 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
Expand All @@ -12,22 +12,27 @@
* 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;
import org.eclipse.glsp.api.types.NavigationTarget;

public class OpenAction extends Action {
private String elementId;
public class NavigateToTargetAction extends Action {
private NavigationTarget navigationTarget;

public OpenAction() {
super(Action.Kind.OPEN);
public NavigateToTargetAction() {
super(Action.Kind.NAVIGATE_TO_TARGET);
}

public OpenAction(final String elementId) {
super(Action.Kind.OPEN);
this.elementId = elementId;
public NavigateToTargetAction(final NavigationTarget navigationTarget) {
this();
this.navigationTarget = navigationTarget;
}

public String getElementId() { return elementId; }
public NavigationTarget getNavigationTarget() { return navigationTarget; }

public void setNavigationTarget(final NavigationTarget navigationTarget) {
this.navigationTarget = navigationTarget;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
********************************************************************************/
package org.eclipse.glsp.api.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -34,7 +33,6 @@ public interface CreateOperationHandler extends OperationHandler {
Class<? extends CreateOperation> getHandledOperationType();

default List<TriggerElementCreationAction> getTriggerActions() {
List<TriggerElementCreationAction> actions = new ArrayList<>();
if (CreateNodeOperation.class.isAssignableFrom(getHandledOperationType())) {
return getHandledElementTypeIds().stream().map(TriggerNodeCreationAction::new).collect(Collectors.toList());
} else if (CreateEdgeOperation.class.isAssignableFrom(getHandledOperationType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
********************************************************************************/
package org.eclipse.glsp.server.action;

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

import org.apache.log4j.Logger;
import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.action.ActionMessage;
import org.eclipse.glsp.api.action.ActionProcessor;
import org.eclipse.glsp.api.action.kind.ResponseAction;
import org.eclipse.glsp.api.handler.ActionHandler;
import org.eclipse.glsp.api.jsonrpc.GLSPClient;
import org.eclipse.glsp.api.jsonrpc.GLSPClientProvider;
Expand All @@ -32,6 +33,7 @@
import com.google.inject.Inject;

public class DefaultActionProcessor implements ActionProcessor {
private static Logger LOG = Logger.getLogger(DefaultActionProcessor.class);

@Inject
protected GLSPClientProvider clientProvider;
Expand All @@ -43,15 +45,21 @@ public class DefaultActionProcessor implements ActionProcessor {
protected ModelStateProvider modelStateProvider;

@Override
public List<Action> dispatch(final String clientId, final Action action) {
Optional<ActionHandler> handler = actionHandlerRegistry.get(action).stream().findFirst();
if (handler.isPresent()) {
GraphicalModelState modelState = modelStateProvider.getModelState(clientId)
.orElseGet(() -> modelStateProvider.create(clientId));
public void process(final String clientId, final Action action) {
List<ActionHandler> actionHandlers = actionHandlerRegistry.get(action);
if (actionHandlers.isEmpty()) {
LOG.warn("No handler registered for action: " + action);
return;
}
GraphicalModelState modelState = modelStateProvider.getModelState(clientId)
.orElseGet(() -> modelStateProvider.create(clientId));

return handler.get().execute(action, modelState);
for (ActionHandler actionHandler : actionHandlers) {
List<Action> responses = actionHandler.execute(action, modelState).stream()
.map(response -> ResponseAction.respond(action, response))
.collect(Collectors.toList());
processAll(clientId, responses);
}
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/********************************************************************************
* 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.server.actionhandler;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.glsp.api.action.Action;
import org.eclipse.glsp.api.action.ActionProcessor;
import org.eclipse.glsp.api.handler.ActionHandler;
import org.eclipse.glsp.api.model.GraphicalModelState;

import com.google.inject.Inject;
import com.google.inject.name.Named;

public class ClientActionHandler implements ActionHandler {
public static final String CLIENT_ACTIONS = "ClientActionHandler";

@Inject
private ActionProcessor actionProcessor;

private final List<Class<? extends Action>> handledActionTypes;

@Inject
public ClientActionHandler(@Named(CLIENT_ACTIONS) final Set<Action> clientActions) {
this.handledActionTypes = clientActions.stream().map(Action::getClass).collect(Collectors.toList());
}

@Override
public List<Class<? extends Action>> getHandledActionTypes() { return handledActionTypes; }

@Override
public List<Action> execute(final Action action, final GraphicalModelState modelState) {
actionProcessor.send(modelState.getClientId(), action);
return Collections.emptyList();
}

}
Loading