]> gerrit.simantics Code Review - simantics/district.git/blob
27de17e921f0ce9bb765a9f022075eee8504f9d3
[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.ontology.DistrictNetworkResource;
18 import org.simantics.g2d.canvas.Hints;
19 import org.simantics.g2d.diagram.participant.pointertool.TranslateMode;
20 import org.simantics.g2d.element.ElementUtils;
21 import org.simantics.g2d.element.IElement;
22 import org.simantics.g2d.element.handler.Move;
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         for (IElement el : elementsToReallyTranslate) {
33             Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);
34             if (move != null) {
35                 Point2D oldPos = move.getPosition(el);
36                 move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);
37             }
38         }
39         try {
40             Simantics.getSession().syncRequest(new WriteRequest() {
41                 @Override
42                 public void perform(WriteGraph graph) throws DatabaseException {
43                     Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();
44                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
45                     DiagramResource DIA = DiagramResource.getInstance(graph);
46                     for (IElement e : elementsToReallyTranslate) {
47                         Object obj = ElementUtils.getObject(e);
48                         if (obj instanceof Resource) {
49                             Resource res = (Resource) obj;
50                             AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());
51                             if (graph.isInstanceOf(res, DN.Vertex)) {
52                                 // write to db
53                                 double[] coords = new double[] { at.getTranslateX(), at.getTranslateY() };
54                                 graph.claimLiteral(res, DIA.HasLocation, coords);
55                             } else {
56                                 // fallback to normal HasTransforms
57                                 transformed.add( new TransformedObject((Resource) obj, at) );
58                             }
59                         }
60                     }
61                     if (!transformed.isEmpty()) {
62                         graph.markUndoPoint();
63                         // Normal transforms
64                         ElementTransforms.setTransformRequest(transformed).perform(graph);
65                         
66                         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
67                         graph.addMetadata(cm.add("Translated " + transformed.size() + " "
68                                 + (transformed.size() == 1 ? "element" : "elements")
69                                 + " by (" + dx + "," + dy + ") mm."));
70                     }
71                 }
72             });
73         } catch (DatabaseException e) {
74             e.printStackTrace();
75         }
76         
77         for (IElement dirty : elementsToDirty)
78             dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
79
80         setDirty();
81         remove();
82         return false;
83     }
84
85 }