1 package org.simantics.district.route.ui.actions;
3 import java.util.Collections;
5 import java.util.concurrent.CompletableFuture;
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;
20 public class SelectRouteAction implements ActionFactory {
22 protected static final Logger LOGGER = LoggerFactory.getLogger(SelectRouteAction.class);
25 public Runnable create(Object target) {
26 if (!(target instanceof Resource))
29 return new Runnable() {
33 Route route = Simantics.getSession().syncRequest(new RoutePersistence.RouteRequest((Resource)target));
35 LOGGER.error("Reading a route object failed"); //$NON-NLS-1$
39 CompletableFuture<List<Resource>> result = new CompletableFuture<>();
41 .exceptionally(e -> Collections.emptyList())
42 .thenAccept(dnElements -> {
43 if (!dnElements.isEmpty()) {
45 Display display = Display.getDefault();
47 DistrictNetworkUIUtil.openDNDiagramWithSelection(display, dnElements);
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$
57 RouterConfiguration config = new RouterConfiguration();
58 new RouteJob(config, route, result).schedule();
60 catch (DatabaseException e) {
61 LOGGER.error("Error in selecting route " + target, e); //$NON-NLS-1$