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