]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/TranslateElement.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / TranslateElement.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.synchronization.graph;\r
13 \r
14 import java.awt.geom.AffineTransform;\r
15 import java.awt.geom.Point2D;\r
16 \r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.common.CommentMetadata;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.diagram.synchronization.ModificationAdapter;\r
22 import org.simantics.g2d.element.ElementHints;\r
23 import org.simantics.g2d.element.IElement;\r
24 \r
25 /**\r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public class TranslateElement extends ModificationAdapter {\r
29 \r
30     Resource element;\r
31     Point2D  absolutePosition;\r
32     Point2D  offset;\r
33 \r
34     public static TranslateElement absolute(Resource element, double x, double y) {\r
35         return new TranslateElement(element, new Point2D.Double(x, y), null);\r
36     }\r
37 \r
38     public static TranslateElement absolute(Resource element, Point2D absolutePosition) {\r
39         return new TranslateElement(element, absolutePosition, null);\r
40     }\r
41 \r
42     public static TranslateElement offset(Resource element, double x, double y) {\r
43         return new TranslateElement(element, null, new Point2D.Double(x, y));\r
44     }\r
45 \r
46     public static TranslateElement offset(Resource element, Point2D offset) {\r
47         return new TranslateElement(element, null, offset);\r
48     }\r
49 \r
50     /**\r
51      * @param element the element to translate, must contain a {@link Resource}\r
52      *        type {@link ElementHints#KEY_OBJECT} hint\r
53      * @param absolutePosition <code>null</code> to not define an absolute\r
54      *        position to set, otherwise this position will override the\r
55      *        original translation of the element\r
56      * @param offset an offset to apply to the element's position. If\r
57      *        {@link #absolutePosition} is null, only an offset will be applied\r
58      */\r
59     public TranslateElement(IElement element, Point2D absolutePosition, Point2D offset) {\r
60         this((Resource) element.getHint(ElementHints.KEY_OBJECT), absolutePosition, offset);\r
61     }\r
62 \r
63     /**\r
64      * @param element the element to translate\r
65      * @param absolutePosition <code>null</code> to not define an absolute\r
66      *        position to set, otherwise this position will override the\r
67      *        original translation of the element\r
68      * @param offset an offset to apply to the element's position. If\r
69      *        {@link #absolutePosition} is null, only an offset will be applied\r
70      */\r
71     public TranslateElement(Resource element, Point2D absolutePosition, Point2D offset) {\r
72         super(LOW_PRIORITY);\r
73         this.element = element;\r
74         this.absolutePosition = absolutePosition;\r
75         this.offset = offset;\r
76     }\r
77 \r
78     @Override\r
79     public void perform(WriteGraph graph) throws DatabaseException {\r
80         if (absolutePosition == null && offset == null)\r
81             return;\r
82 \r
83         AffineTransform at = DiagramGraphUtil.getTransform(graph, element);\r
84 \r
85         double x = at.getTranslateX();\r
86         double y = at.getTranslateY();\r
87         if (absolutePosition != null) {\r
88             x = absolutePosition.getX();\r
89             y = absolutePosition.getY();\r
90         }\r
91         if (offset != null) {\r
92             x += offset.getX();\r
93             y += offset.getY();\r
94         }\r
95 \r
96         boolean changed = !(x == at.getTranslateX() && y == at.getTranslateY());\r
97         if (!changed)\r
98             return;\r
99 \r
100         at.setTransform(at.getScaleX(), at.getShearX(), at.getShearY(), at.getScaleY(), x, y);\r
101 \r
102         DiagramGraphUtil.setTransform(graph, element, at);\r
103         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
104         graph.addMetadata(cm.add("Translated " + element));\r
105     }\r
106 \r
107 }\r