From 15133f8635db1a8fb86d40ec395c7efce0f3dcaf Mon Sep 17 00:00:00 2001 From: Vincent Lorenzo Date: Fri, 6 Jun 2014 15:11:09 +0200 Subject: clean deferred command --- .../commands/FixEdgeAnchorsDeferredCommand.java | 329 +++++++-------------- .../XYLayoutWithConstrainedResizedEditPolicy.java | 2 +- .../common/helper/IdentityAnchorHelper.java | 1 - .../infra/gmfdiag/common/routers/RouterUtil.java | 144 +++++++++ 4 files changed, 248 insertions(+), 228 deletions(-) create mode 100644 plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/routers/RouterUtil.java diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/FixEdgeAnchorsDeferredCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/FixEdgeAnchorsDeferredCommand.java index 932d016c8b8..ea03c402beb 100755 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/FixEdgeAnchorsDeferredCommand.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/FixEdgeAnchorsDeferredCommand.java @@ -18,6 +18,7 @@ package org.eclipse.papyrus.infra.gmfdiag.common.commands; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -25,33 +26,33 @@ import java.util.List; import java.util.Map; import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.draw2d.Connection; -import org.eclipse.draw2d.ConnectionAnchor; -import org.eclipse.draw2d.ConnectionRouter; import org.eclipse.draw2d.IFigure; -import org.eclipse.draw2d.RelativeBendpoint; -import org.eclipse.draw2d.geometry.Dimension; -import org.eclipse.draw2d.geometry.Point; import org.eclipse.draw2d.geometry.PointList; import org.eclipse.draw2d.geometry.PrecisionPoint; import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; +import org.eclipse.gef.EditPart; import org.eclipse.gef.editparts.AbstractConnectionEditPart; +import org.eclipse.gef.requests.ChangeBoundsRequest; import org.eclipse.gmf.runtime.common.core.command.CommandResult; +import org.eclipse.gmf.runtime.common.core.command.CompositeCommand; import org.eclipse.gmf.runtime.diagram.core.commands.SetConnectionAnchorsCommand; import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; -import org.eclipse.gmf.runtime.diagram.ui.editparts.INodeEditPart; import org.eclipse.gmf.runtime.diagram.ui.internal.commands.SetConnectionBendpointsCommand; import org.eclipse.gmf.runtime.diagram.ui.util.EditPartUtil; import org.eclipse.gmf.runtime.draw2d.ui.figures.BaseSlidableAnchor; -import org.eclipse.gmf.runtime.draw2d.ui.figures.IAnchorableFigure; -import org.eclipse.gmf.runtime.draw2d.ui.figures.PolylineConnectionEx; +import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; import org.eclipse.gmf.runtime.notation.Edge; -import org.eclipse.papyrus.infra.gmfdiag.common.figure.edge.CustomRelativeBendpoint; +import org.eclipse.gmf.runtime.notation.View; import org.eclipse.papyrus.infra.gmfdiag.common.helper.IdentityAnchorHelper; +import org.eclipse.papyrus.infra.gmfdiag.common.routers.RouterUtil; /** @@ -61,270 +62,138 @@ import org.eclipse.papyrus.infra.gmfdiag.common.helper.IdentityAnchorHelper; * This class allows to fix the anchors for a collection of connection edit part * */ -public class FixEdgeAnchorsDeferredCommand extends AbstractFixEdgeAnchorDeferredCommand { +//FIXME : rename me into FixEdgePointDeferredCommand +@SuppressWarnings("restriction") +public class FixEdgeAnchorsDeferredCommand extends AbstractTransactionalCommand { /** * the list of the connections to refresh */ private Collection connectionsEditPartToRefresh; - private Map map = new HashMap(); + /** the diagram editpart used to get the editpart registry */ + private IGraphicalEditPart containerEP; - public static class ConnectionInformation { - - private PointList initialPointList; - - private String sourceAnchorInit; - - private String targetAnchorInit; - - private Point sourcePoint; - - private Point targetPoint; - - private final Connection conn; - - public ConnectionInformation(Connection conn) { - this.conn = conn; - this.initialPointList = this.conn.getPoints().getCopy(); - this.sourcePoint = this.conn.getSourceAnchor().getReferencePoint().getCopy(); - this.targetPoint = this.conn.getTargetAnchor().getReferencePoint().getCopy(); - // this.conn.getTargetAnchor().getOwner()? - } - - public PointList getInitialPointList() { - return this.initialPointList.getCopy(); - } - } - - private INodeEditPart child; + /** + * the initial point lists of the connection before the refresh of the diagram + */ + private Map initialPointLists; + /** + * the initial bounds request + */ + private ChangeBoundsRequest request; /** * * Constructor. * * @param editingDomain - * the editing domain - * @param containerEP - * the diagram edit part - * @param movedChild - * TODO * @param request - * the creation request + * @param containerEP + * @param connectionsEditPartToRefresh */ - public FixEdgeAnchorsDeferredCommand(final TransactionalEditingDomain editingDomain, final IGraphicalEditPart containerEP, final Collection connectionsEditPartToRefresh, INodeEditPart movedChild) { - super(editingDomain, "Fix Edge Anchors", containerEP); //$NON-NLS-1$ + public FixEdgeAnchorsDeferredCommand(final TransactionalEditingDomain editingDomain, ChangeBoundsRequest request, final IGraphicalEditPart containerEP, final Collection connectionsEditPartToRefresh) { + super(editingDomain, "Fix Edge Anchors", null); //$NON-NLS-1$ + this.containerEP = containerEP; + this.initialPointLists = new HashMap(); + this.request = request; this.connectionsEditPartToRefresh = new ArrayList(connectionsEditPartToRefresh); - this.child = movedChild; for(Object object : connectionsEditPartToRefresh) { if(object instanceof IGraphicalEditPart) { final IFigure fig = ((IGraphicalEditPart)object).getFigure(); if(fig instanceof Connection) { - Connection fig1 = (Connection)fig; - this.map.put(fig1, new ConnectionInformation(fig1)); + final Connection conn = (Connection)fig; + this.initialPointLists.put(conn, conn.getPoints().getCopy()); } } } - this.child = movedChild; - } - protected static PointList routeDummyConnection(final Connection connection, final boolean isMovingSource, final Map map) { - //2. we create a dummy connection from current connection, with new informations to determine real anchor point - PointList list = map.get(connection).initialPointList; - System.out.println("BEEFORE"); - for(int i = 0; i < list.size(); i++) { - System.out.println(list.getPoint(i)); - } - - Point first = list.getFirstPoint(); - int size = list.size(); - Point last = list.getLastPoint(); - List relatedBendpoints = new ArrayList(); - // List relatedBendpoints = new ArrayList(); - - // first = GridUtils.getFeedBackPointFromGridCoordinate(first, this.child); - // last = GridUtils.getFeedBackPointFromGridCoordinate(last, this.child); - final Connection dummyConnection = new PolylineConnectionEx(); - for(int i = 0; i < list.size(); i++) { - float c1 = 1.0f; - float f2 = 0.0f; - Point current = list.getPoint(i); - // current = GridUtils.getFeedBackPointFromGridCoordinate(current, this.child); - final Dimension s = current.getDifference(first); - final Dimension t = current.getDifference(last); - final CustomRelativeBendpoint relatedBendpoint = new CustomRelativeBendpoint(connection);//TODO FIXME WARNING only for test - relatedBendpoint.setRelativeDimensions(s, t); - relatedBendpoints.add(relatedBendpoint); - if(isMovingSource) { - - if(i > 1) { - System.out.println("dedans"); - relatedBendpoint.setWeight(1.0f); - } else { - relatedBendpoint.setWeight(0); - } - } else { - // if(i < size - 2) {//FIXME : change me in the other location were i'm used - if(i < size - 2) { - // System.out.println("dedans"); - relatedBendpoint.setWeight(0); - } else { - // relatedBendpoint.setWeight(1.0f, 0f);//need more test - if(i == size - 1) { - relatedBendpoint.setWeight(1.0f); - } else - //ok for horizontal target - if(current.y == last.y) { - relatedBendpoint.setWeight(0f, 1.0f); - } else if(current.x == last.x) { - relatedBendpoint.setWeight(1.0f, 0f); - } - // relatedBendpoint.setWeight(1.0f); - } - - } - } - // final Connection dummyConnection = new PolylineConnectionEx(); - - IFigure node = null; - if(isMovingSource) { - node = connection.getSourceAnchor().getOwner(); - } else { - node = connection.getTargetAnchor().getOwner(); - } - //NodeFigure fig = new NodeFigure(); - //fig.setBounds(node.getBounds()); - //fig.setLocation(new Point(600,20)); - if(isMovingSource) { - ConnectionAnchor sourceAnchor = ((IAnchorableFigure)node).getSourceConnectionAnchorAt(connection.getSourceAnchor().getReferencePoint()); - final Point targetPoint = connection.getTargetAnchor().getReferencePoint(); - final ConnectionAnchor targetAnchor = ((IAnchorableFigure)connection.getTargetAnchor().getOwner()).getTargetConnectionAnchorAt(targetPoint); - dummyConnection.setSourceAnchor(sourceAnchor); - dummyConnection.setTargetAnchor(targetAnchor); - } else { - ConnectionAnchor targetAnchor = ((IAnchorableFigure)node).getTargetConnectionAnchorAt(connection.getTargetAnchor().getReferencePoint()); - final Point sourcePoint = connection.getSourceAnchor().getReferencePoint(); - // connection.getSourceAnchor().getOwner().translateToRelative(sourcePoint); - final ConnectionAnchor sourceAnchor = ((IAnchorableFigure)connection.getSourceAnchor().getOwner()).getSourceConnectionAnchorAt(sourcePoint); - dummyConnection.setSourceAnchor(sourceAnchor); - dummyConnection.setTargetAnchor(targetAnchor); - } - - //for test - // Point p1 = list.getFirstPoint(); - // Point p2 = list.getLastPoint(); - // connection.translateToRelative(p1); - // connection.translateToRelative(p2); - // ConnectionAnchor targetAnchor = ((IAnchorableFigure)node).getTargetConnectionAnchorAt(p2); - // final Point sourcePoint = p1; - // final ConnectionAnchor sourceAnchor = ((IAnchorableFigure)connection.getSourceAnchor().getOwner()).getSourceConnectionAnchorAt(sourcePoint); - // dummyConnection.setSourceAnchor(sourceAnchor); - // dummyConnection.setTargetAnchor(targetAnchor); - //end of FOR test - - - // PointList newPoints = initialPoints.getCopy(); - PointList newPoints = map.get(connection).initialPointList.getCopy(); - IFigure parent = connection.getParent(); - // final Layer layer = DiagramEditPartsUtil.getDiagramConnectionLayer(getContainerEP()); - final IFigure layer = parent; - dummyConnection.setParent(layer); - dummyConnection.setPoints(newPoints);//required? - - - //configure the router for the dummy connection - final ConnectionRouter router = connection.getConnectionRouter(); - router.setConstraint(dummyConnection, relatedBendpoints); - dummyConnection.setConnectionRouter(router); - router.route(dummyConnection); - - final PointList res = dummyConnection.getPoints(); - dummyConnection.setRoutingConstraint(null); - dummyConnection.setConnectionRouter(null); - return res; - } - - - /** - * Executes a fix anchor command for the created edge + * Fix the anchor and the bendpoints * */ @Override protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { - Point newLocation = this.child.getFigure().getBounds().getLocation(); - boolean movingTarget = true; + //1.refresh the container (force the routing of existing link + final RefreshConnectionElementsRunnable refreshRunnable = new RefreshConnectionElementsRunnable(this.connectionsEditPartToRefresh, this.containerEP); + EditPartUtil.synchronizeRunnableToMainThread(this.containerEP, refreshRunnable); + for(AbstractConnectionEditPart conn : refreshRunnable.getResult()) { - //this.connectionsEditPartToRefresh.add(this.child); - final RefreshConnectionElementsRunnable refreshRunnable = new RefreshConnectionElementsRunnable(this.connectionsEditPartToRefresh, getContainerEP()); - + //2.detemrine if we are moving source or target + final boolean isMovingSource; + final Connection fig = (Connection)conn.getFigure(); + final EditPart sourceEP = conn.getSource(); + final EditPart targetEP = conn.getTarget(); + if(sourceEP == targetEP) { + //we do nothing + continue; + } + List movedEP = this.request.getEditParts(); + if(movedEP.contains(sourceEP) && movedEP.contains(targetEP)) { + //in some case there is nothing to do + //in some others cases (port ? ) it could be interesting to do something + //we decide to follow the source! + isMovingSource = true; + } else if(movedEP.contains(sourceEP)) { + isMovingSource = true; + } else if(movedEP.contains(targetEP)) { + isMovingSource = false; + } else { + throw new UnknownError("we are updating link points related to an unknown source/target"); //$NON-NLS-1$ + } - EditPartUtil.synchronizeRunnableToMainThread(getContainerEP(), refreshRunnable); + final PointList newPointList = RouterUtil.routeDummyConnection(fig, this.initialPointLists.get(fig), isMovingSource); + Edge edge = (Edge)conn.getAdapter(Edge.class); + final EObjectAdapter adapter = new EObjectAdapter(edge); + final CompositeCommand fixEdgePointCommand = new CompositeCommand("Fix Edge Point Command"); - for(AbstractConnectionEditPart conn : refreshRunnable.getResult()) { - final Connection fig = (Connection)conn.getFigure(); - PointList res2 = routeDummyConnection(fig, false, this.map); - Edge edge = (Edge)conn.getAdapter(Edge.class); - SetConnectionAnchorsCommand setAnchorCommand = new SetConnectionAnchorsCommand(getEditingDomain(), ""); - setAnchorCommand.setEdgeAdaptor(new EObjectAdapter(edge)); - PrecisionPoint sourcePer = BaseSlidableAnchor.getAnchorRelativeLocation(res2.getFirstPoint(), fig.getSourceAnchor().getOwner().getBounds()); + final SetConnectionAnchorsCommand setAnchorCommand = new SetConnectionAnchorsCommand(getEditingDomain(), ""); //$NON-NLS-1$ + setAnchorCommand.setEdgeAdaptor(adapter); - String newSourceTerminal = IdentityAnchorHelper.createNewAnchorIdValue(sourcePer); + final PrecisionPoint sourceLocation = BaseSlidableAnchor.getAnchorRelativeLocation(newPointList.getFirstPoint(), fig.getSourceAnchor().getOwner().getBounds()); + final String newSourceTerminal = IdentityAnchorHelper.createNewAnchorIdValue(sourceLocation); setAnchorCommand.setNewSourceTerminal(newSourceTerminal); - PrecisionPoint targetPer = BaseSlidableAnchor.getAnchorRelativeLocation(res2.getLastPoint(), fig.getTargetAnchor().getOwner().getBounds()); - String newTargetTerminal = IdentityAnchorHelper.createNewAnchorIdValue(targetPer); + final PrecisionPoint targetLocation = BaseSlidableAnchor.getAnchorRelativeLocation(newPointList.getLastPoint(), fig.getTargetAnchor().getOwner().getBounds()); + final String newTargetTerminal = IdentityAnchorHelper.createNewAnchorIdValue(targetLocation); setAnchorCommand.setNewTargetTerminal(newTargetTerminal); - setAnchorCommand.execute(null, null); - SetConnectionBendpointsCommand setBendpoints = new SetConnectionBendpointsCommand(getEditingDomain()); - setBendpoints.setEdgeAdapter(new EObjectAdapter(edge)); - setBendpoints.setNewPointList(res2, res2.getFirstPoint(), res2.getLastPoint()); - setBendpoints.execute(null, null); - } - - - // String newSourceID = + fixEdgePointCommand.add(setAnchorCommand); + SetConnectionBendpointsCommand setBendpointsCommand = new SetConnectionBendpointsCommand(getEditingDomain()); + setBendpointsCommand.setEdgeAdapter(adapter); + setBendpointsCommand.setNewPointList(newPointList, newPointList.getFirstPoint(), newPointList.getLastPoint()); - // newLocation = this.child.getFigure().getBounds().getLocation(); - // final Collection toRefresh = refreshRunnable.getResult(); - // final Iterator iter = toRefresh.iterator(); - // while(iter.hasNext()) { - // final CompoundCommand cc = new CompoundCommand("Fix connections anchors"); //$NON-NLS-1$ - // final AbstractConnectionEditPart current = iter.next(); - // - // Connection currentConn = (Connection)current.getFigure(); - // PolylineConnectionEx dummyCOnnection = new PolylineConnectionEx(); - // final ConnectionRouter router = currentConn.getConnectionRouter(); - // IFigure parent = DiagramEditPartsUtil.getDiagramFeedbackLayer(current);//FIXME : probably not the good parent - // dummyCOnnection.setParent(parent); - // dummyCOnnection.setPoints(this.map.get(currentConn).initialPointList); - // final IFigure sourceOwner = currentConn.getSourceAnchor().getOwner(); - // final IFigure targetOwer = currentConn.getTargetAnchor().getOwner(); - // - // //assuming that we are moving target only! - // router.route(dummyCOnnection); - // - // - // - // - // - // addFixAnchorCommand(current, cc); - // if(cc.canExecute()) { - // cc.execute(); - // } else { - // Activator.log.warn("Command to fix the anchors is null"); //$NON-NLS-1$ - // } - // } + fixEdgePointCommand.add(setBendpointsCommand); + fixEdgePointCommand.execute(new NullProgressMonitor(), null); + } return CommandResult.newOKCommandResult(); } + /** + * + * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#getAffectedFiles() + * + * @return + */ + @Override + @SuppressWarnings("rawtypes") + public List getAffectedFiles() { + if(this.containerEP != null) { + View view = (View)this.containerEP.getModel(); + if(view != null) { + IFile f = WorkspaceSynchronizer.getFile(view.eResource()); + return f != null ? Collections.singletonList(f) : Collections.EMPTY_LIST; + } + } + return super.getAffectedFiles(); + } + /** * * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#cleanup() @@ -334,8 +203,16 @@ public class FixEdgeAnchorsDeferredCommand extends AbstractFixEdgeAnchorDeferred protected void cleanup() { super.cleanup(); this.connectionsEditPartToRefresh.clear(); + this.initialPointLists.clear(); + this.initialPointLists = null; + this.connectionsEditPartToRefresh = null; } + /** + * + * This runnable is used to refresh the figure source/target of the connection before updating their point list + * + */ private static class RefreshConnectionElementsRunnable extends AbstractRefreshConnectionElementsRunnable> { /** diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/XYLayoutWithConstrainedResizedEditPolicy.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/XYLayoutWithConstrainedResizedEditPolicy.java index b1c427191f8..cf74541de16 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/XYLayoutWithConstrainedResizedEditPolicy.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/XYLayoutWithConstrainedResizedEditPolicy.java @@ -202,7 +202,7 @@ public class XYLayoutWithConstrainedResizedEditPolicy extends XYLayoutEditPolicy final CompoundCommand cc = new CompoundCommand(); cc.add(cmd); //see bug 430702: [Diagram] Moving source of a link moves the target too. - cc.add(new ICommandProxy(new FixEdgeAnchorsDeferredCommand(getEditingDomain(), (IGraphicalEditPart)getHost(), connections, (INodeEditPart)child))); + cc.add(new ICommandProxy(new FixEdgeAnchorsDeferredCommand(getEditingDomain(), request, (IGraphicalEditPart)getHost(), connections))); return cc; } } diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/IdentityAnchorHelper.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/IdentityAnchorHelper.java index 23b106fd457..e9293992e69 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/IdentityAnchorHelper.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/helper/IdentityAnchorHelper.java @@ -31,7 +31,6 @@ public class IdentityAnchorHelper { /** * the char ending an id of {@link IdentityAnchor} */ - public static final char END_ID = ')'; /** diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/routers/RouterUtil.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/routers/RouterUtil.java new file mode 100644 index 00000000000..55bf16db76f --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/routers/RouterUtil.java @@ -0,0 +1,144 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST. + * + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.common.routers; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.draw2d.Connection; +import org.eclipse.draw2d.ConnectionAnchor; +import org.eclipse.draw2d.ConnectionRouter; +import org.eclipse.draw2d.IFigure; +import org.eclipse.draw2d.RelativeBendpoint; +import org.eclipse.draw2d.geometry.Dimension; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.PointList; +import org.eclipse.gmf.runtime.draw2d.ui.figures.IAnchorableFigure; +import org.eclipse.gmf.runtime.draw2d.ui.figures.PolylineConnectionEx; +import org.eclipse.papyrus.infra.gmfdiag.common.figure.edge.CustomRelativeBendpoint; + +/** + * + * A util class for routing connnection + * + */ +public class RouterUtil { + + private RouterUtil() { + //to prevent instanciation + } + + + /** + * This method creates a temporary connection (not visible on the diagram) from @param connection and route it with + * the same conditions + * + * + * @param connection + * an existing connection + * @param pointListBeforeMoving + * a copy of the point list of the moved connection + * @param isMovingSource + * true if we are moving the source of the connection and false if we are moving the target of the connection + * @return + * the final PointList of the connection + */ + public static PointList routeDummyConnection(final Connection connection, final PointList pointListBeforeMoving, final boolean isMovingSource) { + //TODO : is it useful to create a dummy connection? + + //2. we create a dummy connection from current connection, with new informations to determine real anchor point + final PointList initialPointList = pointListBeforeMoving.getCopy(); + final Point first = initialPointList.getFirstPoint(); + final Point last = initialPointList.getLastPoint(); + int size = initialPointList.size(); + + final List relatedBendpoints = new ArrayList(); + final Connection dummyConnection = new PolylineConnectionEx(); + for(int i = 0; i < initialPointList.size(); i++) { + final Point current = initialPointList.getPoint(i); + final Dimension s = current.getDifference(first); + final Dimension t = current.getDifference(last); + final CustomRelativeBendpoint relatedBendpoint = new CustomRelativeBendpoint(connection);//connection and not dummyConnection! it is OK! + relatedBendpoint.setRelativeDimensions(s, t); + relatedBendpoints.add(relatedBendpoint); + if(isMovingSource) { + if(i > 1) { + //all points must follow the target -> they must not move + relatedBendpoint.setWeight(CustomRelativeBendpoint.FOLLOW_TARGET); + } else { + if(i == 0) { + //the first point (the source point) must move! + relatedBendpoint.setWeight(CustomRelativeBendpoint.FOLLOW_SOURCE); + } else if(current.y == first.y) {//the second point + //if the segment is horizontal, its x follow the source and its y follow the target point + relatedBendpoint.setWeight(CustomRelativeBendpoint.FOLLOW_TARGET, CustomRelativeBendpoint.FOLLOW_SOURCE); + } else if(current.x == first.x) { + //if the segment is vertical, its x follow the target and its y follow the source point + relatedBendpoint.setWeight(CustomRelativeBendpoint.FOLLOW_SOURCE, CustomRelativeBendpoint.FOLLOW_TARGET); + } + } + } else { + if(i < size - 2) { + //all points must follow the source -> they must not move + relatedBendpoint.setWeight(CustomRelativeBendpoint.FOLLOW_SOURCE); + } else { + if(i == size - 1) { + //the last point (the target point) must move! + relatedBendpoint.setWeight(CustomRelativeBendpoint.FOLLOW_TARGET); + } else if(current.y == last.y) {//the point before the last point + //if the segment is horizontal, its x follow the source and its y follow the target point + relatedBendpoint.setWeight(CustomRelativeBendpoint.FOLLOW_SOURCE, CustomRelativeBendpoint.FOLLOW_TARGET); + } else if(current.x == last.x) { + //if the segment is vertical, its x follow the target and its y follow the source point + relatedBendpoint.setWeight(CustomRelativeBendpoint.FOLLOW_TARGET, CustomRelativeBendpoint.FOLLOW_SOURCE); + } + } + } + } + final IFigure sourceFigure = connection.getSourceAnchor().getOwner(); + final IFigure targetFigure = connection.getTargetAnchor().getOwner(); + + final ConnectionAnchor sourceAnchor; + final ConnectionAnchor targetAnchor; + if(isMovingSource) { + sourceAnchor = ((IAnchorableFigure)sourceFigure).getSourceConnectionAnchorAt(connection.getSourceAnchor().getReferencePoint()); + targetAnchor = ((IAnchorableFigure)targetFigure).getTargetConnectionAnchorAt(connection.getTargetAnchor().getReferencePoint()); + } else { + sourceAnchor = ((IAnchorableFigure)sourceFigure).getSourceConnectionAnchorAt(connection.getSourceAnchor().getReferencePoint()); + targetAnchor = ((IAnchorableFigure)targetFigure).getTargetConnectionAnchorAt(connection.getTargetAnchor().getReferencePoint()); + + } + dummyConnection.setSourceAnchor(sourceAnchor); + dummyConnection.setTargetAnchor(targetAnchor); + + final IFigure parent = connection.getParent(); + final IFigure layer = parent; + dummyConnection.setParent(layer); + dummyConnection.setPoints(initialPointList.getCopy());//required? + + + //configure the router for the dummy connection + final ConnectionRouter router = connection.getConnectionRouter(); + router.setConstraint(dummyConnection, relatedBendpoints); + dummyConnection.setConnectionRouter(router); + router.route(dummyConnection); + + final PointList res = dummyConnection.getPoints(); + dummyConnection.setRoutingConstraint(null); + dummyConnection.setConnectionRouter(null); + dummyConnection.setParent(null); + return res; + } + +} -- cgit v1.2.3