01: package prefuse.controls;
02:
03: import java.awt.event.MouseEvent;
04:
05: import prefuse.Display;
06: import prefuse.visual.VisualItem;
07:
08: /**
09: * Control that enables a tooltip display for items based on mouse hover.
10: *
11: * @author <a href="http://jheer.org">jeffrey heer</a>
12: */
13: public class ToolTipControl extends ControlAdapter {
14:
15: private String[] label;
16: private StringBuffer sbuf;
17:
18: /**
19: * Create a new ToolTipControl.
20: * @param field the field name to use for the tooltip text
21: */
22: public ToolTipControl(String field) {
23: this (new String[] { field });
24: }
25:
26: /**
27: * Create a new ToolTipControl.
28: * @param fields the field names to use for the tooltip text. The
29: * values of each field will be concatenated to form the tooltip.
30: */
31: public ToolTipControl(String[] fields) {
32: label = fields;
33: if (fields.length > 1)
34: sbuf = new StringBuffer();
35: }
36:
37: /**
38: * @see prefuse.controls.Control#itemEntered(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
39: */
40: public void itemEntered(VisualItem item, MouseEvent e) {
41: Display d = (Display) e.getSource();
42: if (label.length == 1) {
43: // optimize the simple case
44: if (item.canGetString(label[0]))
45: d.setToolTipText(item.getString(label[0]));
46: } else {
47: sbuf.delete(0, sbuf.length());
48: for (int i = 0; i < label.length; ++i) {
49: if (item.canGetString(label[i])) {
50: if (sbuf.length() > 0)
51: sbuf.append("; ");
52: sbuf.append(item.getString(label[i]));
53: }
54: }
55: d.setToolTipText(sbuf.toString());
56: }
57: }
58:
59: /**
60: * @see prefuse.controls.Control#itemExited(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
61: */
62: public void itemExited(VisualItem item, MouseEvent e) {
63: Display d = (Display) e.getSource();
64: d.setToolTipText(null);
65: }
66:
67: } // end of class ToolTipControl
|