1 package org.simantics.district.network.ui.contributions;
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.NoninvertibleTransformException;
5 import java.awt.geom.Point2D;
6 import java.lang.reflect.InvocationTargetException;
7 import java.util.Collection;
10 import javax.inject.Named;
12 import org.eclipse.core.commands.ParameterizedCommand;
13 import org.eclipse.e4.core.di.annotations.CanExecute;
14 import org.eclipse.e4.core.di.annotations.Execute;
15 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
16 import org.eclipse.e4.ui.services.IServiceConstants;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.ui.IEditorPart;
19 import org.simantics.Simantics;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.SelectionHints;
27 import org.simantics.db.layer0.util.RemoverUtil;
28 import org.simantics.db.request.Read;
29 import org.simantics.diagram.stubs.DiagramResource;
30 import org.simantics.district.network.ModelledCRS;
31 import org.simantics.district.network.ontology.DistrictNetworkResource;
32 import org.simantics.district.network.ui.DistrictDiagramEditor;
33 import org.simantics.district.network.ui.NetworkDrawingParticipant;
34 import org.simantics.g2d.canvas.ICanvasContext;
35 import org.simantics.g2d.participant.MouseUtil;
36 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
37 import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
38 import org.simantics.utils.ui.ISelectionUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class ChangeVertexToRoutePointHandler {
44 private static final Logger LOGGER = LoggerFactory.getLogger(ChangeVertexToRoutePointHandler.class);
45 static List<Resource> elements;
46 static boolean cut = true;
49 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
50 List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
51 if (elements.size() != 1)
54 return Simantics.getSession().syncRequest(new Read<Boolean>() {
57 public Boolean perform(ReadGraph graph) throws DatabaseException {
58 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
59 for (Resource selection : elements) {
60 if (graph.isInstanceOf(selection, DN.Vertex)) {
61 Collection<Resource> startingEdge = graph.getObjects(selection, DN.HasStartVertex_Inverse);
62 Collection<Resource> endingEdge = graph.getObjects(selection, DN.HasEndVertex_Inverse);
63 return startingEdge.size() == 1 && endingEdge.size() == 1;
69 } catch (DatabaseException e) {
70 LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
76 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart, @Named(IServiceConstants.ACTIVE_SELECTION) Object selection, ParameterizedCommand command) {
77 final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
79 Resource vertex = elements.get(0);
81 Point2D mouseClicked = mouseClickedOnEditor(mActiveEditorPart);
82 if (mouseClicked != null) {
84 IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
85 if (activeEditorPart == null)
87 if (!(activeEditorPart instanceof DistrictDiagramEditor))
90 Simantics.getSession().asyncRequest(new WriteRequest() {
93 public void perform(WriteGraph graph) throws DatabaseException {
94 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
95 DiagramResource DIA = DiagramResource.getInstance(graph);
96 // we support only single start & end edge
97 Resource startingEdge = graph.getSingleObject(vertex, DN.HasEndVertex_Inverse);
98 Resource endingEdge = graph.getSingleObject(vertex, DN.HasStartVertex_Inverse);
99 double[] startingDetailed = graph.getPossibleRelatedValue(startingEdge, DN.Edge_HasGeometry);
100 double[] endingDetailed = graph.getPossibleRelatedValue(endingEdge, DN.Edge_HasGeometry);
101 Resource endingVertex = graph.getSingleObject(endingEdge, DN.HasEndVertex);
102 int length = startingDetailed.length + 2 + endingDetailed.length;
103 double[] newDetailed = new double[length];
104 for (int i = 0; i < startingDetailed.length; i++) {
105 double d = startingDetailed[i];
108 double[] coords = graph.getRelatedValue2(vertex, DIA.HasLocation);
109 int currentVertexIndex = startingDetailed.length;
110 newDetailed[currentVertexIndex] = coords[0];
111 newDetailed[currentVertexIndex + 1] = coords[1];
112 int f = startingDetailed.length + 2;
113 for (int i = 0; i < endingDetailed.length; i++) {
114 double d = endingDetailed[i];
115 newDetailed[f + i] = d;
117 graph.deny(startingEdge, DN.Edge_HasGeometry);
118 graph.claimLiteral(startingEdge, DN.Edge_HasGeometry, newDetailed, Bindings.DOUBLE_ARRAY);
120 graph.deny(startingEdge, DN.HasEndVertex);
121 graph.claim(startingEdge, DN.HasEndVertex, endingVertex);
123 RemoverUtil.remove(graph, vertex);
127 LOGGER.warn("No mouseClicked for editor {}", mActiveEditorPart);
129 } catch (InvocationTargetException e) {
130 LOGGER.error("Could not change route point to vertex", e);
134 private static Point2D mouseClickedOnEditor(MPart mActiveEditorPart) throws InvocationTargetException {
135 IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
136 if (activeEditorPart == null)
138 if (!(activeEditorPart instanceof DistrictDiagramEditor))
140 DistrictDiagramEditor editor = (DistrictDiagramEditor) activeEditorPart;
142 ICanvasContext ctx = editor.getAdapter(ICanvasContext.class);
143 NetworkDrawingParticipant drawingParticipant = ctx.getAtMostOneItemOfClass(NetworkDrawingParticipant.class);
144 AffineTransform drawingTransform = drawingParticipant.getTransform();
145 MouseUtil util = ctx.getAtMostOneItemOfClass(MouseUtil.class);
146 MouseInfo mouseInfo = util.getMousePressedInfo(0);
147 if (mouseInfo == null) {
150 Point2D canvasPosition = mouseInfo.canvasPosition;
151 Point2D transformed = null;
153 transformed = drawingTransform.inverseTransform(canvasPosition, new Point2D.Double());
154 } catch (NoninvertibleTransformException e) {
155 LOGGER.error("Could not create inverse transform of {}", drawingTransform, e);
156 throw new InvocationTargetException(e);
158 double x = ModelledCRS.xToLongitude(transformed.getX());
159 double y = ModelledCRS.yToLatitude(-transformed.getY());
160 return new Point2D.Double(x, y);