1 package org.simantics.district.route.ui.handlers;
3 import java.util.Collections;
5 import java.util.concurrent.CompletableFuture;
7 import javax.inject.Named;
9 import org.eclipse.e4.core.di.annotations.CanExecute;
10 import org.eclipse.e4.core.di.annotations.Execute;
11 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
12 import org.eclipse.e4.ui.services.IServiceConstants;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.swt.widgets.Shell;
16 import org.simantics.Simantics;
17 import org.simantics.db.Resource;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.district.network.ui.DistrictNetworkUIUtil;
20 import org.simantics.district.route.Route;
21 import org.simantics.district.route.RouteJob;
22 import org.simantics.district.route.RouterConfiguration;
23 import org.simantics.utils.ui.ISelectionUtils;
24 import org.simantics.utils.ui.SWTUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * @author Tuukka Lehtonen
31 public class SelectRouteOnDiagram {
33 private static final Logger LOGGER = LoggerFactory.getLogger(SelectRouteOnDiagram.class);
36 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
37 return ISelectionUtils.filterSingleSelection(selection, Route.class) != null;
41 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell,
42 @Named(IServiceConstants.ACTIVE_PART) MPart part,
43 @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
45 Route route = ISelectionUtils.filterSingleSelection(selection, Route.class);
49 Display display = activeShell.getDisplay();
50 CompletableFuture<List<Resource>> result = new CompletableFuture<>();
52 .exceptionally(e -> Collections.emptyList())
53 .thenAccept(dnElements -> {
54 if (!dnElements.isEmpty()) {
56 openDiagram(display, dnElements);
57 } catch (DatabaseException e) {
58 LOGGER.error("Failed to open district network diagram with selection {}", dnElements); //$NON-NLS-1$
63 RouterConfiguration config = new RouterConfiguration();
64 new RouteJob(config, route, result).schedule();
67 public static boolean openDiagram(Display display, List<Resource> dnElements) throws DatabaseException {
68 if (dnElements.isEmpty())
71 DistrictNetworkUIUtil.Input in = Simantics.getSession().syncRequest(
72 new DistrictNetworkUIUtil.ElementToInput(dnElements.get(0)));
76 SWTUtils.asyncExec(display,
77 () -> DistrictNetworkUIUtil.openDNDiagramEditorWithSelection(in, dnElements.toArray()));