Skip to content

Commit

Permalink
#425#120 Rework Actionhandler/Operationhandler API (#135)
Browse files Browse the repository at this point in the history
* #425#120 Rework Actionhandler/Operationhandler API

With the DI rework #127 its no longer necessary to pass the model state object as part of the execute method. instead action/operation handlers
can simply directly inject the model state if the want to use it.  Since action & operation handlers are now client session scoped an arbitary state object can be injected and used for execution. (Part of eclipse-glsp/glsp/issues/120)

- Remove the modelstate argument from the execute() method of `ActionHandler` and `OperationHandler`
- Remove `Handler` super interface because it doesn't provide any additional value
- Add documentation for affected API.
- Introduce new `DefaultActionHandler` & `DefaultOperationHandler` that serve as replacement for the now deprecated `BasicActionHandler` & `BasicOperationHandler`
- Deprecate `BasicOperationHandler` & `BasicActionHandler` & `BasicCreateOperationHandler`
- Replace usage of `BasicActionHandler` with `DefaultActionHandler`
- Replace usage of `BasicOperationHandler'  with `DefaultActionHandler`
- Replace usage of `BasicCreateOperationhandler'  with `DefaultOperationHandler` & `DefaultCreateOperationHandler`

Fixes eclipse-glsp/glsp/issues/425


* Remove GModelState from interfaces as it should now be injected instead

This simplifies the use of custom model state classes because
implementations can now inject their concrete custom class instead of
the generic GModelState that they'd have to cast again.
Also several implementations often don't need the model state making the
method parameter superfluous.

Fixes eclipse-glsp/glsp/issues/120


Co-authored-by: Philip Langer <[email protected]>
  • Loading branch information
tortmayr and planger authored Oct 26, 2021
1 parent 7e388ab commit c969357
Show file tree
Hide file tree
Showing 77 changed files with 744 additions and 413 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
* Copyright (c) 2019-2021 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 Down Expand Up @@ -28,7 +28,6 @@
import org.eclipse.glsp.graph.GraphFactory;
import org.eclipse.glsp.server.features.popup.PopupModelFactory;
import org.eclipse.glsp.server.features.popup.RequestPopupModelAction;
import org.eclipse.glsp.server.model.GModelState;

