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;
public class DNPointerInteractor extends PointerInteractor {
+ private RoutingMode routingMode;
+
private static class DNPickSorter implements PickSorter {
@Override
@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;
}
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.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;
*/
protected SingleElementNode node = null;
+ private RouteServiceListener routeServiceListener;
+
public RoutingMode(int mouseId) {
super(mouseId);
}
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);
}
}
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);
}
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
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(),