X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fsynchronization%2Fgraph%2FTranslateElement.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fsynchronization%2Fgraph%2FTranslateElement.java;h=39401ef283963d48ab7c9b63e9405120c7efb811;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/TranslateElement.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/TranslateElement.java new file mode 100644 index 000000000..39401ef28 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/TranslateElement.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.synchronization.graph; + +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.CommentMetadata; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.synchronization.ModificationAdapter; +import org.simantics.g2d.element.ElementHints; +import org.simantics.g2d.element.IElement; + +/** + * @author Tuukka Lehtonen + */ +public class TranslateElement extends ModificationAdapter { + + Resource element; + Point2D absolutePosition; + Point2D offset; + + public static TranslateElement absolute(Resource element, double x, double y) { + return new TranslateElement(element, new Point2D.Double(x, y), null); + } + + public static TranslateElement absolute(Resource element, Point2D absolutePosition) { + return new TranslateElement(element, absolutePosition, null); + } + + public static TranslateElement offset(Resource element, double x, double y) { + return new TranslateElement(element, null, new Point2D.Double(x, y)); + } + + public static TranslateElement offset(Resource element, Point2D offset) { + return new TranslateElement(element, null, offset); + } + + /** + * @param element the element to translate, must contain a {@link Resource} + * type {@link ElementHints#KEY_OBJECT} hint + * @param absolutePosition null to not define an absolute + * position to set, otherwise this position will override the + * original translation of the element + * @param offset an offset to apply to the element's position. If + * {@link #absolutePosition} is null, only an offset will be applied + */ + public TranslateElement(IElement element, Point2D absolutePosition, Point2D offset) { + this((Resource) element.getHint(ElementHints.KEY_OBJECT), absolutePosition, offset); + } + + /** + * @param element the element to translate + * @param absolutePosition null to not define an absolute + * position to set, otherwise this position will override the + * original translation of the element + * @param offset an offset to apply to the element's position. If + * {@link #absolutePosition} is null, only an offset will be applied + */ + public TranslateElement(Resource element, Point2D absolutePosition, Point2D offset) { + super(LOW_PRIORITY); + this.element = element; + this.absolutePosition = absolutePosition; + this.offset = offset; + } + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + if (absolutePosition == null && offset == null) + return; + + AffineTransform at = DiagramGraphUtil.getTransform(graph, element); + + double x = at.getTranslateX(); + double y = at.getTranslateY(); + if (absolutePosition != null) { + x = absolutePosition.getX(); + y = absolutePosition.getY(); + } + if (offset != null) { + x += offset.getX(); + y += offset.getY(); + } + + boolean changed = !(x == at.getTranslateX() && y == at.getTranslateY()); + if (!changed) + return; + + at.setTransform(at.getScaleX(), at.getShearX(), at.getShearY(), at.getScaleY(), x, y); + + DiagramGraphUtil.setTransform(graph, element, at); + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); + graph.addMetadata(cm.add("Translated " + element)); + } + +}