--- /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.synchronization.graph;\r
+\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Point2D;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.CommentMetadata;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.synchronization.ModificationAdapter;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class TranslateElement extends ModificationAdapter {\r
+\r
+ Resource element;\r
+ Point2D absolutePosition;\r
+ Point2D offset;\r
+\r
+ public static TranslateElement absolute(Resource element, double x, double y) {\r
+ return new TranslateElement(element, new Point2D.Double(x, y), null);\r
+ }\r
+\r
+ public static TranslateElement absolute(Resource element, Point2D absolutePosition) {\r
+ return new TranslateElement(element, absolutePosition, null);\r
+ }\r
+\r
+ public static TranslateElement offset(Resource element, double x, double y) {\r
+ return new TranslateElement(element, null, new Point2D.Double(x, y));\r
+ }\r
+\r
+ public static TranslateElement offset(Resource element, Point2D offset) {\r
+ return new TranslateElement(element, null, offset);\r
+ }\r
+\r
+ /**\r
+ * @param element the element to translate, must contain a {@link Resource}\r
+ * type {@link ElementHints#KEY_OBJECT} hint\r
+ * @param absolutePosition <code>null</code> to not define an absolute\r
+ * position to set, otherwise this position will override the\r
+ * original translation of the element\r
+ * @param offset an offset to apply to the element's position. If\r
+ * {@link #absolutePosition} is null, only an offset will be applied\r
+ */\r
+ public TranslateElement(IElement element, Point2D absolutePosition, Point2D offset) {\r
+ this((Resource) element.getHint(ElementHints.KEY_OBJECT), absolutePosition, offset);\r
+ }\r
+\r
+ /**\r
+ * @param element the element to translate\r
+ * @param absolutePosition <code>null</code> to not define an absolute\r
+ * position to set, otherwise this position will override the\r
+ * original translation of the element\r
+ * @param offset an offset to apply to the element's position. If\r
+ * {@link #absolutePosition} is null, only an offset will be applied\r
+ */\r
+ public TranslateElement(Resource element, Point2D absolutePosition, Point2D offset) {\r
+ super(LOW_PRIORITY);\r
+ this.element = element;\r
+ this.absolutePosition = absolutePosition;\r
+ this.offset = offset;\r
+ }\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ if (absolutePosition == null && offset == null)\r
+ return;\r
+\r
+ AffineTransform at = DiagramGraphUtil.getTransform(graph, element);\r
+\r
+ double x = at.getTranslateX();\r
+ double y = at.getTranslateY();\r
+ if (absolutePosition != null) {\r
+ x = absolutePosition.getX();\r
+ y = absolutePosition.getY();\r
+ }\r
+ if (offset != null) {\r
+ x += offset.getX();\r
+ y += offset.getY();\r
+ }\r
+\r
+ boolean changed = !(x == at.getTranslateX() && y == at.getTranslateY());\r
+ if (!changed)\r
+ return;\r
+\r
+ at.setTransform(at.getScaleX(), at.getShearX(), at.getShearY(), at.getScaleY(), x, y);\r
+\r
+ DiagramGraphUtil.setTransform(graph, element, at);\r
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
+ graph.addMetadata(cm.add("Translated " + element));\r
+ }\r
+\r
+}\r