]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.route.ui/src/org/simantics/district/route/ui/actions/SelectRouteAction.java
Added a browser context for route-related actions.
[simantics/district.git] / org.simantics.district.route.ui / src / org / simantics / district / route / ui / actions / SelectRouteAction.java
1 package org.simantics.district.route.ui.actions;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.concurrent.CompletableFuture;
6
7 import org.eclipse.swt.widgets.Display;
8 import org.simantics.Simantics;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.adapter.ActionFactory;
12 import org.simantics.district.network.ui.DistrictNetworkUIUtil;
13 import org.simantics.district.route.Route;
14 import org.simantics.district.route.RouteJob;
15 import org.simantics.district.route.RouterConfiguration;
16 import org.simantics.district.route.internal.RoutePersistence;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class SelectRouteAction implements ActionFactory {
21
22     protected static final Logger LOGGER = LoggerFactory.getLogger(SelectRouteAction.class);
23
24     @Override
25     public Runnable create(Object target) {
26         if (!(target instanceof Resource))
27             return null;
28
29         return new Runnable() {
30             @Override
31             public void run() {
32                 try {
33                     Route route = Simantics.getSession().syncRequest(new RoutePersistence.RouteRequest((Resource)target));
34                     if (route == null) {
35                         LOGGER.error("Reading a route object failed"); //$NON-NLS-1$
36                         return;
37                     }
38                     
39                     CompletableFuture<List<Resource>> result = new CompletableFuture<>();
40                     result
41                     .exceptionally(e -> Collections.emptyList())
42                     .thenAccept(dnElements -> {
43                         if (!dnElements.isEmpty()) {
44                             try {
45                                 Display display = Display.getDefault();
46                                 if (display != null)
47                                     DistrictNetworkUIUtil.openDNDiagramWithSelection(display, dnElements);
48                                 else
49                                     LOGGER.error("No display found"); //$NON-NLS-1$
50                             } catch (DatabaseException e) {
51                                 LOGGER.error("Failed to open district network diagram with selection " + dnElements, e); //$NON-NLS-1$
52                                 
53                             }
54                         }
55                     });
56                     
57                     RouterConfiguration config = new RouterConfiguration();
58                     new RouteJob(config, route, result).schedule();
59                 }
60                 catch (DatabaseException e) {
61                     LOGGER.error("Error in selecting route " + target, e); //$NON-NLS-1$
62                 }
63             }
64         };
65     }
66
67 }