1 package org.simantics.district.network.ui.participants;
\r
3 import java.awt.geom.AffineTransform;
\r
4 import java.awt.geom.Point2D;
\r
5 import java.util.ArrayList;
\r
6 import java.util.Collection;
\r
8 import org.simantics.Simantics;
\r
9 import org.simantics.db.Resource;
\r
10 import org.simantics.db.WriteGraph;
\r
11 import org.simantics.db.common.CommentMetadata;
\r
12 import org.simantics.db.common.request.WriteRequest;
\r
13 import org.simantics.db.exception.DatabaseException;
\r
14 import org.simantics.diagram.elements.ElementTransforms;
\r
15 import org.simantics.diagram.elements.ElementTransforms.TransformedObject;
\r
16 import org.simantics.diagram.stubs.DiagramResource;
\r
17 import org.simantics.district.network.ontology.DistrictNetworkResource;
\r
18 import org.simantics.g2d.canvas.Hints;
\r
19 import org.simantics.g2d.diagram.participant.pointertool.TranslateMode;
\r
20 import org.simantics.g2d.element.ElementUtils;
\r
21 import org.simantics.g2d.element.IElement;
\r
22 import org.simantics.g2d.element.handler.Move;
\r
24 public class DNTranslateMode extends TranslateMode {
\r
26 public DNTranslateMode(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {
\r
27 super(startingPoint, currentPoint, mouseId, elements);
\r
31 protected boolean commit() {
\r
32 for (IElement el : elementsToReallyTranslate) {
\r
33 Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);
\r
35 Point2D oldPos = move.getPosition(el);
\r
36 move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);
\r
40 Simantics.getSession().syncRequest(new WriteRequest() {
\r
42 public void perform(WriteGraph graph) throws DatabaseException {
\r
43 Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();
\r
44 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
\r
45 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
46 for (IElement e : elementsToReallyTranslate) {
\r
47 Object obj = ElementUtils.getObject(e);
\r
48 if (obj instanceof Resource) {
\r
49 Resource res = (Resource) obj;
\r
50 if (graph.isInstanceOf(res, DN.Vertex)) {
\r
51 AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());
\r
52 transformed.add( new TransformedObject((Resource) obj, at) );
\r
56 double[] coords = new double[] { at.getTranslateX(), at.getTranslateY() };
\r
57 graph.claimLiteral(res, DIA.HasLocation, coords);
\r
62 if (!transformed.isEmpty()) {
\r
63 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
64 graph.addMetadata(cm.add("Translated " + transformed.size() + " "
\r
65 + (transformed.size() == 1 ? "element" : "elements")
\r
66 + " by (" + dx + "," + dy + ") mm."));
\r
67 graph.markUndoPoint();
\r
71 } catch (DatabaseException e) {
\r
72 e.printStackTrace();
\r
75 for (IElement dirty : elementsToDirty)
\r
76 dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
\r