Skip to content

Commit

Permalink
Add ghost elements to tool palette items through trigger actions (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-fleck-at authored Nov 21, 2023
1 parent d7272cf commit 282656a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
********************************************************************************/
package org.eclipse.glsp.example.workflow.handler;

import java.util.HashMap;
import java.util.Optional;

import org.eclipse.glsp.example.workflow.utils.ModelTypes;
import org.eclipse.glsp.example.workflow.wfgraph.Category;
import org.eclipse.glsp.graph.GCompartment;
import org.eclipse.glsp.graph.GModelElement;
import org.eclipse.glsp.graph.GPoint;
import org.eclipse.glsp.server.actions.GhostElement;
import org.eclipse.glsp.server.gmodel.GModelCreateNodeOperationHandler;
import org.eclipse.glsp.server.operations.CreateNodeOperation;

Expand Down Expand Up @@ -50,4 +52,8 @@ protected Optional<GCompartment> getCategoryCompartment(final Category category)
.filter(comp -> ModelTypes.STRUCTURE.equals(comp.getType())).findFirst();
}

@Override
protected GhostElement createTriggerGhostElement(final String elementTypeId) {
return new GhostElement(createNode(Optional.empty(), new HashMap<>()), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
package org.eclipse.glsp.server.emf;

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

import org.eclipse.glsp.server.actions.ActionDispatcher;
import org.eclipse.glsp.server.actions.GhostElement;
import org.eclipse.glsp.server.actions.TriggerElementCreationAction;
import org.eclipse.glsp.server.actions.TriggerNodeCreationAction;
import org.eclipse.glsp.server.operations.CreateNodeOperation;
import org.eclipse.glsp.server.operations.CreateOperation;
import org.eclipse.glsp.server.operations.CreateOperationHandler;

Expand Down Expand Up @@ -46,4 +52,27 @@ public EMFCreateOperationHandler(final List<String> handledElementTypeIds) {
public void setHandledElementTypeIds(final List<String> handledElementTypeIds) {
this.handledElementTypeIds = handledElementTypeIds;
}

@Override
public List<TriggerElementCreationAction> getTriggerActions() {
if (CreateNodeOperation.class.isAssignableFrom(getHandledOperationType())) {
return getHandledElementTypeIds().stream().map(this::createTriggerNodeCreationAction)
.collect(Collectors.toList());
}
return CreateOperationHandler.super.getTriggerActions();
}

protected TriggerNodeCreationAction createTriggerNodeCreationAction(final String elementTypeId) {
return new TriggerNodeCreationAction(elementTypeId,
createTriggerArgs(elementTypeId),
createTriggerGhostElement(elementTypeId));
}

protected GhostElement createTriggerGhostElement(final String elementTypeId) {
return null;
}

protected Map<String, String> createTriggerArgs(final String elementTypeId) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/********************************************************************************
* Copyright (c) 2023 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.actions;

import java.util.Optional;

import org.eclipse.glsp.graph.GModelElement;

/**
* A ghost element describes an element that may be rendered as a schematic visualization of an element that may be
* inserted during creation.
*/
public class GhostElement {
private Object template;
private Boolean dynamic;

public GhostElement(final String templateElementId) {
this.template = templateElementId;
}

public GhostElement(final String templateElementId, final boolean dynamic) {
this.template = templateElementId;
this.dynamic = dynamic;
}

public GhostElement(final GModelElement element) {
this.template = element;
}

public GhostElement(final GModelElement element, final boolean dynamic) {
this.template = element;
this.dynamic = dynamic;
}

public Optional<String> getTemplateElementId() {
return template instanceof String templateElementId ? Optional.of(templateElementId) : Optional.empty();
}

public void setTemplateElementId(final String templateElementId) { this.template = templateElementId; }

public Optional<GModelElement> getTemplateElement() {
return template instanceof GModelElement templateElement ? Optional.of(templateElement) : Optional.empty();
}

public void setTemplateElement(final GModelElement templateElement) { this.template = templateElement; }

public Boolean getDynamic() { return dynamic; }

public void setDynamic(final boolean dynamic) { this.dynamic = dynamic; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class TriggerNodeCreationAction extends TriggerElementCreationAction {

public static final String KIND = "triggerNodeCreation";

private GhostElement ghostElement;

public TriggerNodeCreationAction() {
this(null);
}
Expand All @@ -33,9 +35,23 @@ public TriggerNodeCreationAction(final String elementTypeId) {
this(elementTypeId, new HashMap<>());
}

public TriggerNodeCreationAction(final String elementTypeId, final GhostElement ghostElement) {
this(elementTypeId, new HashMap<>(), ghostElement);
}

public TriggerNodeCreationAction(final String elementTypeId,
final Map<String, String> args) {
super(KIND, elementTypeId, args);
}

public TriggerNodeCreationAction(final String elementTypeId,
final Map<String, String> args, final GhostElement ghostElement) {
super(KIND, elementTypeId, args);
this.ghostElement = ghostElement;
}

public GhostElement getGhostElement() { return ghostElement; }

public void setGhostElement(final GhostElement ghostElement) { this.ghostElement = ghostElement; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.emf.common.command.Command;
import org.eclipse.glsp.graph.GModelElement;
import org.eclipse.glsp.graph.GNode;
import org.eclipse.glsp.graph.GPoint;
import org.eclipse.glsp.server.actions.ActionDispatcher;
import org.eclipse.glsp.server.actions.GhostElement;
import org.eclipse.glsp.server.actions.SelectAction;
import org.eclipse.glsp.server.actions.TriggerElementCreationAction;
import org.eclipse.glsp.server.actions.TriggerNodeCreationAction;
import org.eclipse.glsp.server.operations.CreateEdgeOperation;
import org.eclipse.glsp.server.operations.CreateNodeOperation;
import org.eclipse.glsp.server.utils.LayoutUtil;
Expand Down Expand Up @@ -86,6 +90,26 @@ protected Optional<GModelElement> getContainer(final CreateNodeOperation operati
return modelState.getIndex().get(operation.getContainerId());
}

@Override
public List<TriggerElementCreationAction> getTriggerActions() {
return getHandledElementTypeIds().stream().map(this::createTriggerNodeCreationAction)
.collect(Collectors.toList());
}

protected TriggerNodeCreationAction createTriggerNodeCreationAction(final String elementTypeId) {
return new TriggerNodeCreationAction(elementTypeId,
createTriggerArgs(elementTypeId),
createTriggerGhostElement(elementTypeId));
}

protected GhostElement createTriggerGhostElement(final String elementTypeId) {
return null;
}

protected Map<String, String> createTriggerArgs(final String elementTypeId) {
return null;
}

/**
* Create and return the new Node at the specified (optional) location. The location
* is given in coordinates relative to the
Expand Down

0 comments on commit 282656a

Please sign in to comment.