KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > nbprefuse > render > ReferenceEdgeRenderer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.nbprefuse.render;
21
22 import java.awt.BasicStroke JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.Polygon JavaDoc;
26 import java.awt.Shape JavaDoc;
27 import java.awt.geom.AffineTransform JavaDoc;
28 import java.awt.geom.CubicCurve2D JavaDoc;
29 import java.awt.geom.GeneralPath JavaDoc;
30 import java.awt.geom.Line2D JavaDoc;
31 import java.awt.geom.Point2D JavaDoc;
32 import java.awt.geom.Rectangle2D JavaDoc;
33
34 import prefuse.Constants;
35 import prefuse.render.AbstractShapeRenderer;
36 import prefuse.render.EdgeRenderer;
37 import prefuse.util.ColorLib;
38 import prefuse.util.GraphicsLib;
39 import prefuse.util.StrokeLib;
40 import prefuse.visual.EdgeItem;
41 import prefuse.visual.VisualItem;
42
43
44 /**
45  * JL 2Mar2006
46  * ReferenceEdgeRenderer extends EdgeRenderer so that a hollow
47  * arrowhead can be used. It has a hardcoded 12 pixel width and 14 pixel height.
48  * ReferenceEdgeRenderer uses a GeneralPath instead of the Polygon used by
49  * EdgeRenderer. To see the modifications, do a find on "2MAR2006".
50  *
51  * @author <a HREF="http://jheer.org">jeffrey heer</a>
52  * @author Jeri Lockhart>
53  */

54 public class ReferenceEdgeRenderer extends EdgeRenderer {
55
56     protected GeneralPath JavaDoc m_arrowHead = new GeneralPath JavaDoc(); // 2Mar2006
57

58
59     /**
60      * Create a new EdgeRenderer.
61      */

62     public ReferenceEdgeRenderer() {
63         super();
64         // wider and taller than default filled arrowhead
65
// 2Mar2006
66
m_arrowHead.moveTo(-6, -14);
67         m_arrowHead.lineTo(0,0);
68         m_arrowHead.lineTo(6,-14);
69     }
70     
71     
72     /**
73      * @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem)
74      */

75     protected Shape JavaDoc getRawShape(VisualItem item) {
76         EdgeItem edge = (EdgeItem)item;
77         VisualItem item1 = edge.getSourceItem();
78         VisualItem item2 = edge.getTargetItem();
79         
80         int type = m_edgeType;
81         
82         getAlignedPoint(m_tmpPoints[0], item1.getBounds(),
83                 m_xAlign1, m_yAlign1);
84         getAlignedPoint(m_tmpPoints[1], item2.getBounds(),
85                 m_xAlign2, m_yAlign2);
86         m_curWidth = (float)(m_width * getLineWidth(item));
87         
88         // create the arrow head, if needed
89
EdgeItem e = (EdgeItem)item;
90         if ( e.isDirected() && m_edgeArrow != Constants.EDGE_ARROW_NONE ) {
91             // get starting and ending edge endpoints
92
boolean forward = (m_edgeArrow == Constants.EDGE_ARROW_FORWARD);
93             Point2D JavaDoc start = null, end = null;
94             start = m_tmpPoints[forward?0:1];
95             end = m_tmpPoints[forward?1:0];
96             
97             // compute the intersection with the target bounding box
98
VisualItem dest = forward ? e.getTargetItem() : e.getSourceItem();
99             int i = GraphicsLib.intersectLineRectangle(start, end,
100                     dest.getBounds(), m_isctPoints);
101             if ( i > 0 ) end = m_isctPoints[0];
102             
103             // create the arrow head shape
104
AffineTransform JavaDoc at = getArrowTrans(start, end, m_curWidth);
105             m_curArrow = at.createTransformedShape(m_arrowHead);
106             
107             // Don't shorten the edge -- 2Mar2006
108

109             // update the endpoints for the edge shape
110
// need to bias this by arrow head size
111
// Point2D lineEnd = m_tmpPoints[forward?1:0];
112
// lineEnd.setLocation(0, -m_arrowHeight);
113
// at.transform(lineEnd, lineEnd);
114
} else {
115             m_curArrow = null;
116         }
117         
118         // create the edge shape
119
Shape JavaDoc shape = null;
120         double n1x = m_tmpPoints[0].getX();
121         double n1y = m_tmpPoints[0].getY();
122         double n2x = m_tmpPoints[1].getX();
123         double n2y = m_tmpPoints[1].getY();
124         switch ( type ) {
125             case Constants.EDGE_TYPE_LINE:
126                 m_line.setLine(n1x, n1y, n2x, n2y);
127                 shape = m_line;
128                 break;
129             case Constants.EDGE_TYPE_CURVE:
130                 getCurveControlPoints(edge, m_ctrlPoints,n1x,n1y,n2x,n2y);
131                 m_cubic.setCurve(n1x, n1y,
132                         m_ctrlPoints[0].getX(), m_ctrlPoints[0].getY(),
133                         m_ctrlPoints[1].getX(), m_ctrlPoints[1].getY(),
134                         n2x, n2y);
135                 shape = m_cubic;
136                 break;
137             default:
138                 throw new IllegalStateException JavaDoc("Unknown edge type");
139         }
140         
141         // return the edge shape
142
return shape;
143     }
144     
145     /**
146      * @see prefuse.render.Renderer#render(java.awt.Graphics2D, prefuse.visual.VisualItem)
147      */

148     public void render(Graphics2D JavaDoc g, VisualItem item) {
149         // call super.super.render = AbstractShapeRenderer.render()
150
Shape JavaDoc shape = getShape(item);
151         if (shape != null)
152             drawShape(g, item, shape);
153         
154         // don't call super.render() (EdgeRenderer.render()
155
// because it uses g.fill to render the default polygon arrowhead
156

157         // render the edge arrow head
158
if ( m_curArrow != null ) {
159             // 2MAR2006
160
// draw the arrowhead, don't fill it 2MAR2006
161
g.setPaint(ColorLib.getColor(item.getFillColor()));
162 // g.fill(m_curArrow);
163
g.draw(m_curArrow);
164         }
165     }
166
167
168     
169     
170 } // end of class ReferenceEdgeRenderer
171
Popular Tags