X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.route.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Froute%2Fui%2Fhandlers%2FSelectRouteOnDiagram.java;h=572c65bf0ecc15871431ca3e470e2ef23b4ecbb8;hb=6f963a4a4ee67c7b346472e675ad71e6a0a85ba9;hp=7568ea308d7b6e975cb3564ee28c6674092e5355;hpb=00103b12908c5071e641ca2bea2c42030ff67770;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/SelectRouteOnDiagram.java b/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/SelectRouteOnDiagram.java index 7568ea30..572c65bf 100644 --- a/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/SelectRouteOnDiagram.java +++ b/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/SelectRouteOnDiagram.java @@ -1,26 +1,42 @@ package org.simantics.district.route.ui.handlers; +import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import javax.inject.Named; +import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.ui.model.application.ui.basic.MPart; import org.eclipse.e4.ui.services.IServiceConstants; import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; -import org.simantics.db.layer0.variable.Variable; +import org.simantics.Simantics; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.district.network.ui.DistrictNetworkUIUtil; import org.simantics.district.route.Route; import org.simantics.district.route.RouteJob; import org.simantics.district.route.RouterConfiguration; import org.simantics.utils.ui.ISelectionUtils; +import org.simantics.utils.ui.SWTUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Tuukka Lehtonen */ public class SelectRouteOnDiagram { + private static final Logger LOGGER = LoggerFactory.getLogger(SelectRouteOnDiagram.class); + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { + return ISelectionUtils.filterSingleSelection(selection, Route.class) != null; + } + @Execute public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell, @Named(IServiceConstants.ACTIVE_PART) MPart part, @@ -30,9 +46,36 @@ public class SelectRouteOnDiagram { if (route == null) return; - CompletableFuture> result = new CompletableFuture<>(); + Display display = activeShell.getDisplay(); + CompletableFuture> result = new CompletableFuture<>(); + result + .exceptionally(e -> Collections.emptyList()) + .thenAccept(dnElements -> { + if (!dnElements.isEmpty()) { + try { + openDiagram(display, dnElements); + } catch (DatabaseException e) { + LOGGER.error("Failed to open district network diagram with selection {}", dnElements); //$NON-NLS-1$ + } + } + }); + RouterConfiguration config = new RouterConfiguration(); new RouteJob(config, route, result).schedule(); } + public static boolean openDiagram(Display display, List dnElements) throws DatabaseException { + if (dnElements.isEmpty()) + return false; + + DistrictNetworkUIUtil.Input in = Simantics.getSession().syncRequest( + new DistrictNetworkUIUtil.ElementToInput(dnElements.get(0))); + if (in == null) + return false; + + SWTUtils.asyncExec(display, + () -> DistrictNetworkUIUtil.openDNDiagramEditorWithSelection(in, dnElements.toArray())); + return true; + } + } \ No newline at end of file