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