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