]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.route/src/org/simantics/district/route/internal/RouteImpl.java
Merge "Initial version of the district network Routes view."
[simantics/district.git] / org.simantics.district.route / src / org / simantics / district / route / internal / RouteImpl.java
diff --git a/org.simantics.district.route/src/org/simantics/district/route/internal/RouteImpl.java b/org.simantics.district.route/src/org/simantics/district/route/internal/RouteImpl.java
new file mode 100644 (file)
index 0000000..3619b60
--- /dev/null
@@ -0,0 +1,107 @@
+package org.simantics.district.route.internal;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.simantics.db.Resource;
+import org.simantics.district.route.Route;
+import org.simantics.district.route.RouteEvent;
+import org.simantics.district.route.Waypoint;
+
+public class RouteImpl implements Route {
+
+    private String name;
+    private Resource model;
+    private Resource backend;
+
+    private RouteServiceImpl rs;
+
+    private List<Waypoint> waypoints;
+    private List<Waypoint> unmodifiableWaypoints;
+
+    public RouteImpl(String name) {
+        this.name = name;
+        routeService(rs);
+        waypoints(new ArrayList<>());
+    }
+
+    public RouteImpl modelEntity(Resource model) {
+        this.model = model;
+        return this;
+    }
+
+    public Resource modelEntity() {
+        return model;
+    }
+
+    public RouteImpl backend(Resource backend) {
+        this.backend = backend;
+        return this;
+    }
+
+    public Resource backend() {
+        return backend;
+    }
+
+    public RouteImpl waypoints(List<Waypoint> waypoints) {
+        this.waypoints = waypoints;
+        this.unmodifiableWaypoints = Collections.unmodifiableList(waypoints);
+        return this;
+    }
+
+    public RouteImpl routeService(RouteServiceImpl rs) {
+        this.rs = rs;
+        return this;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public void setName(String name) {
+        this.name = name;
+        fireEvent(RouteEvent.TYPE_ROUTE_RENAMED);
+    }
+
+    @Override
+    public Waypoint createWaypoint(Object backend) {
+        if (backend instanceof Resource)
+            return new WaypointImpl((Resource) backend, "Point 1");
+        throw new IllegalArgumentException("only Resource type waypoints supported, got " + backend); //$NON-NLS-1$
+    }
+
+    @Override
+    public void addWaypoint(int index, Waypoint r) {
+        waypoints.add(index, r);
+        fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
+    }
+
+    @Override
+    public void removeWaypoint(Waypoint r) {
+        waypoints.remove(r);
+        fireEvent(RouteEvent.TYPE_ROUTE_MODIFIED);
+    }
+
+    @Override
+    public List<Waypoint> waypoints() {
+        return unmodifiableWaypoints;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("%s@%x [%s, waypoints=%s]",
+                getClass().getName(),
+                System.identityHashCode(this),
+                name,
+                waypoints.toString());
+    }
+
+    private void fireEvent(int type) {
+        if (rs != null)
+            rs.fireEvent(type, this);
+    }
+
+}