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 NodePainter which paints several lines of text.
10 */
11 public class TextNodePainter implements NodePainter {
12 private Color _color;
13 private Font _font;
14 private Rectangle _bounds;
15 private FontMetrics _metrics;
16 private int _font_height;
17 private int _font_ascent;
18
19 /***
20 Constructor.
21 @param font The font.
22 @param color The text color.
23 */
24 public TextNodePainter(Font font, Color color) {
25 _font = font;
26 _metrics = Toolkit.getDefaultToolkit().getFontMetrics(_font);
27 _font_height = _metrics.getHeight();
28 _font_ascent = _metrics.getAscent();
29 _color = color;
30 _bounds = new Rectangle();
31 }
32
33 /***
34 Subclasses may override this method to provide the
35 text to be painted for the specified node.
36 @param node The node to be painted.
37 @return An array of strings: the lines of text to be painted for the node.
38 */
39 protected String[] getText(Node node) {
40 return new String[] { node.toString() };
41 }
42
43 /***
44 Get the bounds of the text of the given node.
45 @param grp The graphpane.
46 @param node The node.
47 @param lines The text of the node.
48 @param bounds The rectangle to set to the bounds of the text.
49 */
50 protected void getTextBounds(JGraphPane grp, Node node, String[] lines, Rectangle bounds) {
51 Point p = grp.getScreenPointForNode(node);
52 int height = lines.length * _font_height;
53 int width = 0;
54 for (int i=0; i<lines.length; i++) {
55 width = Math.max(width, _metrics.stringWidth(lines[i]));
56 }
57 bounds.setBounds(p.x - width/2,
58 p.y - height/2,
59 width,
60 height);
61 }
62
63 public void paintNode(JGraphPane graphpane,Graphics2D g,Node node) {
64 String[] lines = getText(node);
65
66 g.setFont(_font);
67 g.setColor(_color);
68
69 getTextBounds(graphpane, node, lines, _bounds);
70
71 for (int i=0; i<lines.length; i++) {
72 g.drawString(lines[i], _bounds.x, _bounds.y + (i * _font_height) + _font_ascent);
73 }
74 }
75
76 public boolean isInNode(JGraphPane graphpane,Node node,Point point) {
77 getNodeScreenBounds(graphpane, node, _bounds);
78 return _bounds.contains(point);
79 }
80
81 public void getNodeScreenBounds(JGraphPane graphpane, Node node, Rectangle nodebounds) {
82 getTextBounds(graphpane, node, getText(node), nodebounds);
83 }
84
85 public String getToolTipText(JGraphPane graphpane,Node node,Point point) {
86 return node.toString();
87 }
88 }