--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.participant;\r
+\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.CommentMetadata;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.content.ConnectionUtil;\r
+import org.simantics.diagram.elements.ElementTransforms;\r
+import org.simantics.diagram.elements.ElementTransforms.TransformedObject;\r
+import org.simantics.diagram.flag.IOTableUtil;\r
+import org.simantics.diagram.flag.IOTablesInfo;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.ui.DiagramModelHints;\r
+import org.simantics.g2d.canvas.Hints;\r
+import org.simantics.g2d.diagram.participant.pointertool.TranslateMode;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.Move;\r
+import org.simantics.scenegraph.g2d.IG2DNode;\r
+import org.simantics.scenegraph.g2d.nodes.Decoration;\r
+import org.simantics.scenegraph.utils.NodeUtil;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * This participant handles the diagram in translate elements mode with support\r
+ * for route graph connections.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class TranslateMode2 extends TranslateMode {\r
+\r
+ public TranslateMode2(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {\r
+ super(startingPoint, currentPoint, mouseId, elements);\r
+ }\r
+\r
+ @Override\r
+ protected boolean commit() {\r
+ for (IElement el : elementsToReallyTranslate) {\r
+ Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);\r
+ if (move != null) {\r
+ Point2D oldPos = move.getPosition(el);\r
+ move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);\r
+ }\r
+ }\r
+ \r
+ try {\r
+ Simantics.getSession().syncRequest(new WriteRequest() {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();\r
+ for (IElement e : elementsToReallyTranslate) {\r
+ Object obj = ElementUtils.getObject(e);\r
+ if (obj instanceof Resource) {\r
+ Resource r = (Resource) obj;\r
+ if (graph.isInstanceOf(r, DIA.RouteGraphConnection))\r
+ continue;\r
+ if (graph.isInstanceOf(r, DIA.Flag)) {\r
+ if(handleFlag(graph, DIA, e, r))\r
+ continue;\r
+ }\r
+ AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());\r
+ transformed.add( new TransformedObject((Resource) obj, at) );\r
+ }\r
+ }\r
+\r
+ if (!transformed.isEmpty()) {\r
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
+ graph.addMetadata(cm.add("Translated " + transformed.size() + " "\r
+ + (transformed.size() == 1 ? "element" : "elements")\r
+ + " by (" + dx + "," + dy + ") mm."));\r
+ graph.markUndoPoint();\r
+ }\r
+\r
+ // Normal transforms\r
+ ElementTransforms.setTransformRequest(transformed).perform(graph);\r
+\r
+ // Custom handling for moving interior route nodes\r
+ ConnectionUtil cu = new ConnectionUtil(graph);\r
+ for (IElement c : translatedConnections) {\r
+ Object obj = (Object) ElementUtils.getObject(c);\r
+ if (obj instanceof Resource) {\r
+ Resource connection = (Resource) obj;\r
+ if (graph.isInstanceOf(connection, DIA.RouteGraphConnection)) {\r
+ cu.translateRouteNodes(connection, dx, dy);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ });\r
+ } catch (DatabaseException err) {\r
+ ErrorLogger.defaultLogError(err);\r
+ }\r
+\r
+ for (IElement dirty : elementsToDirty)\r
+ dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);\r
+\r
+ setDirty();\r
+ remove();\r
+ return false;\r
+ }\r
+ \r
+ IOTablesInfo ioTablesInfo = null;\r
+ \r
+ private boolean handleFlag(WriteGraph graph, DiagramResource DIA,\r
+ IElement flagElement, Resource flagResource) throws DatabaseException {\r
+ if(ioTablesInfo == null)\r
+ ioTablesInfo = IOTableUtil.getIOTablesInfo(graph, \r
+ (Resource)diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE));\r
+ \r
+ // Use the center of the flag for determining the table binding\r
+ Object sgNode = flagElement.getHint(ElementHints.KEY_SG_NODE);\r
+ if(!(sgNode instanceof IG2DNode))\r
+ return false;\r
+ Rectangle2D flagBounds = NodeUtil.getLocalBounds((IG2DNode)sgNode, Decoration.class);\r
+ Point2D translateVector = getTranslateVector();\r
+ double flagX = flagBounds.getCenterX()+translateVector.getX();\r
+ double flagY = flagBounds.getCenterY()+translateVector.getY();\r
+ return ioTablesInfo.updateBinding(graph, DIA, flagResource, flagX, flagY);\r
+ }\r
+\r
+}\r