1 package org.simantics.district.route.ui.actions;
3 import java.util.Collections;
5 import java.util.concurrent.CompletableFuture;
7 import org.simantics.Simantics;
8 import org.simantics.db.Resource;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.adapter.ActionFactory;
11 import org.simantics.district.route.Route;
12 import org.simantics.district.route.RouteJob;
13 import org.simantics.district.route.RouterConfiguration;
14 import org.simantics.district.route.internal.RoutePersistence;
15 import org.simantics.utils.ui.dialogs.ShowMessage;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public class ValidateRouteAction implements ActionFactory {
21 protected static final Logger LOGGER = LoggerFactory.getLogger(ValidateRouteAction.class);
24 public Runnable create(Object target) {
25 if (!(target instanceof Resource))
28 return new Runnable() {
32 Route route = Simantics.getSession().syncRequest(new RoutePersistence.RouteRequest((Resource)target));
34 ShowMessage.showInformation("Route Validation", "Reading the route object failed");
35 LOGGER.error("Reading a route object failed"); //$NON-NLS-1$
39 CompletableFuture<List<Resource>> result = new CompletableFuture<>();
40 result.exceptionally(e -> null).thenAccept(dnElements -> {
41 if (dnElements != null) {
42 if (!dnElements.isEmpty())
43 ShowMessage.showInformation("Route Validation", "The route is valid");
45 ShowMessage.showInformation("Route Validation", "The route produced an empty list of vertices");
49 RouterConfiguration config = new RouterConfiguration();
50 new RouteJob(config, route, result).schedule();
52 catch (DatabaseException e) {
53 LOGGER.error("Error in selecting route " + target, e); //$NON-NLS-1$