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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.synchronization.graph;
\r
14 import java.awt.geom.AffineTransform;
\r
15 import java.awt.geom.Point2D;
\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
26 * @author Tuukka Lehtonen
\r
28 public class TranslateElement extends ModificationAdapter {
\r
31 Point2D absolutePosition;
\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
38 public static TranslateElement absolute(Resource element, Point2D absolutePosition) {
\r
39 return new TranslateElement(element, absolutePosition, null);
\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
46 public static TranslateElement offset(Resource element, Point2D offset) {
\r
47 return new TranslateElement(element, null, offset);
\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
59 public TranslateElement(IElement element, Point2D absolutePosition, Point2D offset) {
\r
60 this((Resource) element.getHint(ElementHints.KEY_OBJECT), absolutePosition, offset);
\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
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
79 public void perform(WriteGraph graph) throws DatabaseException {
\r
80 if (absolutePosition == null && offset == null)
\r
83 AffineTransform at = DiagramGraphUtil.getTransform(graph, element);
\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
91 if (offset != null) {
\r
96 boolean changed = !(x == at.getTranslateX() && y == at.getTranslateY());
\r
100 at.setTransform(at.getScaleX(), at.getShearX(), at.getShearY(), at.getScaleY(), x, y);
\r
102 DiagramGraphUtil.setTransform(graph, element, at);
\r
103 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
104 graph.addMetadata(cm.add("Translated " + element));
\r