1 package org.simantics.district.route.internal;
3 import java.util.ArrayList;
4 import java.util.Collections;
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;
18 public class RouteImpl implements Route {
20 private static final Logger LOGGER = LoggerFactory.getLogger(RouteImpl.class);
23 private Resource model;
24 private Resource backend;
26 private RouteServiceImpl rs;
28 private List<Waypoint> waypoints;
29 private List<Waypoint> unmodifiableWaypoints;
31 public RouteImpl(String name) {
34 waypoints(new ArrayList<>());
37 public RouteImpl modelEntity(Resource model) {
42 public Resource modelEntity() {
46 public RouteImpl backend(Resource backend) {
47 this.backend = backend;
51 public Resource backend() {
55 public RouteImpl waypoints(List<Waypoint> waypoints) {
56 this.waypoints = waypoints;
57 this.unmodifiableWaypoints = Collections.unmodifiableList(waypoints);
61 public RouteImpl routeService(RouteServiceImpl rs) {
67 public String getName() {
72 public void setName(String name) {
74 fireEvent(RouteEvent.TYPE_ROUTE_RENAMED);
78 public boolean persisted() {
79 return backend != null;
83 public Waypoint createWaypoint(Object backend) {
84 if (backend instanceof Resource) {
85 Resource waypoint = (Resource) backend;
86 WaypointImpl wp = new WaypointImpl(waypoint, "");
88 // Read real label in background
89 ThreadUtils.getBlockingWorkExecutor().submit(() -> {
91 Waypoint p = Simantics.getSession().syncRequest(
92 (Read<Waypoint>) graph -> RoutePersistence.toWaypoint(graph, waypoint));
94 wp.setLabel(p.getLabel());
95 rs.fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED, RouteImpl.this);
97 } catch (DatabaseException e) {
98 LOGGER.error("Failed to read waypoint {} label", backend, e);
104 throw new IllegalArgumentException("only Resource type waypoints supported, got " + backend); //$NON-NLS-1$
108 public void addWaypoint(int index, Waypoint r) {
109 waypoints.add(index, r);
110 fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
114 public void removeWaypoint(Waypoint r) {
116 fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
120 public List<Waypoint> waypoints() {
121 return unmodifiableWaypoints;
125 public String toString() {
126 return String.format("%s@%x [%s, waypoints=%s]",
127 getClass().getName(),
128 System.identityHashCode(this),
130 waypoints.toString());
133 private void fireEvent(int type) {
135 rs.fireEvent(type, this);