]> gerrit.simantics Code Review - simantics/district.git/blob
572c65bf0ecc15871431ca3e470e2ef23b4ecbb8
[simantics/district.git] /
1 package org.simantics.district.route.ui.handlers;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.concurrent.CompletableFuture;
6
7 import javax.inject.Named;
8
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;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class SelectRouteOnDiagram {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(SelectRouteOnDiagram.class);
34
35     @CanExecute
36     public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
37         return ISelectionUtils.filterSingleSelection(selection, Route.class) != null;
38     }
39
40     @Execute
41     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell,
42             @Named(IServiceConstants.ACTIVE_PART) MPart part,
43             @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
44         // get selected route
45         Route route = ISelectionUtils.filterSingleSelection(selection, Route.class);
46         if (route == null)
47             return;
48
49         Display display = activeShell.getDisplay();
50         CompletableFuture<List<Resource>> result = new CompletableFuture<>();
51         result
52         .exceptionally(e -> Collections.emptyList())
53         .thenAccept(dnElements -> {
54             if (!dnElements.isEmpty()) {
55                 try {
56                     openDiagram(display, dnElements);
57                 } catch (DatabaseException e) {
58                     LOGGER.error("Failed to open district network diagram with selection {}", dnElements); //$NON-NLS-1$
59                 }
60             }
61         });
62
63         RouterConfiguration config = new RouterConfiguration();
64         new RouteJob(config, route, result).schedule();
65     }
66
67     public static boolean openDiagram(Display display, List<Resource> dnElements) throws DatabaseException {
68         if (dnElements.isEmpty())
69             return false;
70
71         DistrictNetworkUIUtil.Input in = Simantics.getSession().syncRequest(
72                 new DistrictNetworkUIUtil.ElementToInput(dnElements.get(0)));
73         if (in == null)
74             return false;
75
76         SWTUtils.asyncExec(display,
77                 () -> DistrictNetworkUIUtil.openDNDiagramEditorWithSelection(in, dnElements.toArray()));
78         return true;
79     }
80
81 }