public class WorkflowPopupFactory implements PopupModelFactory {

Expand All @@ -44,8 +43,7 @@ private String generateBody(final TaskNode task) {
private static final String NL = "<br/>";

@Override
public Optional<GHtmlRoot> createPopupModel(final GModelElement element, final RequestPopupModelAction action,
final GModelState modelState) {
public Optional<GHtmlRoot> createPopupModel(final GModelElement element, final RequestPopupModelAction action) {
if (element != null && element instanceof TaskNode) {
TaskNode task = (TaskNode) element;
GHtmlRoot root = GraphFactory.eINSTANCE.createGHtmlRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ protected ActivityNodeBuilder builder(final Optional<GPoint> point, final GModel
}

@Override
protected GNode createNode(final Optional<GPoint> point, final Map<String, String> args,
final GModelState modelState) {
protected GNode createNode(final Optional<GPoint> point, final Map<String, String> args) {
return builder(point, modelState).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ protected CategoryNodeBuilder builder(final Optional<GPoint> point, final GModel
}

@Override
protected GNode createNode(final Optional<GPoint> point, final Map<String, String> args,
final GModelState modelState) {
protected GNode createNode(final Optional<GPoint> point, final Map<String, String> args) {
return builder(point, modelState).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.glsp.graph.GNode;
import org.eclipse.glsp.graph.GPoint;
import org.eclipse.glsp.graph.builder.impl.GLayoutOptions;
import org.eclipse.glsp.server.model.GModelState;

public class CreateDecisionNodeHandler extends CreateActivityNodeHandler {

Expand All @@ -32,8 +31,7 @@ public CreateDecisionNodeHandler() {
}

@Override
protected GNode createNode(final Optional<GPoint> point, final Map<String, String> args,
final GModelState modelState) {
protected GNode createNode(final Optional<GPoint> point, final Map<String, String> args) {
String nodeType = ModelTypes.toNodeType(getElementTypeId());
return new ActivityNodeBuilder(getElementTypeId(), nodeType) //
.layoutOptions(new GLayoutOptions().minHeight(32d).minWidth(32d)) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ protected TaskNodeBuilder builder(final Optional<GPoint> point, final GModelStat
}

@Override
protected GNode createNode(final Optional<GPoint> point, final Map<String, String> args,
final GModelState modelState) {
protected GNode createNode(final Optional<GPoint> point, final Map<String, String> args) {
return builder(point, modelState).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.glsp.graph.GCompartment;
import org.eclipse.glsp.graph.GModelElement;
import org.eclipse.glsp.graph.GPoint;
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.operations.CreateNodeOperation;
import org.eclipse.glsp.server.operations.gmodel.CreateNodeOperationHandler;

Expand All @@ -38,8 +37,8 @@ protected Optional<GPoint> getLocation(final CreateNodeOperation operation) {
}

@Override
protected Optional<GModelElement> getContainer(final CreateNodeOperation operation, final GModelState modelState) {
Optional<GModelElement> container = super.getContainer(operation, modelState);
protected Optional<GModelElement> getContainer(final CreateNodeOperation operation) {
Optional<GModelElement> container = super.getContainer(operation);
// If the container is a Category node, find its structure compartment
Optional<GModelElement> structCompt = container.filter(Category.class::isInstance).map(Category.class::cast)
.flatMap(this::getCategoryCompartment);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2020 EclipseSource and others.
* Copyright (c) 2020-2021 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 @@ -22,15 +22,14 @@
import org.apache.log4j.Logger;
import org.eclipse.glsp.example.workflow.action.LogAction;
import org.eclipse.glsp.server.actions.Action;
import org.eclipse.glsp.server.actions.BasicActionHandler;
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.actions.AbstractActionHandler;
import org.eclipse.glsp.server.types.Severity;

public class LogActionHandler extends BasicActionHandler<LogAction> {
public class LogActionHandler extends AbstractActionHandler<LogAction> {
private static Logger LOG = Logger.getLogger(LogActionHandler.class);

@Override
protected List<Action> executeAction(final LogAction action, final GModelState modelState) {
protected List<Action> executeAction(final LogAction action) {
LOG.log(toLevel(action.getSeverity()), action.getMessage());
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2020 EclipseSource and others.
* Copyright (c) 2020-2021 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 @@ -23,13 +23,12 @@
import org.eclipse.glsp.server.features.contextactions.RequestContextActions;
import org.eclipse.glsp.server.features.contextactions.RequestContextActionsHandler;
import org.eclipse.glsp.server.features.contextactions.SetContextActions;
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.types.Severity;

public class WorkflowRequestContextActionsHandler extends RequestContextActionsHandler {
@Override
public List<Action> executeAction(final RequestContextActions action, final GModelState modelState) {
List<Action> actions = new ArrayList<>(super.executeAction(action, modelState));
public List<Action> executeAction(final RequestContextActions action) {
List<Action> actions = new ArrayList<>(super.executeAction(action));
actions.stream()
.filter(SetContextActions.class::isInstance)
.map(SetContextActions.class::cast)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019-2020 EclipseSource and others.
* Copyright (c) 2019-2021 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 @@ -23,10 +23,15 @@
import org.eclipse.glsp.server.features.directediting.ValidationStatus;
import org.eclipse.glsp.server.model.GModelState;

import com.google.inject.Inject;

public class WorkflowLabelEditValidator implements LabelEditValidator {

@Inject
protected GModelState modelState;

@Override
public ValidationStatus validate(final GModelState modelState, final String label, final GModelElement element) {
public ValidationStatus validate(final String label, final GModelElement element) {
if (label.length() < 1) {
return ValidationStatus.error("Name must not be empty");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
* Copyright (c) 2019-2021 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 @@ -20,9 +20,15 @@
import org.eclipse.glsp.layout.GLSPLayoutConfigurator;
import org.eclipse.glsp.server.model.GModelState;

import com.google.inject.Inject;

public class WorkflowLayoutEngine extends ElkLayoutEngine {

@Inject
protected GModelState modelState;

@Override
public void layout(final GModelState modelState) {
public void layout() {
if (modelState.getRoot() instanceof GGraph) {
GLSPLayoutConfigurator configurator = new GLSPLayoutConfigurator();
configurator.configureByType("graph");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
* Copyright (c) 2019-2021 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 Down Expand Up @@ -33,10 +33,15 @@
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.utils.GModelUtil;

import com.google.inject.Inject;

public class WorkflowModelValidator implements ModelValidator {

@Inject
protected GModelState modelState;

@Override
public List<Marker> validate(final GModelState modelState, final GModelElement... elements) {
public List<Marker> validate(final GModelElement... elements) {
List<Marker> markers = new ArrayList<>();

for (GModelElement element : elements) {
Expand All @@ -51,8 +56,7 @@ public List<Marker> validate(final GModelState modelState, final GModelElement..
}
}
if (element.getChildren() != null) {
markers.addAll(validate(modelState,
element.getChildren().toArray(new GModelElement[element.getChildren().size()])));
markers.addAll(validate(element.getChildren().toArray(new GModelElement[element.getChildren().size()])));
}
}
return markers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
import org.eclipse.glsp.server.utils.ClientOptionsUtil;
import org.eclipse.glsp.server.utils.MapUtil;

import com.google.inject.Inject;

public abstract class AbstractNextOrPreviousNavigationTargetProvider implements NavigationTargetProvider {

@Inject
protected GModelState modelState;

@Override
public List<? extends NavigationTarget> getTargets(final EditorContext editorContext,
final GModelState modelState) {
public List<? extends NavigationTarget> getTargets(final EditorContext editorContext) {
Optional<String> sourceUri = MapUtil.getValue(modelState.getClientOptions(), ClientOptionsUtil.SOURCE_URI);
return editorContext.getSelectedElementIds().stream()
.flatMap(id -> modelState.getIndex().get(id).stream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.eclipse.glsp.server.utils.ClientOptionsUtil;
import org.eclipse.glsp.server.utils.MapUtil;

import com.google.inject.Inject;

/**
* An example {@link NavigationTargetProvider} that opens an md file and selects a specified range.
* <p>
Expand All @@ -41,8 +43,11 @@ public class NodeDocumentationNavigationTargetProvider implements NavigationTarg
@Override
public String getTargetTypeId() { return "documentation"; }

@Inject
protected GModelState modelState;

@Override
public List<? extends NavigationTarget> getTargets(final EditorContext editorContext, final GModelState modelState) {
public List<? extends NavigationTarget> getTargets(final EditorContext editorContext) {
if (editorContext.getSelectedElementIds().size() == 1) {
Optional<TaskNode> taskNode = modelState.getIndex()
.findElementByClass(editorContext.getSelectedElementIds().get(0), TaskNode.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.inject.Inject;

public class WorkflowCommandPaletteActionProvider implements CommandPaletteActionProvider {

@Inject
protected GModelState modelState;

@Override
@SuppressWarnings("checkstyle:CyclomaticComplexity")
public List<LabeledAction> getActions(final EditorContext editorContext, final GModelState modelState) {
public List<LabeledAction> getActions(final EditorContext editorContext) {
List<LabeledAction> actions = Lists.newArrayList();
if (modelState.isReadonly()) {
return actions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
* Copyright (c) 2019-2021 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 Down Expand Up @@ -31,13 +31,16 @@
import org.eclipse.glsp.server.operations.CreateNodeOperation;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

public class WorkflowContextMenuItemProvider implements ContextMenuItemProvider {

@Inject
protected GModelState modelState;

@Override
public List<MenuItem> getItems(final List<String> selectedElementIds, final GPoint position,
final Map<String, String> args,
final GModelState modelState) {
final Map<String, String> args) {
if (modelState.isReadonly()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@

import org.eclipse.glsp.server.actions.ActionDispatcher;
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.operations.BasicOperationHandler;
import org.eclipse.glsp.server.operations.AbstractOperationHandler;
import org.eclipse.glsp.server.types.GLSPServerException;

import com.google.inject.Inject;

public class ApplyTaskEditOperationHandler extends BasicOperationHandler<ApplyTaskEditOperation> {
public class ApplyTaskEditOperationHandler extends AbstractOperationHandler<ApplyTaskEditOperation> {

@Inject
private ActionDispatcher actionProcessor;
protected ActionDispatcher actionDispatcher;

@Inject
protected GModelState modelState;

@Override
protected void executeOperation(final ApplyTaskEditOperation operation, final GModelState modelState) {
protected void executeOperation(final ApplyTaskEditOperation operation) {
String text = operation.getExpression();
if (text.startsWith(TaskEditContextActionProvider.DURATION_PREFIX)) {
String durationString = text.substring(TaskEditContextActionProvider.DURATION_PREFIX.length());
actionProcessor.dispatch(new EditTaskOperation(operation.getTaskId(), "duration", durationString));
actionDispatcher.dispatch(new EditTaskOperation(operation.getTaskId(), "duration", durationString));
} else if (text.startsWith(TaskEditContextActionProvider.TYPE_PREFIX)) {
String typeString = text.substring(TaskEditContextActionProvider.TYPE_PREFIX.length());
actionProcessor.dispatch(new EditTaskOperation(operation.getTaskId(), "taskType", typeString));
actionDispatcher.dispatch(new EditTaskOperation(operation.getTaskId(), "taskType", typeString));
} else {
throw new GLSPServerException(
"Cannot process 'ApplyTaskEditOperation' expression: " + operation.getExpression());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@

import org.eclipse.glsp.example.workflow.wfgraph.TaskNode;
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.operations.BasicOperationHandler;
import org.eclipse.glsp.server.operations.AbstractOperationHandler;
import org.eclipse.glsp.server.types.GLSPServerException;

public class EditTaskOperationHandler extends BasicOperationHandler<EditTaskOperation> {
import com.google.inject.Inject;

public class EditTaskOperationHandler extends AbstractOperationHandler<EditTaskOperation> {

@Inject
protected GModelState modelState;

@Override
protected void executeOperation(final EditTaskOperation operation, final GModelState modelState) {
protected void executeOperation(final EditTaskOperation operation) {
Optional<TaskNode> task = modelState.getIndex().findElementByClass(operation.getTaskId(), TaskNode.class);
if (task.isEmpty()) {
throw new RuntimeException("Cannot find task with id '" + operation.getTaskId() + "'");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2020 EclipseSource and others.
* Copyright (c) 2020-2021 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 Down Expand Up @@ -27,6 +27,7 @@
import org.eclipse.glsp.server.types.EditorContext;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

public class TaskEditContextActionProvider implements ContextActionsProvider {

Expand All @@ -37,8 +38,11 @@ public class TaskEditContextActionProvider implements ContextActionsProvider {
@Override
public String getContextId() { return "task-editor"; }

@Inject
protected GModelState modelState;

@Override
public List<? extends LabeledAction> getActions(final EditorContext editorContext, final GModelState modelState) {
public List<? extends LabeledAction> getActions(final EditorContext editorContext) {
String text = editorContext.getArgs().getOrDefault("text", "");
Optional<TaskNode> taskNode = modelState.getIndex()
.findElementByClass(editorContext.getSelectedElementIds().get(0), TaskNode.class);
Expand Down
Loading

0 comments on commit c969357

Please sign in to comment.