View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwui.graphview;
7   
8   import de.fzi.wim.guibase.graphview.graph.Edge;
9   import de.fzi.wim.guibase.graphview.view.EdgePainter;
10  import de.fzi.wim.guibase.graphview.view.JGraphPane;
11  import net.kwfgrid.gwui.ZoomGroup;
12  import org.glassbox.Theme;
13  import org.glassbox.graphview.*;
14  import org.glassbox.gui.Group;
15  import org.glassbox.gui.Member;
16  
17  import java.awt.*;
18  import java.awt.geom.Point2D;
19  
20  ///
21  /// FIXME: TODO: Refactor to ZoomableEdgePainter and delegate listening for zoom-lens changes to another class(?)
22  ///
23  
24  /***
25   * The painter for <code>ArcEdges</code>.<br>
26   * This painter has the following properties which can be configured via the application's theme:
27   * <pre><ul>
28   * <li>kwfgrid.ArcEdgePainter.arrow.size</li>
29   * <li>kwfgrid.ArcEdgePainter.text.color</li>
30   * <li>kwfgrid.ArcEdgePainter.font</li>
31   * <li>kwfgrid.ArcEdgePainter.color</li>
32   * <li>kwfgrid.ArcEdgePainter.stroke</li>
33   * </ul></pre>
34   * The size property of the arrow assumes an upright arrow.
35   */
36  public class ArcEdgePainter implements Member, EdgePainter {
37      public static final String IDENTIFIER = "kwfgrid.ArcEdgePainter";
38  
39      protected static final String ARROW_SIZE_KEY = "kwfgrid.ArcEdgePainter.arrow.size";
40      protected static final String COLOR_KEY = "kwfgrid.ArcEdgePainter.color";
41      protected static final String TEXT_COLOR_KEY = "kwfgrid.ArcEdgePainter.text.color";
42      protected static final String FONT_KEY = "kwfgrid.ArcEdgePainter.font";
43      protected static final String STROKE_KEY = "kwfgrid.ArcEdgePainter.stroke";
44      protected static final Dimension ARROW_SIZE = Theme.getSize(ARROW_SIZE_KEY);
45      protected static final Color COLOR = Theme.getColor(COLOR_KEY);
46      protected static final Color TEXT_COLOR = Theme.getColor(TEXT_COLOR_KEY);
47      protected static final Font FONT = Theme.getFont(FONT_KEY);
48      protected static final Stroke STROKE = Theme.getStroke(STROKE_KEY);
49  
50      private ZoomGroup _group;
51      private boolean _paintlabels;
52      private int _labellength;
53  
54      // Painter for edges which have not been layouted by graphviz.
55      private ZoomableEdgePainter _linepainter = new AbstractArrowEdgePainter(ARROW_SIZE.height, ARROW_SIZE.width) {
56          protected Color getEdgeColor(JGraphPane gpane, Edge edge) {
57              return COLOR;
58          }
59  
60          protected Color getPointerColor(JGraphPane gpane, Edge edge) {
61              return COLOR;
62          }
63  
64          protected Color getTextColor(JGraphPane gpane, Edge edge) {
65              return TEXT_COLOR;
66          }
67  
68          protected Font getFont(JGraphPane gpane, Edge edge) {
69              return FONT;
70          }
71  
72          protected Stroke getEdgeStroke(JGraphPane gpane, Edge edge) {
73              return STROKE;
74          }
75  
76          protected String getEdgeLabel(JGraphPane grp, Edge e) {
77              String l = _paintlabels ? ((ArcEdge) e).getLabel() : "";
78              if (l.length() > _labellength) {
79                  l = l.substring(0, _labellength) + "...";
80              }
81              return l;
82          }
83      };
84  
85      private ZoomableEdgePainter _readlinepainter = new AbstractArrowReadEdgePainter(ARROW_SIZE.height, ARROW_SIZE.width) {
86          protected Color getEdgeColor(JGraphPane gpane, Edge edge) {
87              return COLOR;
88          }
89  
90          protected Color getPointerColor(JGraphPane gpane, Edge edge) {
91              return COLOR;
92          }
93  
94          protected Color getTextColor(JGraphPane gpane, Edge edge) {
95              return TEXT_COLOR;
96          }
97  
98          protected Font getFont(JGraphPane gpane, Edge edge) {
99              return FONT;
100         }
101 
102         protected Stroke getEdgeStroke(JGraphPane gpane, Edge edge) {
103             return STROKE;
104         }
105 
106         protected String getEdgeLabel(JGraphPane grp, Edge e) {
107             String l = _paintlabels ? ((ArcEdge) e).getLabel() : "";
108             if (l.length() > _labellength) {
109                 l = l.substring(0, _labellength) + "...";
110             }
111             return l;
112         }
113     };
114 
115     // Painter for edges which have been layouted by graphviz.
116     private ZoomableEdgePainter _cubiccurvepainter = new AbstractCubicCurveEdgePainter(ARROW_SIZE.height, ARROW_SIZE.width) {
117         protected Point2D[] getControlPoints(Edge e) {
118             return ((ArcEdge) e).getControlPoints();
119         }
120 
121         protected boolean hasArrowAtStart(Edge e) {
122             return ((ArcEdge) e).hasArrowAtStart();
123         }
124 
125         protected boolean hasArrowAtEnd(Edge e) {
126             return ((ArcEdge) e).hasArrowAtEnd();
127         }
128 
129         protected Point2D getPointOfArrowAtStart(Edge e) {
130             return ((ArcEdge) e).getPointOfArrowAtStart();
131         }
132 
133         protected Point2D getPointOfArrowAtEnd(Edge e) {
134             return ((ArcEdge) e).getPointOfArrowAtEnd();
135         }
136 
137         protected boolean hasLabel(Edge e) {
138             return ((ArcEdge) e).hasLabel();
139         }
140 
141         protected Point2D getLabelPosition(Edge e) {
142             return ((ArcEdge) e).getLabelPosition();
143         }
144 
145         protected Color getEdgeColor(JGraphPane gpane, Edge edge) {
146             return COLOR;
147         }
148 
149         protected Color getPointerColor(JGraphPane gpane, Edge edge) {
150             return COLOR;
151         }
152 
153         protected Color getTextColor(JGraphPane gpane, Edge edge) {
154             return TEXT_COLOR;
155         }
156 
157         protected Font getFont(JGraphPane gpane, Edge edge) {
158             return FONT;
159         }
160 
161         protected Stroke getEdgeStroke(JGraphPane gpane, Edge edge) {
162             return STROKE;
163         }
164 
165         protected String getEdgeLabel(JGraphPane grp, Edge e) {
166             String l = _paintlabels ? ((ArcEdge) e).getLabel() : "";
167             if (l.length() > _labellength) {
168                 l = l.substring(0, _labellength) + "...";
169             }
170             return l;
171         }
172     };
173 
174     private ZoomableEdgePainter _readcubiccurvepainter = new AbstractCubicCurveReadEdgePainter(ARROW_SIZE.height, ARROW_SIZE.width) {
175         protected Point2D[] getControlPoints(Edge e) {
176             return ((ArcEdge) e).getControlPoints();
177         }
178 
179         protected boolean hasArrowAtStart(Edge e) {
180             return ((ArcEdge) e).hasArrowAtStart();
181         }
182 
183         protected boolean hasArrowAtEnd(Edge e) {
184             return ((ArcEdge) e).hasArrowAtEnd();
185         }
186 
187         protected Point2D getPointOfArrowAtStart(Edge e) {
188             return ((ArcEdge) e).getPointOfArrowAtStart();
189         }
190 
191         protected Point2D getPointOfArrowAtEnd(Edge e) {
192             return ((ArcEdge) e).getPointOfArrowAtEnd();
193         }
194 
195         protected boolean hasLabel(Edge e) {
196             return ((ArcEdge) e).hasLabel();
197         }
198 
199         protected Point2D getLabelPosition(Edge e) {
200             return ((ArcEdge) e).getLabelPosition();
201         }
202 
203         protected Color getEdgeColor(JGraphPane gpane, Edge edge) {
204             return COLOR;
205         }
206 
207         protected Color getPointerColor(JGraphPane gpane, Edge edge) {
208             return COLOR;
209         }
210 
211         protected Color getTextColor(JGraphPane gpane, Edge edge) {
212             return TEXT_COLOR;
213         }
214 
215         protected Font getFont(JGraphPane gpane, Edge edge) {
216             return FONT;
217         }
218 
219         protected Stroke getEdgeStroke(JGraphPane gpane, Edge edge) {
220             return STROKE;
221         }
222 
223         protected String getEdgeLabel(JGraphPane grp, Edge e) {
224             String l = _paintlabels ? ((ArcEdge) e).getLabel() : "";
225             if (l.length() > _labellength) {
226                 l = l.substring(0, _labellength) + "...";
227             }
228             return l;
229         }
230     };
231 
232     public ArcEdgePainter() {
233         _group = null;
234         updateArrowSize();
235     }
236 
237     ///
238     /// Private API
239     /// ....................................................................................................
240 
241     private void updateArrowSize() {
242         double zf = 1d;
243         if (_group != null) {
244             zf = ((Double) _group.getProperty(ZoomGroup.ZOOM_FACTOR_KEY)).doubleValue();
245         }
246         _cubiccurvepainter.setZoomFactor(zf);
247         _readcubiccurvepainter.setZoomFactor(zf);
248         _linepainter.setZoomFactor(zf);
249         _readlinepainter.setZoomFactor(zf);
250         _paintlabels = zf > 0.5d;
251         _labellength = (int) (zf * 15d + 10);
252     }
253 
254     private EdgePainter getState(Edge e) {
255         ArcEdge ae = (ArcEdge) e;
256         if (ae instanceof ReadEdge) {
257             if (ae.getControlPoints() != null) {
258                 return _readcubiccurvepainter;
259             } else {
260                 return _readlinepainter;
261             }
262         } else {
263             if (ae.getControlPoints() != null) {
264                 return _cubiccurvepainter;
265             } else {
266                 return _linepainter;
267             }
268         }
269     }
270 
271     ///
272     /// Implementation of Member
273     /// ....................................................................................................
274 
275     public Group getGroup() {
276         return _group;
277     }
278 
279     public String getIdentifier() {
280         return IDENTIFIER;
281     }
282 
283     public void groupPropertyChanged(String name, Object oldvalue, Object newvalue) {
284         if (ZoomGroup.ZOOM_FACTOR_KEY.equals(name)) {
285             updateArrowSize();
286         }
287     }
288 
289     public void setGroup(Group group) throws IllegalArgumentException {
290         if (!(group == null || group instanceof ZoomGroup))
291             throw new IllegalArgumentException("ArcEdgePainter must be member of a ZoomGroup.");
292 
293         _group = (ZoomGroup) group;
294 
295         updateArrowSize();
296     }
297 
298     ///
299     /// Implementation of EdgePainter
300     /// ....................................................................................................
301 
302     public void paintEdge(JGraphPane grp, Graphics2D g, Edge edge) {
303         getState(edge).paintEdge(grp, g, edge);
304     }
305 
306     public void getEdgeScreenBounds(JGraphPane grp, Edge edge, Rectangle scr) {
307         getState(edge).getEdgeScreenBounds(grp, edge, scr);
308     }
309 
310     public double screenDistanceFromEdge(JGraphPane grp, Edge edge, Point point) {
311         return getState(edge).screenDistanceFromEdge(grp, edge, point);
312     }
313 }