KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > ToolTipControl


1 package prefuse.controls;
2
3 import java.awt.event.MouseEvent JavaDoc;
4
5 import prefuse.Display;
6 import prefuse.visual.VisualItem;
7
8
9 /**
10  * Control that enables a tooltip display for items based on mouse hover.
11  *
12  * @author <a HREF="http://jheer.org">jeffrey heer</a>
13  */

14 public class ToolTipControl extends ControlAdapter {
15
16     private String JavaDoc[] label;
17     private StringBuffer JavaDoc sbuf;
18     
19     /**
20      * Create a new ToolTipControl.
21      * @param field the field name to use for the tooltip text
22      */

23     public ToolTipControl(String JavaDoc field) {
24         this(new String JavaDoc[] {field});
25     }
26
27     /**
28      * Create a new ToolTipControl.
29      * @param fields the field names to use for the tooltip text. The
30      * values of each field will be concatenated to form the tooltip.
31      */

32     public ToolTipControl(String JavaDoc[] fields) {
33         label = fields;
34         if ( fields.length > 1 )
35             sbuf = new StringBuffer JavaDoc();
36     }
37     
38     /**
39      * @see prefuse.controls.Control#itemEntered(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
40      */

41     public void itemEntered(VisualItem item, MouseEvent JavaDoc e) {
42         Display d = (Display)e.getSource();
43         if ( label.length == 1 ) {
44             // optimize the simple case
45
if ( item.canGetString(label[0]) )
46                 d.setToolTipText(item.getString(label[0]));
47         } else {
48             sbuf.delete(0, sbuf.length());
49             for ( int i=0; i<label.length; ++i ) {
50                 if ( item.canGetString(label[i]) ) {
51                     if ( sbuf.length() > 0 )
52                         sbuf.append("; ");
53                     sbuf.append(item.getString(label[i]));
54                 }
55             }
56             d.setToolTipText(sbuf.toString());
57         }
58     }
59     
60     /**
61      * @see prefuse.controls.Control#itemExited(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
62      */

63     public void itemExited(VisualItem item, MouseEvent JavaDoc e) {
64         Display d = (Display)e.getSource();
65         d.setToolTipText(null);
66     }
67     
68 } // end of class ToolTipControl
69
Popular Tags