glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
}
- public static Optional<Resource> create(WriteGraph graph, Resource diagramResource, double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException {
+ public static Optional<Resource> create(WriteGraph graph, Resource diagramResource, double[] start, double startElevation, double[] end, double endElevation, double[] detailedGeometryCoords, double padding) throws DatabaseException {
Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
double halfPadding = padding / 2;
Envelope e = new Envelope(x1, x2, y1, y2);
vv.insert(e, new ResourceVertex(vertex, coords, false));
}
- return create(graph, vv, diagramResource, null, start, startElevation, end, endElevation, padding, false);
+ return create(graph, vv, diagramResource, null, start, startElevation, end, endElevation, detailedGeometryCoords, padding, false);
}
- public static Optional<Resource> create(WriteGraph graph, Quadtree vertices, Resource diagramResource, Resource mapping, double[] start, double startElevation, double[] end, double endElevation, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException {
-
+ public static Optional<Resource> create(WriteGraph graph, Quadtree vertices, Resource diagramResource, Resource mapping, double[] start, double startElevation, double[] end, double endElevation, double[] detailedGeometryCoords, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException {
DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
// 2. Add vertices
}
// 1. Get diagram edge to construct
- Resource edge = getOrCreateEdge(graph, diagramResource, mapping);
+ Resource edge = getOrCreateEdge(graph, diagramResource, mapping, detailedGeometryCoords);
if (writeElevationToEdgeFromPoints) {
graph.claimLiteral(edge, DN.Edge_HasElevation, calculateElevationFromVertices(graph, startVertex, endVertex), Bindings.DOUBLE);
return 0;
}
- public void create(WriteGraph graph, double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException {
-
- Optional<Resource> edge = create(graph, diagramResource, start, startElevation, end, endElevation, padding);
+ public void create(WriteGraph graph, double[] start, double startElevation, double[] end, double endElevation, double[] detailedGeometryCoords, double padding) throws DatabaseException {
+ Optional<Resource> edge = create(graph, diagramResource, start, startElevation, end, endElevation, detailedGeometryCoords, padding);
// 7. Put the element on all the currently active layers if possible.
if (glm != null) {
putOnActiveLayer(graph, edge.get());
return vertex;
}
- private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping) throws DatabaseException {
- return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping);
+ private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping, double[] detailedGeometryCoords) throws DatabaseException {
+ return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping, detailedGeometryCoords);
}
public static class ResourceVertex {
import org.simantics.db.WriteGraph;
import org.simantics.db.common.request.WriteRequest;
import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Write;
import org.simantics.diagram.elements.DiagramNodeUtil;
import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.district.network.DistrictNetworkUtil;
import org.simantics.district.network.ModelledCRS;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
import org.simantics.district.network.ui.DNEdgeBuilder;
import org.simantics.district.network.ui.NetworkDrawingParticipant;
import org.simantics.g2d.canvas.Hints;
public class NetworkDrawingNode extends G2DNode {
+ static class DrawingNode {
+
+ private List<Point2D> routeNodes = new ArrayList<>();
+ }
+
private static final long serialVersionUID = -3475301184009620573L;
private Point2D currentMousePos = null;
-
- private List<Point2D> nodes = new ArrayList<>();
+
+ private List<DrawingNode> nodes = new ArrayList<>();
+ private DrawingNode currentRouteNode = null;
private Resource diagramResource;
public void render(Graphics2D g2d) {
if (nodes.isEmpty())
return;
-
- Path2D path = new Path2D.Double();
- Iterator<Point2D> nodeIter = nodes.iterator();
- if (nodeIter.hasNext()) {
- Point2D node = nodeIter.next();
- path.moveTo(node.getX(), node.getY());
- }
- while (nodeIter.hasNext()) {
- Point2D node = nodeIter.next();
- path.lineTo(node.getX(), node.getY());
- }
- if (currentMousePos != null)
- path.lineTo(currentMousePos.getX(), currentMousePos.getY());
-
+
Color old = g2d.getColor();
Stroke oldStroke = g2d.getStroke();
-
- if (DASHED_STROKE != null) {
- if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
- BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
- g2d.setStroke(bs);
- } else {
- g2d.setStroke(DASHED_STROKE);
+
+ Iterator<DrawingNode> dnodeIterator = nodes.iterator();
+ while (dnodeIterator.hasNext()) {
+ Path2D path = new Path2D.Double();
+ DrawingNode dnode = dnodeIterator.next();
+ Iterator<Point2D> nodeIter = dnode.routeNodes.iterator();
+ if (nodeIter.hasNext()) {
+ Point2D node = nodeIter.next();
+ path.moveTo(node.getX(), node.getY());
}
- }
-
- g2d.setColor(BLUE_ALPHA);
+ while (nodeIter.hasNext()) {
+ Point2D node = nodeIter.next();
+ path.lineTo(node.getX(), node.getY());
+ }
+ if (!dnodeIterator.hasNext()) {
+ if (currentMousePos != null)
+ path.lineTo(currentMousePos.getX(), currentMousePos.getY());
+ }
+
+ if (DASHED_STROKE != null) {
+ if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
+ BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
+ g2d.setStroke(bs);
+ } else {
+ g2d.setStroke(DASHED_STROKE);
+ }
+ }
+
+ g2d.setColor(BLUE_ALPHA);
- g2d.draw(path);
+ g2d.draw(path);
+ }
g2d.setStroke(oldStroke);
g2d.setColor(old);
// nodes to path2d
IToolMode mode = getToolMode();
if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
- Point2D start = null;
- Point2D end = null;
- Iterator<Point2D> nodeIter = nodes.iterator();
- while (nodeIter.hasNext()) {
- if (end == null) {
- start = nodeIter.next();
- if (!nodeIter.hasNext()) {
- break;
+ // ok, new routenode starts from here
+ Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
+ Point2D.Double pos = new Point2D.Double(localPos.getX(), localPos.getY());
+ if (currentRouteNode != null) {
+ //currentRouteNode.routeNodes.add(pos);
+ currentRouteNode = new DrawingNode();
+ currentRouteNode.routeNodes.add(pos);
+ nodes.add(currentRouteNode);
+ } else {
+ // ok, this must be creation of dh_point
+ double scale = getTransform().getScaleY();
+ double x = ModelledCRS.xToLongitude(pos.getX() / scale);
+ double y = ModelledCRS.yToLatitude(-pos.getY() / scale);
+ Simantics.getSession().asyncRequest(new Write() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ graph.markUndoPoint();
+ Resource defaultMapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
+ DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, 0, defaultMapping); // TODO: elevation can be fetched from e.g. elevation API
}
- } else {
- start = end;
- }
-
- end = nodeIter.next();
-
- createEdge(start, end);
+ });
}
-
- nodes.clear();
- committed = true;
-
repaint();
-
return true;
}
return super.mouseDoubleClicked(e);
}
- private void createEdge(Point2D start, Point2D end) {
+ private void createEdge(DrawingNode node) {
+
+ Point2D start = node.routeNodes.get(0);
+ Point2D end = node.routeNodes.get(node.routeNodes.size() - 1);
double currentPadding = DistrictNetworkVertexNode.width;
AffineTransform test = getTransform();
double[] startCoords = new double[] { startLon, startLat };
double[] endCoords = new double[] { endLon, endLat };
+ double[] detailedGeometryCoords = new double[node.routeNodes.size() * 2];
+ int i = 0;
+ for (Point2D p : node.routeNodes) {
+ double lat = ModelledCRS.yToLatitude(-p.getY() / scaleY);
+ double lon = ModelledCRS.xToLongitude(p.getX() / scaleX);
+ detailedGeometryCoords[i++] = lon;
+ detailedGeometryCoords[i++] = lat;
+ }
+
DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);
Simantics.getSession().asyncRequest(new WriteRequest() {
@Override
public void perform(WriteGraph graph) throws DatabaseException {
- builder.create(graph, startCoords, 0, endCoords, 0, padding);
+ builder.create(graph, startCoords, 0, endCoords, 0, detailedGeometryCoords, padding);
}
});
nodes.remove(nodes.size() - 1);
} else if (e.button == MouseEvent.LEFT_BUTTON) {
Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
- nodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
+ if (currentRouteNode == null && canStartEdge(localPos)) {
+ // ok, we can start from here
+ currentRouteNode = new DrawingNode();
+ currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
+ nodes.add(currentRouteNode);
+ } else if (currentRouteNode != null && canStartEdge(localPos)) {
+ // let's commit our new routenode
+ currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
+ Iterator<DrawingNode> nodeIter = nodes.iterator();
+ while (nodeIter.hasNext()) {
+ createEdge(nodeIter.next());
+ }
+ currentRouteNode = null;
+ nodes.clear();
+ committed = true;
+ } else if (currentRouteNode != null) {
+ currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
+ }
}
repaint();
return true;
}
return super.mouseClicked(e);
}
-
+
+ private boolean canStartEdge(Point2D currentPos) {
+ return participant.isHoveringOverNode(currentPos);
+ }
+
private IToolMode getToolMode() {
return participant.getHint(Hints.KEY_TOOL);
}
@Override
protected boolean keyPressed(KeyPressedEvent e) {
if (e.keyCode == java.awt.event.KeyEvent.VK_ESCAPE) {
+ currentRouteNode = null;
nodes.clear();
repaint();
return true;