1 package org.simantics.district.network.ui.participants;
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.awt.geom.Rectangle2D;
6 import java.util.ArrayList;
7 import java.util.Collection;
9 import org.simantics.Simantics;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.CommentMetadata;
13 import org.simantics.db.common.request.WriteRequest;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.diagram.elements.ElementTransforms;
16 import org.simantics.diagram.elements.ElementTransforms.TransformedObject;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.district.network.ModelledCRS;
19 import org.simantics.district.network.ontology.DistrictNetworkResource;
20 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
21 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
22 import org.simantics.g2d.canvas.Hints;
23 import org.simantics.g2d.diagram.participant.pointertool.TranslateMode;
24 import org.simantics.g2d.element.ElementUtils;
25 import org.simantics.g2d.element.IElement;
26 import org.simantics.g2d.element.handler.Move;
28 public class DNTranslateMode extends TranslateMode {
30 public DNTranslateMode(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {
31 super(startingPoint, currentPoint, mouseId, elements);
35 protected boolean commit() {
36 for (IElement el : elementsToReallyTranslate) {
37 Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);
39 Point2D oldPos = move.getPosition(el);
40 move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);
44 Simantics.getSession().syncRequest(new WriteRequest() {
46 public void perform(WriteGraph graph) throws DatabaseException {
47 Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();
48 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
49 DiagramResource DIA = DiagramResource.getInstance(graph);
50 for (IElement e : elementsToReallyTranslate) {
52 DistrictNetworkVertexNode node = e.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
53 Rectangle2D bounds = node.getBounds();
55 Object obj = ElementUtils.getObject(e);
56 if (obj instanceof Resource) {
57 Resource res = (Resource) obj;
58 AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());
59 if (graph.isInstanceOf(res, DN.Vertex)) {
61 double x = at.getTranslateX();
62 double y = at.getTranslateY();
64 double lat = ModelledCRS.yToLatitude(y / at.getScaleY());
65 double lon = ModelledCRS.xToLongitude(x / at.getScaleX());
68 double[] coords = new double[] { lon, lat };
69 graph.claimLiteral(res, DIA.HasLocation, coords);
71 // fallback to normal HasTransforms
72 transformed.add( new TransformedObject((Resource) obj, at) );
76 if (!transformed.isEmpty()) {
77 graph.markUndoPoint();
79 ElementTransforms.setTransformRequest(transformed).perform(graph);
81 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
82 graph.addMetadata(cm.add("Translated " + transformed.size() + " "
83 + (transformed.size() == 1 ? "element" : "elements")
84 + " by (" + dx + "," + dy + ") mm."));
88 } catch (DatabaseException e) {
92 for (IElement dirty : elementsToDirty)
93 dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);