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;
9 import java.util.Optional;
10 import java.util.concurrent.TimeUnit;
12 import javax.inject.Named;
14 import org.eclipse.core.commands.ParameterizedCommand;
15 import org.eclipse.e4.core.di.annotations.CanExecute;
16 import org.eclipse.e4.core.di.annotations.Execute;
17 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18 import org.eclipse.e4.ui.services.IServiceConstants;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.ui.IEditorPart;
21 import org.simantics.Simantics;
22 import org.simantics.databoard.Bindings;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.WriteGraph;
26 import org.simantics.db.common.request.ReadRequest;
27 import org.simantics.db.common.request.WriteRequest;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.layer0.SelectionHints;
30 import org.simantics.db.layer0.util.RemoverUtil;
31 import org.simantics.db.request.Read;
32 import org.simantics.diagram.stubs.DiagramResource;
33 import org.simantics.district.network.DistrictNetworkUtil;
34 import org.simantics.district.network.ModelledCRS;
35 import org.simantics.district.network.ontology.DistrictNetworkResource;
36 import org.simantics.district.network.ui.DNEdgeBuilder;
37 import org.simantics.district.network.ui.DistrictDiagramEditor;
38 import org.simantics.district.network.ui.NetworkDrawingParticipant;
39 import org.simantics.g2d.canvas.ICanvasContext;
40 import org.simantics.g2d.participant.MouseUtil;
41 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
42 import org.simantics.maps.elevation.server.SingletonTiffTileInterface;
43 import org.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences;
44 import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
45 import org.simantics.utils.threads.ThreadUtils;
46 import org.simantics.utils.ui.ISelectionUtils;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class ChangeVertexToRoutePointHandler {
52 private static final Logger LOGGER = LoggerFactory.getLogger(ChangeVertexToRoutePointHandler.class);
53 static List<Resource> elements;
54 static boolean cut = true;
57 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
58 List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
59 if (elements.size() != 1)
62 return Simantics.getSession().syncRequest(new Read<Boolean>() {
65 public Boolean perform(ReadGraph graph) throws DatabaseException {
66 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
67 for (Resource selection : elements) {
68 if (graph.isInstanceOf(selection, DN.Vertex)) {
69 Collection<Resource> startingEdge = graph.getObjects(selection, DN.HasStartVertex_Inverse);
70 Collection<Resource> endingEdge = graph.getObjects(selection, DN.HasEndVertex_Inverse);
71 return startingEdge.size() == 1 && endingEdge.size() == 1;
77 } catch (DatabaseException e) {
78 LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
84 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart, @Named(IServiceConstants.ACTIVE_SELECTION) Object selection, ParameterizedCommand command) {
85 final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
87 Resource vertex = elements.get(0);
89 Point2D mouseClicked = mouseClickedOnEditor(mActiveEditorPart);
90 if (mouseClicked != null) {
92 IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
93 if (activeEditorPart == null)
95 if (!(activeEditorPart instanceof DistrictDiagramEditor))
97 DistrictDiagramEditor editor = (DistrictDiagramEditor) activeEditorPart;
98 Resource diagram = editor.getInputResource();
100 Simantics.getSession().asyncRequest(new WriteRequest() {
103 public void perform(WriteGraph graph) throws DatabaseException {
104 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
105 DiagramResource DIA = DiagramResource.getInstance(graph);
106 // we support only single start & end edge
107 Resource startingEdge = graph.getSingleObject(vertex, DN.HasEndVertex_Inverse);
108 Resource endingEdge = graph.getSingleObject(vertex, DN.HasStartVertex_Inverse);
109 double[] startingDetailed = graph.getPossibleRelatedValue(startingEdge, DN.Edge_HasGeometry);
110 double[] endingDetailed = graph.getPossibleRelatedValue(endingEdge, DN.Edge_HasGeometry);
111 Resource endingVertex = graph.getSingleObject(endingEdge, DN.HasEndVertex);
112 int length = startingDetailed.length + 2 + endingDetailed.length;
113 double[] newDetailed = new double[length];
114 for (int i = 0; i < startingDetailed.length; i++) {
115 double d = startingDetailed[i];
118 double[] coords = graph.getRelatedValue2(vertex, DIA.HasLocation);
119 int currentVertexIndex = startingDetailed.length;
120 newDetailed[currentVertexIndex] = coords[0];
121 newDetailed[currentVertexIndex + 1] = coords[1];
122 int f = startingDetailed.length + 2;
123 for (int i = 0; i < endingDetailed.length; i++) {
124 double d = endingDetailed[i];
125 newDetailed[f + i] = d;
127 graph.deny(startingEdge, DN.Edge_HasGeometry);
128 graph.claimLiteral(startingEdge, DN.Edge_HasGeometry, newDetailed, Bindings.DOUBLE_ARRAY);
130 graph.deny(startingEdge, DN.HasEndVertex);
131 graph.claim(startingEdge, DN.HasEndVertex, endingVertex);
133 RemoverUtil.remove(graph, vertex);
137 LOGGER.warn("No mouseClicked for editor {}", mActiveEditorPart);
139 } catch (InvocationTargetException e) {
140 LOGGER.error("Could not change route point to vertex", e);
144 private static Point2D mouseClickedOnEditor(MPart mActiveEditorPart) throws InvocationTargetException {
145 IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
146 if (activeEditorPart == null)
148 if (!(activeEditorPart instanceof DistrictDiagramEditor))
150 DistrictDiagramEditor editor = (DistrictDiagramEditor) activeEditorPart;
152 ICanvasContext ctx = editor.getAdapter(ICanvasContext.class);
153 NetworkDrawingParticipant drawingParticipant = ctx.getAtMostOneItemOfClass(NetworkDrawingParticipant.class);
154 AffineTransform drawingTransform = drawingParticipant.getTransform();
155 MouseUtil util = ctx.getAtMostOneItemOfClass(MouseUtil.class);
156 MouseInfo mouseInfo = util.getMousePressedInfo(0);
157 if (mouseInfo == null) {
160 Point2D canvasPosition = mouseInfo.canvasPosition;
161 Point2D transformed = null;
163 transformed = drawingTransform.inverseTransform(canvasPosition, new Point2D.Double());
164 } catch (NoninvertibleTransformException e) {
165 LOGGER.error("Could not create inverse transform of {}", drawingTransform, e);
166 throw new InvocationTargetException(e);
168 double x = ModelledCRS.xToLongitude(transformed.getX());
169 double y = ModelledCRS.yToLatitude(-transformed.getY());
170 return new Point2D.Double(x, y);