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