]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.route/src/org/simantics/district/route/internal/RouteImpl.java
3619b602143a4c0be0c7c897df625acd3512829e
[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.db.Resource;
8 import org.simantics.district.route.Route;
9 import org.simantics.district.route.RouteEvent;
10 import org.simantics.district.route.Waypoint;
11
12 public class RouteImpl implements Route {
13
14     private String name;
15     private Resource model;
16     private Resource backend;
17
18     private RouteServiceImpl rs;
19
20     private List<Waypoint> waypoints;
21     private List<Waypoint> unmodifiableWaypoints;
22
23     public RouteImpl(String name) {
24         this.name = name;
25         routeService(rs);
26         waypoints(new ArrayList<>());
27     }
28
29     public RouteImpl modelEntity(Resource model) {
30         this.model = model;
31         return this;
32     }
33
34     public Resource modelEntity() {
35         return model;
36     }
37
38     public RouteImpl backend(Resource backend) {
39         this.backend = backend;
40         return this;
41     }
42
43     public Resource backend() {
44         return backend;
45     }
46
47     public RouteImpl waypoints(List<Waypoint> waypoints) {
48         this.waypoints = waypoints;
49         this.unmodifiableWaypoints = Collections.unmodifiableList(waypoints);
50         return this;
51     }
52
53     public RouteImpl routeService(RouteServiceImpl rs) {
54         this.rs = rs;
55         return this;
56     }
57
58     @Override
59     public String getName() {
60         return name;
61     }
62
63     @Override
64     public void setName(String name) {
65         this.name = name;
66         fireEvent(RouteEvent.TYPE_ROUTE_RENAMED);
67     }
68
69     @Override
70     public Waypoint createWaypoint(Object backend) {
71         if (backend instanceof Resource)
72             return new WaypointImpl((Resource) backend, "Point 1");
73         throw new IllegalArgumentException("only Resource type waypoints supported, got " + backend); //$NON-NLS-1$
74     }
75
76     @Override
77     public void addWaypoint(int index, Waypoint r) {
78         waypoints.add(index, r);
79         fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
80     }
81
82     @Override
83     public void removeWaypoint(Waypoint r) {
84         waypoints.remove(r);
85         fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
86     }
87
88     @Override
89     public List<Waypoint> waypoints() {
90         return unmodifiableWaypoints;
91     }
92
93     @Override
94     public String toString() {
95         return String.format("%s@%x [%s, waypoints=%s]",
96                 getClass().getName(),
97                 System.identityHashCode(this),
98                 name,
99                 waypoints.toString());
100     }
101
102     private void fireEvent(int type) {
103         if (rs != null)
104             rs.fireEvent(type, this);
105     }
106
107 }