]> gerrit.simantics Code Review - simantics/district.git/blob
74a464758590f7c91288ed8df749eae454cedc18
[simantics/district.git] /
1 package org.simantics.district.network.ui.participants;\r
2 \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
7 \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
23 \r
24 public class DNTranslateMode extends TranslateMode {\r
25 \r
26     public DNTranslateMode(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {\r
27         super(startingPoint, currentPoint, mouseId, elements);\r
28     }\r
29     \r
30     @Override\r
31     protected boolean commit() {\r
32         for (IElement el : elementsToReallyTranslate) {\r
33             Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);\r
34             if (move != null) {\r
35                 Point2D oldPos = move.getPosition(el);\r
36                 move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);\r
37             }\r
38         }\r
39         try {\r
40             Simantics.getSession().syncRequest(new WriteRequest() {\r
41                 @Override\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
53                                 \r
54                                 \r
55                                 // write to db\r
56                                 double[] coords = new double[] { at.getTranslateX(), at.getTranslateY() };\r
57                                 graph.claimLiteral(res, DIA.HasLocation, coords);\r
58                             }\r
59                         }\r
60                     }\r
61                     \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
68                     }\r
69                 }\r
70             });\r
71         } catch (DatabaseException e) {\r
72             e.printStackTrace();\r
73         }\r
74         \r
75         for (IElement dirty : elementsToDirty)\r
76             dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);\r
77 \r
78         setDirty();\r
79         remove();\r
80         return false;\r
81     }\r
82 \r
83 }\r