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.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.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, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { // get selected route Route route = ISelectionUtils.filterSingleSelection(selection, Route.class); if (route == null) return; Display display = activeShell.getDisplay(); CompletableFuture> result = new CompletableFuture<>(); result .exceptionally(e -> Collections.emptyList()) .thenAccept(dnElements -> { if (!dnElements.isEmpty()) { try { DistrictNetworkUIUtil.openDNDiagramWithSelection(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(); } }