Skip to content

Commit

Permalink
#738: Extend ComputedBoundsAction with routing points
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-fleck-at committed Oct 15, 2022
1 parent fa089aa commit c414ace
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,31 @@
import org.eclipse.glsp.server.actions.Action;
import org.eclipse.glsp.server.types.ElementAndAlignment;
import org.eclipse.glsp.server.types.ElementAndBounds;
import org.eclipse.glsp.server.types.ElementAndRoutingPoints;

public class ComputedBoundsAction extends Action {

public static final String KIND = "computedBounds";

private List<ElementAndBounds> bounds;
private List<ElementAndAlignment> alignments;
private List<ElementAndRoutingPoints> routingPoints;
private int revision;

public ComputedBoundsAction() {
super(KIND);
this.bounds = new ArrayList<>();
this.alignments = new ArrayList<>();
this.routingPoints = new ArrayList<>();
}

public ComputedBoundsAction(final List<ElementAndBounds> bounds, final List<ElementAndAlignment> alignments,
final int revision) {
final int revision, final List<ElementAndRoutingPoints> routingPoints) {
super(KIND);
this.bounds = bounds;
this.alignments = alignments;
this.revision = revision;
this.routingPoints = routingPoints;
}

public List<ElementAndBounds> getBounds() { return bounds; }
Expand All @@ -53,6 +57,12 @@ public ComputedBoundsAction(final List<ElementAndBounds> bounds, final List<Elem

public void setAlignments(final List<ElementAndAlignment> alignments) { this.alignments = alignments; }

public List<ElementAndRoutingPoints> getRoutingPoints() { return routingPoints; }

public void setRoutingPoints(final List<ElementAndRoutingPoints> routingPoints) {
this.routingPoints = routingPoints;
}

public Optional<Integer> getRevision() { return Optional.ofNullable(revision); }

public void setRevision(final int revision) { this.revision = revision; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
********************************************************************************/
package org.eclipse.glsp.server.gmodel;

import static org.eclipse.glsp.server.types.GLSPServerException.getOrThrow;

import org.eclipse.emf.common.util.EList;
import org.eclipse.glsp.graph.GEdge;
import org.eclipse.glsp.graph.GModelIndex;
import org.eclipse.glsp.graph.GPoint;
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.operations.AbstractOperationHandler;
import org.eclipse.glsp.server.operations.ChangeRoutingPointsOperation;
import org.eclipse.glsp.server.types.ElementAndRoutingPoints;
import org.eclipse.glsp.server.utils.LayoutUtil;

import com.google.inject.Inject;

Expand All @@ -38,24 +33,11 @@ public class GModelChangeRoutingPointsHandler extends AbstractOperationHandler<C

@Override
protected void executeOperation(final ChangeRoutingPointsOperation operation) {

// check for null-values
if (operation.getNewRoutingPoints() == null) {
throw new IllegalArgumentException("Incomplete change routingPoints action");
}

// check for existence of matching elements
GModelIndex index = modelState.getIndex();

for (ElementAndRoutingPoints ear : operation.getNewRoutingPoints()) {
GEdge edge = getOrThrow(index.findElementByClass(ear.getElementId(), GEdge.class),
"Invalid edge: edge ID " + ear.getElementId());

// reroute
EList<GPoint> routingPoints = edge.getRoutingPoints();
routingPoints.clear();
routingPoints.addAll(ear.getNewRoutingPoints());
}

operation.getNewRoutingPoints().forEach(routingPoints -> LayoutUtil.applyRoutingPonts(routingPoints, index));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Optional;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.glsp.graph.GAlignable;
import org.eclipse.glsp.graph.GBounds;
Expand All @@ -36,6 +37,7 @@
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.types.ElementAndAlignment;
import org.eclipse.glsp.server.types.ElementAndBounds;
import org.eclipse.glsp.server.types.ElementAndRoutingPoints;

public final class LayoutUtil {

Expand All @@ -51,29 +53,60 @@ private LayoutUtil() {}
public static void applyBounds(final GModelRoot root, final ComputedBoundsAction action,
final GModelState modelState) {
GModelIndex index = modelState.getIndex();
for (ElementAndBounds b : action.getBounds()) {
GModelElement element = getOrThrow(index.get(b.getElementId()),
"Model element not found! ID: " + b.getElementId());
if (element instanceof GBoundsAware) {
GBoundsAware bae = (GBoundsAware) element;
if (b.getNewPosition() != null) {
bae.setPosition(GraphUtil.copy(b.getNewPosition()));
}
if (b.getNewSize() != null) {
bae.setSize(GraphUtil.copy(b.getNewSize()));
}
action.getBounds().forEach(bounds -> applyBounds(bounds, index));
action.getAlignments().forEach(alignment -> applyAlignment(alignment, index));
action.getRoutingPoints().forEach(routingPoints -> applyRoutingPonts(routingPoints, index));
}

/**
* Applies the new bounds to the model.
*
* @param bounds The new bounds.
* @param index The model index.
*/
public static void applyBounds(final ElementAndBounds bounds, final GModelIndex index) {
GModelElement element = getOrThrow(index.get(bounds.getElementId()),
"Model element not found! ID: " + bounds.getElementId());
if (element instanceof GBoundsAware) {
GBoundsAware bae = (GBoundsAware) element;
if (bounds.getNewPosition() != null) {
bae.setPosition(GraphUtil.copy(bounds.getNewPosition()));
}
}
for (ElementAndAlignment a : action.getAlignments()) {
GModelElement element = getOrThrow(index.get(a.getElementId()),
"Model element not found! ID: " + a.getElementId());
if (element instanceof GAlignable) {
GAlignable alignable = (GAlignable) element;
alignable.setAlignment(a.getNewAlignment());
if (bounds.getNewSize() != null) {
bae.setSize(GraphUtil.copy(bounds.getNewSize()));
}
}
}

/**
* Applies the new alignment to the model.
*
* @param alignment The new alignment.
* @param index The model index.
*/
public static void applyAlignment(final ElementAndAlignment alignment, final GModelIndex index) {
GModelElement element = getOrThrow(index.get(alignment.getElementId()),
"Model element not found! ID: " + alignment.getElementId());
if (element instanceof GAlignable) {
GAlignable alignable = (GAlignable) element;
alignable.setAlignment(alignment.getNewAlignment());
}
}

/**
* Applies the new routing points to the model.
*
* @param routingPoints The new routing points.
* @param index The model index.
*/
public static void applyRoutingPonts(final ElementAndRoutingPoints routingPoints, final GModelIndex index) {
GEdge edge = getOrThrow(index.findElementByClass(routingPoints.getElementId(), GEdge.class),
"Model element not found! ID: " + routingPoints.getElementId());
EList<GPoint> edgeRoutingPoints = edge.getRoutingPoints();
edgeRoutingPoints.clear();
edgeRoutingPoints.addAll(routingPoints.getNewRoutingPoints());
}

/**
* Returns the relative location of the given absolute location within the container.
*
Expand Down

0 comments on commit c414ace

Please sign in to comment.