]> gerrit.simantics Code Review - simantics/district.git/blob
486294275aa554289e4f1d65bb2d15ae057e8515
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;\r
2 \r
3 import java.awt.Color;\r
4 import java.awt.Graphics2D;\r
5 import java.awt.geom.Path2D;\r
6 import java.awt.geom.Point2D;\r
7 import java.awt.geom.Rectangle2D;\r
8 import java.util.ArrayList;\r
9 import java.util.HashSet;\r
10 import java.util.Iterator;\r
11 import java.util.List;\r
12 import java.util.Set;\r
13 \r
14 import org.simantics.Simantics;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.WriteGraph;\r
17 import org.simantics.db.common.request.WriteRequest;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.diagram.ui.DiagramModelHints;\r
20 import org.simantics.district.network.ui.DNEdgeBuilder;\r
21 import org.simantics.district.network.ui.NetworkDrawingParticipant;\r
22 import org.simantics.g2d.canvas.Hints;\r
23 import org.simantics.g2d.canvas.IToolMode;\r
24 import org.simantics.g2d.diagram.IDiagram;\r
25 import org.simantics.scenegraph.g2d.G2DNode;\r
26 import org.simantics.scenegraph.g2d.events.EventTypes;\r
27 import org.simantics.scenegraph.g2d.events.MouseEvent;\r
28 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;\r
29 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
30 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
31 import org.simantics.scenegraph.utils.NodeUtil;\r
32 \r
33 public class NetworkDrawingNode extends G2DNode {\r
34 \r
35     private static final long serialVersionUID = -3475301184009620573L;\r
36     \r
37     private List<Point2D> nodes = new ArrayList<>();\r
38 \r
39     private Rectangle2D rect = new Rectangle2D.Double(10, 10, 10, 10);\r
40 \r
41     private Set<Path2D> paths = new HashSet<>();\r
42 \r
43     private Resource diagramResource;\r
44 \r
45     private boolean committed;\r
46 \r
47     private NetworkDrawingParticipant participant;\r
48 \r
49     private IDiagram diagram;\r
50     \r
51     @Override\r
52     public void init() {\r
53         super.init();\r
54         addEventHandler(this);\r
55     }\r
56     \r
57     public void setNetworkDrawingParticipant(NetworkDrawingParticipant participant) {\r
58         this.participant = participant;\r
59     }\r
60     \r
61     public void setDiagram(IDiagram diagram) {\r
62         if (diagram != null) {\r
63             this.diagram = diagram;\r
64             this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
65         }\r
66     }\r
67 \r
68     @Override\r
69     public void render(Graphics2D g2d) {\r
70         Color old = g2d.getColor();\r
71         g2d.setColor(Color.BLUE);\r
72         \r
73         paths.forEach(p -> {\r
74             g2d.draw(p);\r
75         });\r
76         \r
77         g2d.setColor(old);\r
78     }\r
79 \r
80     @Override\r
81     public Rectangle2D getBoundsInLocal() {\r
82         return rect.getBounds2D();\r
83     }\r
84     \r
85     @Override\r
86     public int getEventMask() {\r
87         return EventTypes.MouseMask;\r
88     }\r
89     \r
90     @Override\r
91     protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) {\r
92         // nodes to path2d\r
93         IToolMode mode = getToolMode();\r
94         if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {\r
95             Point2D start = null;\r
96             Point2D end = null;\r
97             Iterator<Point2D> nodeIter = nodes.iterator();\r
98             while (nodeIter.hasNext()) {\r
99                 if (end == null) {\r
100                     start = nodeIter.next();\r
101                 } else {\r
102                     start = end;\r
103                 }\r
104                 end = nodeIter.next();\r
105                 \r
106                 createEdge(start, end);\r
107             }\r
108             \r
109             nodes.clear();\r
110             committed = true;\r
111             \r
112             repaint();\r
113             \r
114             return true;\r
115         }\r
116         return super.mouseDoubleClicked(e);\r
117     }\r
118     \r
119     private void createEdge(Point2D start, Point2D end) {\r
120         double[] startCoords = new double[] { start.getX(), start.getY() };\r
121         double[] endCoords = new double[] { end.getX(), end.getY() };\r
122         \r
123         DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);\r
124         Simantics.getSession().asyncRequest(new WriteRequest() {\r
125             \r
126             @Override\r
127             public void perform(WriteGraph graph) throws DatabaseException {\r
128                 builder.create(graph, startCoords, endCoords);\r
129             }\r
130         });\r
131         \r
132     }\r
133     \r
134     @Override\r
135     protected boolean mouseClicked(MouseClickEvent e) {\r
136         // check ToolMode\r
137         IToolMode mode = getToolMode();\r
138         if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {\r
139             if (committed) {\r
140                 committed = false;\r
141                 return false;\r
142             }\r
143             Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());\r
144             nodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));\r
145         }\r
146         return super.mouseClicked(e);\r
147     }\r
148     \r
149     private IToolMode getToolMode() {\r
150         return participant.getHint(Hints.KEY_TOOL);\r
151     }\r
152 \r
153     @Override\r
154     protected boolean mouseMoved(MouseMovedEvent e) {\r
155         return super.mouseMoved(e);\r
156     }\r
157 }