View Javadoc

1   package org.glassbox.graphview;
2   
3   import de.fzi.wim.guibase.graphview.view.*;
4   import de.fzi.wim.guibase.graphview.graph.Edge;
5   import de.fzi.wim.guibase.graphview.graph.Node;
6   
7   import java.awt.*;
8   import java.awt.geom.*;
9   
10  /***
11     An abstract painter for CubicCurve edges with optional pointers, ideal for GraphViz layouted edges.
12   */
13  public abstract class AbstractCubicCurveEdgePainter extends AbstractEdgePainter implements ZoomableEdgePainter {
14      private CubicCurve2D _curve;
15      private Polygon _arrow;
16      private int _arrowwidth, _arrowlength;
17      private double _aw, _al;
18      private Point _p1, _cp1, _cp2, _p2;
19  
20      protected AbstractCubicCurveEdgePainter(int arrowlength, int arrowwidth) {
21  	_curve = new CubicCurve2D.Double();
22  	_p1 = new Point();
23  	_cp1 = new Point();
24  	_cp2 = new Point();
25  	_p2 = new Point();	
26  	_arrow = new Polygon();
27  	_arrowwidth = arrowwidth;
28  	_arrowlength = arrowlength;
29  	setZoomFactor(1d);
30      }
31  
32      public void setZoomFactor(double zf) {
33  	_al = (int)((double)_arrowlength * zf);
34  	_aw = (int)((double)_arrowwidth * zf);
35      }
36  
37      public void paintEdge(JGraphPane grp, Graphics2D g, Edge e) {
38  	Point2D[] cp = getControlPoints(e);
39  
40  	g.setColor(getEdgeColor(grp, e));
41  	g.setStroke(getEdgeStroke(grp, e));	
42  
43  	// paint the spline	
44  	for (int i=0; i+3<cp.length; i+=3) {
45  	    grp.graphToScreenPoint(cp[i], _p1);
46  	    grp.graphToScreenPoint(cp[i+1], _cp1);
47  	    grp.graphToScreenPoint(cp[i+2], _cp2);
48  	    grp.graphToScreenPoint(cp[i+3], _p2);
49  	    _curve.setCurve(_p1.x, _p1.y, _cp1.x, _cp1.y, _cp2.x, _cp2.y, _p2.x, _p2.y);
50  	    g.draw(_curve);
51  
52  	    /*
53  	    /// For Test
54  	    g.setColor(Color.red);
55  	    g.fillRect(_cp1.x-5, _cp1.y-5, 11, 11);
56  	    g.setColor(Color.green);
57  	    g.fillRect(_cp2.x-2, _cp2.y-2, 5, 5);
58  	    g.setColor(getEdgeColor(grp, e));
59  	    g.drawRect(_cp1.x-5, _cp1.y-5, 11, 11);
60  	    g.drawRect(_cp2.x-2, _cp2.y-2, 5, 5);
61  	    ///
62  	    */
63  	}
64  
65  	// paint the arrows
66  	g.setColor(getPointerColor(grp, e));
67  
68  	if (hasArrowAtStart(e)) {
69  	    Point2D p0 = cp[0];
70  	    Point2D ps = getPointOfArrowAtStart(e);
71  	    grp.graphToScreenPoint(p0, _p1);
72  	    grp.graphToScreenPoint(ps, _p2);
73  	    ArrowTools.getArrowShape(_p1, _p2, _al, _aw, _arrow);
74  	    g.fillPolygon(_arrow);
75  	}
76  
77  	if (hasArrowAtEnd(e)) {
78  	    Point2D pn = cp[cp.length-1];
79  	    Point2D pe = getPointOfArrowAtEnd(e);
80  	    grp.graphToScreenPoint(pn, _p1);
81  	    grp.graphToScreenPoint(pe, _p2);
82  	    ArrowTools.getArrowShape(_p1, _p2, _al, _aw, _arrow);
83  	    g.fillPolygon(_arrow);
84  	}
85  
86  	// paint the label
87  	g.setColor(getTextColor(grp, e));
88  	g.setFont(getFont(grp, e));
89  
90  	if (hasLabel(e)) {
91  	    Point2D lp = getLabelPosition(e);
92  	    grp.graphToScreenPoint(lp, _p1);
93  	    String en = getEdgeLabel(grp, e);
94  	    int lw = grp.getFontMetrics(g.getFont()).stringWidth(en);
95  	    int lh = grp.getFontMetrics(g.getFont()).getHeight();
96  	    int asc = grp.getFontMetrics(g.getFont()).getAscent();
97  	    int x = _p1.x - lw/2;
98  	    int y = _p1.y - lh/2 + asc;
99  	    g.drawString(en, x, y);    	    
100 	}
101     }
102 
103     public void getEdgeScreenBounds(JGraphPane grp, Edge edge, Rectangle scr) {
104 	Point2D[] cp = getControlPoints(edge);
105 
106 	boolean first = true;
107 	for (int i=0; i+3<cp.length; i+=3) {
108 	    grp.graphToScreenPoint(cp[i], _p1);
109 	    grp.graphToScreenPoint(cp[i+1], _cp1);
110 	    grp.graphToScreenPoint(cp[i+2], _cp2);
111 	    grp.graphToScreenPoint(cp[i+3], _p2);
112 	    _curve.setCurve(_p1.x, _p1.y, _cp1.x, _cp1.y, _cp2.x, _cp2.y, _p2.x, _p2.y);
113 	    if (first) {
114 		first = false;
115 		scr.setBounds(_curve.getBounds());
116 	    } else {
117 		scr.add(_curve.getBounds());
118 	    }
119 	}
120 
121 	if (hasArrowAtStart(edge)) {
122 	    Point2D p0 = cp[0];
123 	    Point2D ps = getPointOfArrowAtStart(edge);		
124 	    grp.graphToScreenPoint(p0, _p1);
125 	    grp.graphToScreenPoint(ps, _p2);
126 	    ArrowTools.getArrowShape(_p1, _p2, _al, _aw, _arrow);
127 	    scr.add(_arrow.getBounds());
128 	}
129 	    
130 	if (hasArrowAtEnd(edge)) {
131 	    Point2D pn = cp[cp.length-1];
132 	    Point2D pe = getPointOfArrowAtEnd(edge);
133 	    grp.graphToScreenPoint(pn, _p1);
134 	    grp.graphToScreenPoint(pe, _p2);
135 	    ArrowTools.getArrowShape(_p1, _p2, _al, _aw, _arrow);
136 	    scr.add(_arrow.getBounds());
137 	}
138 
139 	scr.x -= 1;
140 	scr.y -= 1;
141 	scr.width += 2;
142 	scr.height += 2;
143     }
144 
145     public double screenDistanceFromEdge(JGraphPane grp,Edge edge,Point point) {
146 	/// FIXME! TODO
147 	return super.screenDistanceFromEdge(grp, edge, point);
148     }
149 
150     protected abstract Point2D[] getControlPoints(Edge e);
151 
152     protected abstract boolean hasArrowAtStart(Edge e);
153 
154     protected abstract boolean hasArrowAtEnd(Edge e);
155 
156     protected abstract Point2D getPointOfArrowAtStart(Edge e);
157 
158     protected abstract Point2D getPointOfArrowAtEnd(Edge e);
159     
160     protected abstract boolean hasLabel(Edge e);
161     
162     protected abstract Point2D getLabelPosition(Edge e);
163 
164     protected abstract Color getEdgeColor(JGraphPane grp, Edge e);
165 
166     protected abstract Color getPointerColor(JGraphPane grp, Edge e);
167 
168     protected abstract Color getTextColor(JGraphPane grp, Edge e);
169 
170     protected abstract Font getFont(JGraphPane grp, Edge e);
171 
172     protected abstract Stroke getEdgeStroke(JGraphPane grp, Edge e);
173 
174     protected abstract String getEdgeLabel(JGraphPane grp, Edge e);
175 }