]> gerrit.simantics Code Review - simantics/district.git/blob
8603add13e995f670265ec0845abfe2476210e84
[simantics/district.git] /
1 package org.simantics.district.network.ui.participants;
2
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.awt.geom.Rectangle2D;
6 import java.util.ArrayList;
7 import java.util.Collection;
8
9 import org.simantics.Simantics;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.CommentMetadata;
13 import org.simantics.db.common.request.WriteRequest;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.diagram.elements.ElementTransforms;
16 import org.simantics.diagram.elements.ElementTransforms.TransformedObject;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.district.network.ModelledCRS;
19 import org.simantics.district.network.ontology.DistrictNetworkResource;
20 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
21 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
22 import org.simantics.g2d.canvas.Hints;
23 import org.simantics.g2d.diagram.participant.pointertool.TranslateMode;
24 import org.simantics.g2d.element.ElementUtils;
25 import org.simantics.g2d.element.IElement;
26 import org.simantics.g2d.element.handler.Move;
27
28 public class DNTranslateMode extends TranslateMode {
29
30     public DNTranslateMode(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {
31         super(startingPoint, currentPoint, mouseId, elements);
32     }
33     
34     @Override
35     protected boolean commit() {
36         for (IElement el : elementsToReallyTranslate) {
37             Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);
38             if (move != null) {
39                 Point2D oldPos = move.getPosition(el);
40                 move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);
41             }
42         }
43         try {
44             Simantics.getSession().syncRequest(new WriteRequest() {
45                 @Override
46                 public void perform(WriteGraph graph) throws DatabaseException {
47                     Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();
48                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
49                     DiagramResource DIA = DiagramResource.getInstance(graph);
50                     for (IElement e : elementsToReallyTranslate) {
51                         
52                         DistrictNetworkVertexNode node = e.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
53                         Rectangle2D bounds = node.getBounds();
54                         
55                         Object obj = ElementUtils.getObject(e);
56                         if (obj instanceof Resource) {
57                             Resource res = (Resource) obj;
58                             AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());
59                             if (graph.isInstanceOf(res, DN.Vertex)) {
60                                 
61                                 double x = at.getTranslateX();
62                                 double y = at.getTranslateY();
63
64                                 double lat = ModelledCRS.yToLatitude(y / at.getScaleY());
65                                 double lon = ModelledCRS.xToLongitude(x / at.getScaleX());
66                                 
67                                 // write to db
68                                 double[] coords = new double[] { lon, lat };
69                                 graph.claimLiteral(res, DIA.HasLocation, coords);
70                             } else {
71                                 // fallback to normal HasTransforms
72                                 transformed.add( new TransformedObject((Resource) obj, at) );
73                             }
74                         }
75                     }
76                     if (!transformed.isEmpty()) {
77                         graph.markUndoPoint();
78                         // Normal transforms
79                         ElementTransforms.setTransformRequest(transformed).perform(graph);
80                         
81                         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
82                         graph.addMetadata(cm.add("Translated " + transformed.size() + " "
83                                 + (transformed.size() == 1 ? "element" : "elements")
84                                 + " by (" + dx + "," + dy + ") mm."));
85                     }
86                 }
87             });
88         } catch (DatabaseException e) {
89             e.printStackTrace();
90         }
91         
92         for (IElement dirty : elementsToDirty)
93             dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
94
95         setDirty();
96         remove();
97         return false;
98     }
99
100 }