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