1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.synchronization.graph;
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Point2D;
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;
26 * @author Tuukka Lehtonen
28 public class TranslateElement extends ModificationAdapter {
31 Point2D absolutePosition;
34 public static TranslateElement absolute(Resource element, double x, double y) {
35 return new TranslateElement(element, new Point2D.Double(x, y), null);
38 public static TranslateElement absolute(Resource element, Point2D absolutePosition) {
39 return new TranslateElement(element, absolutePosition, null);
42 public static TranslateElement offset(Resource element, double x, double y) {
43 return new TranslateElement(element, null, new Point2D.Double(x, y));
46 public static TranslateElement offset(Resource element, Point2D offset) {
47 return new TranslateElement(element, null, offset);
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
59 public TranslateElement(IElement element, Point2D absolutePosition, Point2D offset) {
60 this((Resource) element.getHint(ElementHints.KEY_OBJECT), absolutePosition, offset);
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
71 public TranslateElement(Resource element, Point2D absolutePosition, Point2D offset) {
73 this.element = element;
74 this.absolutePosition = absolutePosition;
79 public void perform(WriteGraph graph) throws DatabaseException {
80 if (absolutePosition == null && offset == null)
83 AffineTransform at = DiagramGraphUtil.getTransform(graph, element);
85 double x = at.getTranslateX();
86 double y = at.getTranslateY();
87 if (absolutePosition != null) {
88 x = absolutePosition.getX();
89 y = absolutePosition.getY();
96 boolean changed = !(x == at.getTranslateX() && y == at.getTranslateY());
100 at.setTransform(at.getScaleX(), at.getShearX(), at.getShearY(), at.getScaleY(), x, y);
102 DiagramGraphUtil.setTransform(graph, element, at);
103 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
104 graph.addMetadata(cm.add("Translated " + element));