]> gerrit.simantics Code Review - simantics/district.git/blob
fda0fc717d3dce0d1511c3dceb414294a2f293ad
[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.util.ArrayList;
6 import java.util.Collection;
7
8 import javax.xml.bind.DataBindingException;
9
10 import org.simantics.Simantics;
11 import org.simantics.db.Resource;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.common.CommentMetadata;
14 import org.simantics.db.common.request.WriteRequest;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.diagram.elements.ElementTransforms;
17 import org.simantics.diagram.elements.ElementTransforms.TransformedObject;
18 import org.simantics.diagram.stubs.DiagramResource;
19 import org.simantics.district.network.ModelledCRS;
20 import org.simantics.district.network.ontology.DistrictNetworkResource;
21 import org.simantics.g2d.canvas.Hints;
22 import org.simantics.g2d.diagram.participant.pointertool.TranslateMode;
23 import org.simantics.g2d.element.ElementUtils;
24 import org.simantics.g2d.element.IElement;
25
26 public class DNTranslateMode extends TranslateMode {
27
28     public DNTranslateMode(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {
29         super(startingPoint, currentPoint, mouseId, elements);
30     }
31     
32     @Override
33     protected boolean commit() {
34         try {
35             Simantics.getSession().syncRequest(new WriteRequest() {
36                 @Override
37                 public void perform(WriteGraph graph) throws DatabaseException {
38                     Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();
39                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
40                     DiagramResource DIA = DiagramResource.getInstance(graph);
41                     for (IElement e : elementsToReallyTranslate) {
42                         
43                         
44                         Object obj = ElementUtils.getObject(e);
45                         if (obj instanceof Resource && graph.isInstanceOf((Resource) obj, DN.Vertex)) {
46                             
47                             Resource res = (Resource) obj;
48                             
49                             // Read current location from graph
50                             double[] currentCoords = graph.getRelatedValue2(res, DIA.HasLocation);
51                             
52                             double x = ModelledCRS.longitudeToX(currentCoords[0]);
53                             double y = ModelledCRS.latitudeToY(currentCoords[1]);
54                                     
55                             AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());
56                             if (graph.isInstanceOf(res, DN.Vertex)) {
57                                 
58                                 double lat = ModelledCRS.yToLatitude(y + (dy / at.getScaleY()));
59                                 double lon = ModelledCRS.xToLongitude(x + (dx / at.getScaleX()));
60                                 
61                                 // write to db
62                                 double[] coords = new double[] { lon, lat };
63                                 graph.claimLiteral(res, DIA.HasLocation, coords);
64                             } else {
65                                 // fallback to normal HasTransforms
66                                 transformed.add( new TransformedObject((Resource) obj, at) );
67                             }
68                         }
69                     }
70                     if (!transformed.isEmpty()) {
71                         graph.markUndoPoint();
72                         // Normal transforms
73                         ElementTransforms.setTransformRequest(transformed).perform(graph);
74                         
75                         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
76                         graph.addMetadata(cm.add("Translated " + transformed.size() + " "
77                                 + (transformed.size() == 1 ? "element" : "elements")
78                                 + " by (" + dx + "," + dy + ") mm."));
79                     }
80                 }
81             });
82         } catch (DatabaseException e) {
83             e.printStackTrace();
84         }
85         
86         for (IElement dirty : elementsToDirty)
87             dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
88
89         setDirty();
90         remove();
91         return false;
92     }
93
94 }