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 Waypoint createWaypoint(Object backend) {
79 if (backend instanceof Resource) {
80 Resource waypoint = (Resource) backend;
81 WaypointImpl wp = new WaypointImpl(waypoint, "");
83 // Read real label in background
84 ThreadUtils.getBlockingWorkExecutor().submit(() -> {
86 Waypoint p = Simantics.getSession().syncRequest(
87 (Read<Waypoint>) graph -> RoutePersistence.toWaypoint(graph, waypoint));
89 wp.setLabel(p.getLabel());
90 rs.fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED, RouteImpl.this);
92 } catch (DatabaseException e) {
93 LOGGER.error("Failed to read waypoint {} label", backend, e);
99 throw new IllegalArgumentException("only Resource type waypoints supported, got " + backend); //$NON-NLS-1$
103 public void addWaypoint(int index, Waypoint r) {
104 waypoints.add(index, r);
105 fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
109 public void removeWaypoint(Waypoint r) {
111 fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
115 public List<Waypoint> waypoints() {
116 return unmodifiableWaypoints;
120 public String toString() {
121 return String.format("%s@%x [%s, waypoints=%s]",
122 getClass().getName(),
123 System.identityHashCode(this),
125 waypoints.toString());
128 private void fireEvent(int type) {
130 rs.fireEvent(type, this);