]> gerrit.simantics Code Review - simantics/district.git/blob
5b0e7d0506e456b40532fe8fd18364bb052123c0
[simantics/district.git] /
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.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;
18
19 public class ValidateRouteAction implements ActionFactory {
20
21     protected static final Logger LOGGER = LoggerFactory.getLogger(ValidateRouteAction.class);
22
23     @Override
24     public Runnable create(Object target) {
25         if (!(target instanceof Resource))
26             return null;
27
28         return new Runnable() {
29             @Override
30             public void run() {
31                 try {
32                     Route route = Simantics.getSession().syncRequest(new RoutePersistence.RouteRequest((Resource)target));
33                     if (route == null) {
34                         ShowMessage.showInformation("Route Validation", "Reading the route object failed");
35                         LOGGER.error("Reading a route object failed"); //$NON-NLS-1$
36                         return;
37                     }
38                     
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");
44                             else
45                                 ShowMessage.showInformation("Route Validation", "The route produced an empty list of vertices");
46                         }
47                     });
48                     
49                     RouterConfiguration config = new RouterConfiguration();
50                     new RouteJob(config, route, result).schedule();
51                 }
52                 catch (DatabaseException e) {
53                     LOGGER.error("Error in selecting route " + target, e); //$NON-NLS-1$
54                 }
55             }
56         };
57     }
58 }