]> gerrit.simantics Code Review - simantics/district.git/blob
7bec07dc138507ae14f4b344081e8d5b5c9c4fcf
[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 import org.simantics.g2d.element.handler.Move;
24
25 public class DNTranslateMode extends TranslateMode {
26
27     public DNTranslateMode(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {
28         super(startingPoint, currentPoint, mouseId, elements);
29     }
30     
31     @Override
32     protected boolean commit() {
33         for (IElement el : elementsToReallyTranslate) {
34             Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);
35             if (move != null) {
36                 Point2D oldPos = move.getPosition(el);
37                 move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);
38             }
39         }
40         try {
41             Simantics.getSession().syncRequest(new WriteRequest() {
42                 @Override
43                 public void perform(WriteGraph graph) throws DatabaseException {
44                     Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();
45                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
46                     DiagramResource DIA = DiagramResource.getInstance(graph);
47                     for (IElement e : elementsToReallyTranslate) {
48                         Object obj = ElementUtils.getObject(e);
49                         if (obj instanceof Resource) {
50                             Resource res = (Resource) obj;
51                             AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());
52                             if (graph.isInstanceOf(res, DN.Vertex)) {
53                                 
54                                 double x = at.getTranslateX();
55                                 double y = at.getTranslateY();
56                                 // TODO: Fix scale!
57                                 double scale = 100000;
58 //                                double scale = 1;
59                                 double lat = ModelledCRS.yToLatitude(y / scale);
60                                 double lon = ModelledCRS.xToLongitude(x / scale);
61                                 
62                                 // write to db
63                                 double[] coords = new double[] { lon, lat };
64                                 graph.claimLiteral(res, DIA.HasLocation, coords);
65                             } else {
66                                 // fallback to normal HasTransforms
67                                 transformed.add( new TransformedObject((Resource) obj, at) );
68                             }
69                         }
70                     }
71                     if (!transformed.isEmpty()) {
72                         graph.markUndoPoint();
73                         // Normal transforms
74                         ElementTransforms.setTransformRequest(transformed).perform(graph);
75                         
76                         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
77                         graph.addMetadata(cm.add("Translated " + transformed.size() + " "
78                                 + (transformed.size() == 1 ? "element" : "elements")
79                                 + " by (" + dx + "," + dy + ") mm."));
80                     }
81                 }
82             });
83         } catch (DatabaseException e) {
84             e.printStackTrace();
85         }
86         
87         for (IElement dirty : elementsToDirty)
88             dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
89
90         setDirty();
91         remove();
92         return false;
93     }
94
95 }