]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.route/src/org/simantics/district/route/internal/RouteImpl.java
Improved Routes view functionality
[simantics/district.git] / org.simantics.district.route / src / org / simantics / district / route / internal / RouteImpl.java
1 package org.simantics.district.route.internal;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.simantics.Simantics;
8 import org.simantics.db.Resource;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.request.Read;
11 import org.simantics.district.route.Route;
12 import org.simantics.district.route.RouteEvent;
13 import org.simantics.district.route.Waypoint;
14 import org.simantics.utils.threads.ThreadUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class RouteImpl implements Route {
19
20     private static final Logger LOGGER = LoggerFactory.getLogger(RouteImpl.class);
21
22     private String name;
23     private Resource model;
24     private Resource backend;
25
26     private RouteServiceImpl rs;
27
28     private List<Waypoint> waypoints;
29     private List<Waypoint> unmodifiableWaypoints;
30
31     public RouteImpl(String name) {
32         this.name = name;
33         routeService(rs);
34         waypoints(new ArrayList<>());
35     }
36
37     public RouteImpl modelEntity(Resource model) {
38         this.model = model;
39         return this;
40     }
41
42     public Resource modelEntity() {
43         return model;
44     }
45
46     public RouteImpl backend(Resource backend) {
47         this.backend = backend;
48         return this;
49     }
50
51     public Resource backend() {
52         return backend;
53     }
54
55     public RouteImpl waypoints(List<Waypoint> waypoints) {
56         this.waypoints = waypoints;
57         this.unmodifiableWaypoints = Collections.unmodifiableList(waypoints);
58         return this;
59     }
60
61     public RouteImpl routeService(RouteServiceImpl rs) {
62         this.rs = rs;
63         return this;
64     }
65
66     @Override
67     public String getName() {
68         return name;
69     }
70
71     @Override
72     public void setName(String name) {
73         this.name = name;
74         fireEvent(RouteEvent.TYPE_ROUTE_RENAMED);
75     }
76
77     @Override
78     public Waypoint createWaypoint(Object backend) {
79         if (backend instanceof Resource) {
80             Resource waypoint = (Resource) backend;
81             WaypointImpl wp = new WaypointImpl(waypoint, "");
82
83             // Read real label in background
84             ThreadUtils.getBlockingWorkExecutor().submit(() -> {
85                 try {
86                     Waypoint p = Simantics.getSession().syncRequest(
87                             (Read<Waypoint>) graph -> RoutePersistence.toWaypoint(graph, waypoint));
88                     if (p != null) {
89                         wp.setLabel(p.getLabel());
90                         rs.fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED, RouteImpl.this);
91                     }
92                 } catch (DatabaseException e) {
93                     LOGGER.error("Failed to read waypoint {} label", backend, e);
94                 }
95             });
96
97             return wp;
98         }
99         throw new IllegalArgumentException("only Resource type waypoints supported, got " + backend); //$NON-NLS-1$
100     }
101
102     @Override
103     public void addWaypoint(int index, Waypoint r) {
104         waypoints.add(index, r);
105         fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
106     }
107
108     @Override
109     public void removeWaypoint(Waypoint r) {
110         waypoints.remove(r);
111         fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
112     }
113
114     @Override
115     public List<Waypoint> waypoints() {
116         return unmodifiableWaypoints;
117     }
118
119     @Override
120     public String toString() {
121         return String.format("%s@%x [%s, waypoints=%s]",
122                 getClass().getName(),
123                 System.identityHashCode(this),
124                 name,
125                 waypoints.toString());
126     }
127
128     private void fireEvent(int type) {
129         if (rs != null)
130             rs.fireEvent(type, this);
131     }
132
133 }