]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/participant/TranslateMode2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / participant / TranslateMode2.java
index ec579422a1f719a8bd2dc6737305f0970e15e38d..ca937a8d81747736966f2e3e5beb30c8c37c7572 100644 (file)
-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * 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:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.participant;
+
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.diagram.content.ConnectionUtil;
+import org.simantics.diagram.elements.ElementTransforms;
+import org.simantics.diagram.elements.ElementTransforms.TransformedObject;
+import org.simantics.diagram.flag.IOTableUtil;
+import org.simantics.diagram.flag.IOTablesInfo;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.g2d.canvas.Hints;
+import org.simantics.g2d.diagram.participant.pointertool.TranslateMode;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.handler.Move;
+import org.simantics.scenegraph.g2d.IG2DNode;
+import org.simantics.scenegraph.g2d.nodes.Decoration;
+import org.simantics.scenegraph.utils.NodeUtil;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * This participant handles the diagram in translate elements mode with support
+ * for route graph connections.
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class TranslateMode2 extends TranslateMode {
+
+    public TranslateMode2(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {
+        super(startingPoint, currentPoint, mouseId, elements);
+    }
+
+    @Override
+    protected boolean commit() {
+        for (IElement el : elementsToReallyTranslate) {
+            Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);
+            if (move != null) {
+                Point2D oldPos = move.getPosition(el);
+                move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);
+            }
+        }
+        try {
+            Simantics.getSession().syncRequest(new WriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    DiagramResource DIA = DiagramResource.getInstance(graph);
+
+                    Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();
+                    for (IElement e : elementsToReallyTranslate) {
+                        Object obj = ElementUtils.getObject(e);
+                        if (obj instanceof Resource) {
+                            Resource r = (Resource) obj;
+                            if (graph.isInstanceOf(r, DIA.RouteGraphConnection))
+                                continue;
+                            if (graph.isInstanceOf(r, DIA.Flag)) {
+                                if(handleFlag(graph, DIA, e, r))
+                                    continue;
+                            }
+                            AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());
+                            transformed.add( new TransformedObject((Resource) obj, at) );
+                        }
+                    }
+
+                    if (!transformed.isEmpty()) {
+                        CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
+                        graph.addMetadata(cm.add("Translated " + transformed.size() + " "
+                                + (transformed.size() == 1 ? "element" : "elements")
+                                + " by (" + dx + "," + dy + ") mm."));
+                        graph.markUndoPoint();
+                    }
+
+                    // Normal transforms
+                    ElementTransforms.setTransformRequest(transformed).perform(graph);
+
+                    // Custom handling for moving interior route nodes
+                    ConnectionUtil cu = new ConnectionUtil(graph);
+                    for (IElement c : translatedConnections) {
+                        Object obj = (Object) ElementUtils.getObject(c);
+                        if (obj instanceof Resource) {
+                            Resource connection = (Resource) obj;
+                            if (graph.isInstanceOf(connection, DIA.RouteGraphConnection)) {
+                                cu.translateRouteNodes(connection, dx, dy);
+                            }
+                        }
+                    }
+                }
+            });
+        } catch (DatabaseException err) {
+            ErrorLogger.defaultLogError(err);
+        }
+
+        for (IElement dirty : elementsToDirty)
+            dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
+
+        setDirty();
+        remove();
+        return false;
+    }
+    
+    IOTablesInfo ioTablesInfo = null;
+    
+    private boolean handleFlag(WriteGraph graph, DiagramResource DIA,
+            IElement flagElement, Resource flagResource) throws DatabaseException {
+        if(ioTablesInfo == null)
+            ioTablesInfo = IOTableUtil.getIOTablesInfo(graph, 
+                    (Resource)diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE));
+        
+        // Use the center of the flag for determining the table binding
+        Object sgNode = flagElement.getHint(ElementHints.KEY_SG_NODE);
+        if(!(sgNode instanceof IG2DNode))
+            return false;
+        Rectangle2D flagBounds = NodeUtil.getLocalBounds((IG2DNode)sgNode, Decoration.class);
+        Point2D translateVector = getTranslateVector();
+        double flagX = flagBounds.getCenterX()+translateVector.getX();
+        double flagY = flagBounds.getCenterY()+translateVector.getY();
+        return ioTablesInfo.updateBinding(graph, DIA, flagResource, flagX, flagY);
+    }
+
+}