]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java
Add default mappings for right/left click & prevent edge drawing
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / NetworkDrawingNode.java
index b94639e2311080c9881044f0228a4c06e14bbc6d..35d76289b0ab000f18e36566e55e355c3c2a9042 100644 (file)
@@ -11,6 +11,7 @@ import java.awt.geom.Rectangle2D;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import org.simantics.Simantics;
 import org.simantics.db.Resource;
@@ -38,6 +39,7 @@ import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
 import org.simantics.scenegraph.utils.GeometryUtils;
 import org.simantics.scenegraph.utils.NodeUtil;
+import org.simantics.utils.threads.ThreadUtils;
 
 public class NetworkDrawingNode extends G2DNode {
 
@@ -163,15 +165,28 @@ public class NetworkDrawingNode extends G2DNode {
                 double scale = getTransform().getScaleY();
                 double x = ModelledCRS.xToLongitude(pos.getX() / scale);
                 double y = ModelledCRS.yToLatitude(-pos.getY() / scale);
-                Simantics.getSession().asyncRequest(new Write() {
-                    
-                    @Override
-                    public void perform(WriteGraph graph) throws DatabaseException {
-                        graph.markUndoPoint();
-                        Resource defaultMapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
-                        DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, 0, defaultMapping); // TODO: elevation can be fetched from e.g. elevation API
-                    }
-                });
+                
+                boolean leftButton = e.button == MouseEvent.LEFT_BUTTON;
+                
+                ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
+                    Simantics.getSession().asyncRequest(new Write() {
+                        
+                        @Override
+                        public void perform(WriteGraph graph) throws DatabaseException {
+                            graph.markUndoPoint();
+                            Resource mapping = null;
+                            if (leftButton) {
+                               mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).LeftClickDefaultMapping);
+                            } else {
+                               mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).RightClickDefaultMapping);
+                            }
+                            if (mapping == null) {
+                               mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
+                            }
+                            DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, 0, mapping); // TODO: elevation can be fetched from e.g. elevation API
+                        }
+                    });
+                }, 100, TimeUnit.MILLISECONDS);
             }
             repaint();
             return true;