View Javadoc

1   package org.glassbox.graphview;
2   
3   import java.awt.*;
4   
5   import de.fzi.wim.guibase.graphview.graph.*;
6   import de.fzi.wim.guibase.graphview.view.*;
7   
8   /***
9      A decorator for a NodePainter which paints an additional border around the node.
10   */
11  public class NodeBorder implements ZoomableNodePainter {
12      private Color _border;
13      private Paint _background;
14      private Stroke _stroke;
15      private Dimension _arc;
16      private Insets _insets;
17      private NodePainter _painter;
18      private Rectangle _bounds;
19      private int _ah, _aw, _il, _ir, _it, _ib;
20  
21      /***
22         Constructor.
23         @param delegate The node painter to be decorated with this border.
24         @param insets The insets.
25         @param border The border color.       
26         @param background The background paint.
27         @param stroke The stroke for the border.
28         @param arc The width and height of the rounded corners of the border (0 will paint rectangular border).
29       */
30      public NodeBorder(NodePainter delegate, Insets insets, Color border, Paint background, Stroke stroke, Dimension arc) {
31  	_painter = delegate;
32  	_insets = insets;
33  	_border = border;
34  	_background = background;
35  	_stroke = stroke;
36  	_arc = arc;
37  	_bounds = new Rectangle();
38  	setZoomFactor(1d);
39      }
40  
41      public NodePainter getDelegate() {
42  	return _painter;
43      }
44  
45      public void setPaint(Paint background) {
46  	_background = background;
47      }
48  
49      public void setZoomFactor(double zf) {
50  	_aw = (int)((double)_arc.width * zf);
51  	_ah = (int)((double)_arc.height * zf);
52  	_il = (int)((double)_insets.left * zf);
53  	_ir = (int)((double)_insets.right * zf);
54  	_it = (int)((double)_insets.top * zf);
55  	_ib = (int)((double)_insets.bottom * zf);
56  
57  	if (_painter instanceof ZoomableNodePainter) {
58  	    ((ZoomableNodePainter)_painter).setZoomFactor(zf);
59  	}
60      }
61  
62      public void paintNode(JGraphPane graphpane,Graphics2D g,Node node) {
63  	Stroke oldstroke = g.getStroke();
64  	//graphpane.getNodeScreenBounds(node, _bounds);
65  	getNodeScreenBounds(graphpane, node, _bounds);	
66  	g.setPaint(_background);
67  	if (_arc.width==0 || _arc.height==0) 
68  	    g.fillRect(_bounds.x, _bounds.y, _bounds.width, _bounds.height);
69  	else
70  	    g.fillRoundRect(_bounds.x, _bounds.y, _bounds.width, _bounds.height, _aw, _ah);
71  	g.setColor(_border);
72  	g.setStroke(_stroke);
73  	if (_arc.width==0 || _arc.height==0) 
74  	    g.drawRect(_bounds.x, _bounds.y, _bounds.width-1, _bounds.height-1);
75  	else
76  	    g.drawRoundRect(_bounds.x, _bounds.y, _bounds.width-1, _bounds.height-1, _aw, _ah);	
77  	g.setStroke(oldstroke);		    
78  	_painter.paintNode(graphpane, g, node);
79      }
80  
81      public boolean isInNode(JGraphPane graphpane,Node node,Point point) {
82  	getNodeScreenBounds(graphpane, node, _bounds);
83  	return _bounds.contains(point);
84      }
85  
86      public void getNodeScreenBounds(JGraphPane graphpane, Node node, Rectangle nodebounds) {
87  	_painter.getNodeScreenBounds(graphpane, node, nodebounds);
88  	nodebounds.x -= _il;
89  	nodebounds.y -= _it;
90  	nodebounds.width += _il+_ir;
91  	nodebounds.height += _it+_ib;
92      }
93  
94      public String getToolTipText(JGraphPane graphpane,Node node,Point point) {
95  	return _painter.getToolTipText(graphpane, node, point);
96      }
97  }