X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Fparticipants%2FRoutingMode.java;h=54224f4f85e7ea0813516e73350d6a2aee90e2cc;hb=7c1f539de88c6ca79b2ac6dc891be3f45282abe2;hp=d274a617931b11d1641149bbc722ae4826f45699;hpb=716ce6c9abe27be04635922ad437f242c4bd7dfc;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/RoutingMode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/RoutingMode.java index d274a617..54224f4f 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/RoutingMode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/RoutingMode.java @@ -7,14 +7,19 @@ 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; import org.eclipse.ui.PlatformUI; import org.simantics.db.Resource; import org.simantics.diagram.ui.DiagramModelHints; +import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement; 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; @@ -26,6 +31,7 @@ import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit; import org.simantics.g2d.diagram.handler.PickContext; import org.simantics.g2d.diagram.participant.Selection; import org.simantics.g2d.diagram.participant.pointertool.AbstractMode; +import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; import org.simantics.g2d.participant.TransformUtil; @@ -40,6 +46,7 @@ import org.simantics.scenegraph.g2d.nodes.SingleElementNode; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintObservable; import org.simantics.utils.ui.AdaptionUtils; +import org.simantics.utils.ui.SWTUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -81,6 +88,8 @@ public class RoutingMode extends AbstractMode { */ protected SingleElementNode node = null; + private RouteServiceListener routeServiceListener; + public RoutingMode(int mouseId) { super(mouseId); } @@ -92,9 +101,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); } } @@ -105,11 +123,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); } @@ -127,12 +147,22 @@ public class RoutingMode extends AbstractMode { return; Object o = ElementUtils.getObject(element); - if (o instanceof Resource) { + if (o instanceof Resource && filterAcceptableElement(element, o)) { Waypoint wp = route.createWaypoint(o); route.addWaypoint(wp); } } + /** + * For now, routing can only start from vertices + * + * @return true if element is a vertex + */ + private boolean filterAcceptableElement(IElement element, Object o) { + ElementClass elementClass = element.getElementClass(); + return elementClass.getId().equals(DistrictNetworkVertexElement.CLASS.getId()); + } + @SGInit public void initSG(G2DParentNode parent) { this.parent = parent; @@ -158,29 +188,75 @@ public class RoutingMode extends AbstractMode { return false; KeyPressedEvent kpe = (KeyPressedEvent) e; if (kpe.keyCode == java.awt.event.KeyEvent.VK_ENTER) { - routeService.persistRoute(route); + Route committedRoute = route; route = null; + SWTUtils.asyncExec(Display.getDefault(), () -> { + 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)) { - InputDialog dialog = new InputDialog( - PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), - "Rename Route", - "Route name", - route.getName(), - s -> s.trim().length() > 0 ? null : "Name must be non-empty"); - if (dialog.open() == Window.OK) { - route.setName(dialog.getValue()); - } + // TODO: still needs key binding contribution for the district diagram editor + SWTUtils.asyncExec(Display.getDefault(), () -> { + String newName = askRouteName("Rename Route", route.getName()); + if (newName != null) { + route.setName(newName); + routeService.refreshRoute(route); + } + }); } } 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(), + dialogTitle, + "Route name", + initialValue, + s -> s.trim().length() > 0 ? null : "Name must be non-empty"); + if (dialog.open() == Window.OK) { + return dialog.getValue(); + } + return null; + } + protected boolean dispose() { setDirty(); remove();