1
28 package demo.view.mindmap;
29
30 import y.base.Edge;
31 import y.base.EdgeCursor;
32 import y.base.Node;
33 import y.base.NodeCursor;
34 import y.base.YList;
35 import y.util.Cursors;
36 import y.view.EdgeRealizer;
37 import y.view.Graph2D;
38 import y.view.Graph2DTraversal;
39 import y.view.NodeLabel;
40 import y.view.NodeRealizer;
41 import y.view.OrderRenderer;
42
43 import java.awt.Graphics2D;
44 import java.awt.Rectangle;
45 import java.awt.geom.Rectangle2D;
46 import java.util.Iterator;
47
48
58 class MindMapRenderer implements OrderRenderer {
59
62 private final Graph2DTraversal order;
63
64
67 MindMapRenderer() {
68 this.order = new MindMapTraversal();
69 }
70
71
75 public Graph2DTraversal getPaintOrder() {
76 return order;
77 }
78
79
83 public Graph2DTraversal getSloppyPaintOrder() {
84 return order;
85 }
86
87
92 public void paint( final Graphics2D gfx, final Graph2D graph) {
93 paintCore(gfx, graph, false);
94 }
95
96
101 public void paintSloppy( final Graphics2D gfx, final Graph2D graph ) {
102 paintCore(gfx, graph, true);
103 }
104
105
112 private void paintCore(
113 final Graphics2D gfx, final Graph2D graph, final boolean sloppy
114 ) {
115 Rectangle clip = gfx.getClipBounds();
116 if (clip == null) {
117 clip = graph.getBoundingBox();
118 }
119 final double cx = clip.getX();
120 final double cy = clip.getY();
121 final double cw = clip.getWidth();
122 final double ch = clip.getHeight();
123
124 final Rectangle2D.Double tmp = new Rectangle2D.Double();
125 final int types =
126 Graph2DTraversal.NODES |
127 Graph2DTraversal.EDGES |
128 Graph2DTraversal.NODE_LABELS;
129 for (Iterator it = order.firstToLast(graph, types); it.hasNext();) {
130 final Object o = it.next();
131
132 if (o instanceof Node) {
133 final NodeRealizer nr = graph.getRealizer((Node) o);
134 tmp.setFrame(0, 0, -1, -1);
136 nr.calcUnionRect(tmp);
137 if (tmp.intersects(cx, cy, cw, ch)) {
138 if (sloppy) {
140 nr.paintSloppy(gfx);
141 } else {
142 nr.paint(gfx);
143 }
144 }
145 } else if (o instanceof Edge) {
146 final EdgeRealizer er = graph.getRealizer((Edge) o);
147 if (er.intersects(clip)) {
149 if (sloppy) {
151 er.paintSloppy(gfx);
152 } else {
153 er.paint(gfx);
154 }
155 }
156 } else if (o instanceof NodeLabel) {
157 final NodeLabel nl = (NodeLabel) o;
158 if (nl.intersects(cx, cy, cw, ch)) {
160 nl.paint(gfx);
162 }
163 }
164 }
165 }
166
167
168
171 private static final class MindMapTraversal implements Graph2DTraversal {
172
175 public Iterator firstToLast(Graph2D graph, int elementTypes) {
176 return collectElements(graph, elementTypes).iterator();
177 }
178
179
182 public Iterator lastToFirst(Graph2D graph, int elementTypes) {
183 final YList list = collectElements(graph, elementTypes);
184 return Cursors.createReverseIterator(list.cursor());
185 }
186
187 private YList collectElements( final Graph2D graph, final int types ) {
189 final YList elements = new YList();
190 final YList crossReferences = new YList();
191 final YList bends = new YList();
192
193 final boolean addEdges = (Graph2DTraversal.EDGES & types) != 0;
195 final boolean addBends = (Graph2DTraversal.BENDS & types) != 0;
196 if (addEdges || addBends) {
197 final ViewModel model = ViewModel.instance;
198 for (EdgeCursor ec = graph.edges(); ec.ok(); ec.next()) {
199 if (model.isCrossReference(ec.edge())) {
201 if (addEdges) {
202 crossReferences.add(ec.edge());
203 }
204 final EdgeRealizer er = graph.getRealizer(ec.edge());
205 if (addBends) {
206 bends.addAll(er.bends());
207 }
208 } else {
209 if (addEdges) {
210 elements.add(ec.edge());
211 }
212 }
213 }
214 }
215
216
217 final YList nodeLabels = new YList();
218
219 final boolean addNodes = (Graph2DTraversal.NODES & types) != 0;
221 final boolean addNodeLabels = (Graph2DTraversal.NODE_LABELS & types) != 0;
222 if (addNodes || addNodeLabels) {
223 for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
224 if (addNodeLabels) {
226 final NodeRealizer nr = graph.getRealizer(nc.node());
227 for (int i = 0, n = nr.labelCount(); i < n; ++i) {
228 nodeLabels.add(nr.getLabel(i));
229 }
230 }
231 if (addNodes) {
232 elements.add(nc.node());
233 }
234 }
235 }
236
237 elements.splice(crossReferences);
239 elements.splice(bends);
241 elements.splice(nodeLabels);
243 return elements;
244 }
245 }
246 }
247