]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Minor usability fixes for routing in district diagrams 16/2616/1
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 24 Jan 2019 13:59:48 +0000 (15:59 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Thu, 24 Jan 2019 13:59:48 +0000 (15:59 +0200)
gitlab #25

Change-Id: Ie5f87133f4602898377bf8c4b279c02d4cbec281

org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNPointerInteractor.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/RoutingMode.java
org.simantics.district.route/src/org/simantics/district/route/Route.java
org.simantics.district.route/src/org/simantics/district/route/internal/RouteImpl.java

index 298c4ce11e7be89c25f881cc6e1ca710b20f7b39..c61adba9fa335f7303a75bc223080dd42f4d4fa5 100644 (file)
@@ -7,6 +7,7 @@ import java.util.Set;
 
 import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElement;
 import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement;
+import org.simantics.g2d.canvas.ICanvasContext;
 import org.simantics.g2d.canvas.ICanvasParticipant;
 import org.simantics.g2d.diagram.handler.PickRequest.PickSorter;
 import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;
@@ -21,6 +22,8 @@ import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
 
 public class DNPointerInteractor extends PointerInteractor {
 
+    private RoutingMode routingMode;
+
     private static class DNPickSorter implements PickSorter {
 
         @Override
@@ -69,7 +72,11 @@ public class DNPointerInteractor extends PointerInteractor {
     @EventHandler(priority = 1000)
     public boolean enterroutingMode(KeyEvent ke) {
         if (Character.toLowerCase(ke.character) == 't' && ke instanceof KeyReleasedEvent) {
-            getContext().add(new RoutingMode(0));
+            ICanvasContext canvasContext = getContext();
+            if (routingMode == null || routingMode.isRemoved()) {
+                routingMode = new RoutingMode(0);
+                canvasContext.add(routingMode);
+            }
         }
         return false;
     }
index 4f023c880a2968d2a3d306fff7ed685551956c47..daacb31296ac0fa7c64d82b1366cc13a7cf1fb2d 100644 (file)
@@ -7,6 +7,7 @@ import java.util.Collection;
 import org.eclipse.jface.dialogs.IDialogSettings;
 import org.eclipse.jface.dialogs.IInputValidator;
 import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.window.Window;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
@@ -15,7 +16,9 @@ import org.simantics.db.Resource;
 import org.simantics.diagram.ui.DiagramModelHints;
 import org.simantics.district.network.ui.internal.Activator;
 import org.simantics.district.route.Route;
+import org.simantics.district.route.RouteEvent;
 import org.simantics.district.route.RouteService;
+import org.simantics.district.route.RouteServiceListener;
 import org.simantics.district.route.Waypoint;
 import org.simantics.g2d.canvas.ICanvasContext;
 import org.simantics.g2d.canvas.IMouseCursorContext;
@@ -83,6 +86,8 @@ public class RoutingMode extends AbstractMode {
      */
     protected SingleElementNode    node = null;
 
+    private RouteServiceListener routeServiceListener;
+
     public RoutingMode(int mouseId) {
         super(mouseId);
     }
@@ -94,9 +99,18 @@ public class RoutingMode extends AbstractMode {
         cursor = mcc == null ? null : mcc.setCursor(mouseId, new Cursor(Cursor.CROSSHAIR_CURSOR));
         routeService = Activator.getInstance().getRouteService();
         if (routeService != null) {
+            
+            routeServiceListener = e -> {
+                switch (e.type) {
+                case RouteEvent.TYPE_ROUTE_PERSISTED:
+                    dispose();
+                    break;
+                }
+            };
+            routeService.addListener(routeServiceListener);
             Resource diagramResource = getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
             LOGGER.info("Diagram resource: " + diagramResource);
-            route = routeService.createRoute("Current", diagramResource);
+            route = routeService.createRoute("[UNPERSISTED] Current", diagramResource);
             routeService.registerRoute(route);
         }
     }
@@ -107,11 +121,13 @@ public class RoutingMode extends AbstractMode {
             cursor.remove();
             cursor = null;
         }
-        // Discard route if it hasn't been persisted before this
-        if (route != null) {
-            routeService.discardRoute(route);
-            route = null;
-            routeService = null;
+        if (routeService != null) {
+            routeService.removeListener(routeServiceListener);
+            // Discard route if it hasn't been persisted before this
+            if (route != null && !route.persisted()) {
+                routeService.discardRoute(route);
+                route = null;
+            }
         }
         super.removedFromContext(ctx);
     }
@@ -163,19 +179,22 @@ public class RoutingMode extends AbstractMode {
                 Route committedRoute = route;
                 route = null;
                 SWTUtils.asyncExec(Display.getDefault(), () -> {
-                    String newName = askRouteName("Confirm Route", committedRoute.getName());
-                    if (newName != null) {
-                        committedRoute.setName(newName);
-                        routeService.persistRoute(committedRoute);
-                    } else {
-                        routeService.discardRoute(committedRoute);
-                    }
+                    askRouteNameAndPersist(committedRoute);
                 });
                 dispose();
             }
         } else if (e instanceof CommandEvent) {
             Command cmd = ((CommandEvent) e).command;
             if (cmd.equals(Commands.CANCEL)) {
+                // let's ensure if user wants to dispose possibly unpersisted route
+                if (route != null && !route.persisted()) {
+                    Route committedRoute = route;
+                    SWTUtils.asyncExec(Display.getDefault(), () -> {
+                        if (!askDiscardUnpersistedRoute()) {
+                            askRouteNameAndPersist(committedRoute);
+                        }
+                    });
+                }
                 return dispose();
             } else if (cmd.equals(Commands.RENAME)) {
                 // TODO: still needs key binding contribution for the district diagram editor
@@ -191,6 +210,28 @@ public class RoutingMode extends AbstractMode {
         return false;
     }
 
+    private void askRouteNameAndPersist(Route committedRoute) {
+        String newName = askRouteName("Confirm Route", committedRoute.getName());
+        if (newName != null) {
+            committedRoute.setName(newName);
+            routeService.persistRoute(committedRoute);
+        } else {
+            routeService.discardRoute(committedRoute);
+        }
+    }
+    
+    private boolean askDiscardUnpersistedRoute() {
+        MessageDialog dialog = new MessageDialog(
+                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+                "Discard route",
+                null,
+                "Discard current unpersisted route?",
+                MessageDialog.INFORMATION,
+                0,
+                new String[] { "OK", "Cancel" });
+        return dialog.open() == Window.OK;
+    }
+    
     private String askRouteName(String dialogTitle, String initialValue) {
         InputDialog dialog = new InputDialog(
                 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
index fcd91171eb1c768975da674211428df18c32aaa4..4801e10a69e40b5ff23dd0febd249a12596e3fe0 100644 (file)
@@ -16,6 +16,8 @@ public interface Route {
 
     Waypoint createWaypoint(Object backend);
 
+    boolean persisted();
+
     default void addWaypoint(Waypoint r) {
         addWaypoint(count(), r);
     }
index 224f4af0392084adea8d168f215c10ace4c6b30e..25b12be760df602448cf1ac03935ac468f8ffd3c 100644 (file)
@@ -74,6 +74,11 @@ public class RouteImpl implements Route {
         fireEvent(RouteEvent.TYPE_ROUTE_RENAMED);
     }
 
+    @Override
+    public boolean persisted() {
+        return backend != null;
+    }
+
     @Override
     public Waypoint createWaypoint(Object backend) {
         if (backend instanceof Resource) {