package org.simantics.district.network.ui.participants; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.Collection; import org.simantics.Simantics; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.CommentMetadata; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.elements.ElementTransforms; import org.simantics.diagram.elements.ElementTransforms.TransformedObject; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.district.network.ModelledCRS; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.g2d.canvas.Hints; import org.simantics.g2d.diagram.participant.pointertool.TranslateMode; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; public class DNTranslateMode extends TranslateMode { public DNTranslateMode(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection elements) { super(startingPoint, currentPoint, mouseId, elements); } @Override protected boolean commit() { try { Simantics.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { Collection transformed = new ArrayList(); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); DiagramResource DIA = DiagramResource.getInstance(graph); for (IElement e : elementsToReallyTranslate) { Object obj = ElementUtils.getObject(e); if (obj instanceof Resource && graph.isInstanceOf((Resource) obj, DN.Vertex)) { Resource res = (Resource) obj; // Read current location from graph double[] currentCoords = graph.getRelatedValue2(res, DIA.HasLocation); double x = ModelledCRS.longitudeToX(currentCoords[0]); double y = ModelledCRS.latitudeToY(currentCoords[1]); AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform()); if (graph.isInstanceOf(res, DN.Vertex)) { double lat = ModelledCRS.yToLatitude(y + (-dy / at.getScaleY())); double lon = ModelledCRS.xToLongitude(x + (dx / at.getScaleX())); // write to db double[] coords = new double[] { lon, lat }; graph.claimLiteral(res, DIA.HasLocation, coords); } else { // fallback to normal HasTransforms transformed.add( new TransformedObject((Resource) obj, at) ); } } } if (!transformed.isEmpty()) { graph.markUndoPoint(); // Normal transforms ElementTransforms.setTransformRequest(transformed).perform(graph); CommentMetadata cm = graph.getMetadata(CommentMetadata.class); graph.addMetadata(cm.add("Translated " + transformed.size() + " " + (transformed.size() == 1 ? "element" : "elements") + " by (" + dx + "," + -dy + ") mm.")); } } }); } catch (DatabaseException e) { e.printStackTrace(); } for (IElement dirty : elementsToDirty) dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY); setDirty(); remove(); return false; } }