KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ui > JCustomTooltip


1 package prefuse.util.ui;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.Graphics JavaDoc;
7 import java.awt.Insets JavaDoc;
8 import java.awt.Point JavaDoc;
9 import java.awt.Window JavaDoc;
10 import java.awt.event.MouseAdapter JavaDoc;
11 import java.awt.event.MouseEvent JavaDoc;
12
13 import javax.swing.BorderFactory JavaDoc;
14 import javax.swing.JComponent JavaDoc;
15 import javax.swing.JToolTip JavaDoc;
16 import javax.swing.Popup JavaDoc;
17 import javax.swing.PopupFactory JavaDoc;
18 import javax.swing.SwingUtilities JavaDoc;
19 import javax.swing.ToolTipManager JavaDoc;
20 import javax.swing.event.AncestorEvent JavaDoc;
21 import javax.swing.event.AncestorListener JavaDoc;
22
23 /**
24  * Tooltip component that allows arbitrary Swing components to be
25  * used within tooltips. To use this class, provide the constructor
26  * with both the source component (the component to provide the
27  * tooltip for) and the tooltip component (a JComponent to use as the
28  * displayed tooltip). This class can be used to provide
29  * a custom tooltip for a prefuse {@link prefuse.Display} instance,
30  * by registering it with the
31  * {@link prefuse.Display#setCustomToolTip(JToolTip)} method.
32  *
33  * <p>In general, <code>JCustomTooltip</code> can be used with any Swing
34  * widget. This is done by overriding JComponent's <code>createToolTip</code>
35  * method such that it returns the custom tooltip instance.</p>
36  *
37  * <p>Before using this class, you might first check if you can
38  * achieve your desired custom tooltip by using HTML formatting.
39  * As with JLabel instances, the standard Swing tooltip mechanism includes
40  * support for HTML tooltip text, allowing multi-line tooltips using
41  * coloring and various fonts to be created. See
42  * See <a HREF="http://examples.oreilly.com/jswing2/code/ch04/HtmlLabel.java">
43  * this example</a> for an instance of using HTML formatting in
44  * a JLabel. The same HTML string could be used as the input to
45  * JComponent's <code>setToolTipText</code> method.</p>
46  *
47  * @author <a HREF="http://jheer.org">jeffrey heer</a>
48  */

49 public class JCustomTooltip extends JToolTip JavaDoc {
50     
51     private boolean m_persist = false;
52     private Listener JavaDoc m_lstnr = null;
53    
54     /**
55      * Create a new JCustomTooltip
56      * @param src the component for which this is a tooltip
57      * @param content the component to use as the tooltip content
58      */

59     public JCustomTooltip(JComponent JavaDoc src, JComponent JavaDoc content) {
60         this(src, content, false);
61     }
62     
63     /**
64      * Create a new JCustomTooltip
65      * @param src the component for which this is a tooltip
66      * @param content the component to use as the tooltip content
67      * @param inter indicates if the tooltip should be interactive
68      */

69     public JCustomTooltip(JComponent JavaDoc src, JComponent JavaDoc content, boolean inter)
70     {
71         this.setLayout(new BorderLayout JavaDoc());
72         this.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
73         this.setComponent(src);
74         this.add(content);
75         
76         setPersistent(inter);
77     }
78     
79     /**
80      * Indicates if the tooltip will stay persistent on the screen to
81      * support interaction within the tooltip component.
82      * @return true if persistent, false otherwise.
83      */

84     public boolean isPersistent() {
85         return m_persist;
86     }
87     
88     /**
89      * Sets if the tooltip will stay persistent on the screen to
90      * support interaction within the tooltip component.
91      * @param inter true for persistence, false otherwise.
92      */

93     public void setPersistent(boolean inter) {
94         if ( inter == m_persist )
95             return;
96         
97         if ( inter ) {
98             m_lstnr = new Listener JavaDoc();
99             this.addAncestorListener(m_lstnr);
100         } else {
101             this.removeAncestorListener(m_lstnr);
102             m_lstnr = null;
103         }
104         m_persist = inter;
105     }
106     
107     /**
108      * Set the content component of the tooltip
109      * @param content the tooltip content
110      */

111     public void setContent(JComponent JavaDoc content) {
112         this.removeAll();
113         this.add(content);
114     }
115     
116     /**
117      * @see java.awt.Component#getPreferredSize()
118      */

119     public Dimension JavaDoc getPreferredSize() {
120         if ( getComponentCount() > 0 ) {
121             Dimension JavaDoc d = getComponent(0).getPreferredSize();
122             Insets JavaDoc ins = getInsets();
123             return new Dimension JavaDoc(d.width+ins.left+ins.right,
124                                  d.height+ins.top+ins.bottom);
125         } else {
126             return super.getPreferredSize();
127         }
128     }
129     
130     /**
131      * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
132      */

133     public void paintComponent(Graphics JavaDoc g) {
134         if ( getComponentCount() > 0 ) {
135             // paint background
136
g.setColor(getBackground());
137             g.drawRect(0,0,getWidth()-1,getHeight()-1);
138             g.setColor(getComponent(0).getBackground());
139             g.fillRect(1,1,getWidth()-2,getHeight()-2);
140         }
141     }
142     
143     /**
144      * Listener class that registers the tooltip component and performs
145      * persistence management.
146      */

147     private class Listener extends MouseAdapter JavaDoc implements AncestorListener JavaDoc {
148         private Point JavaDoc point = new Point JavaDoc();
149         private boolean showing = false;
150         private Popup JavaDoc popup;
151         
152         public void ancestorAdded(AncestorEvent JavaDoc event) {
153             if ( showing ) { return; }
154
155             Window JavaDoc ttip = SwingUtilities.getWindowAncestor(getParent());
156             if ( ttip == null || !ttip.isVisible() ) {
157                 return;
158             }
159             //ttip.addMouseListener(this);
160
ttip.getLocation(point);
161             ttip.setVisible(false);
162             getParent().remove(JCustomTooltip.this);
163             
164             JComponent JavaDoc c = getComponent();
165             c.setToolTipText(null);
166             c.removeMouseMotionListener(ToolTipManager.sharedInstance());
167             
168             popup = PopupFactory.getSharedInstance().getPopup(
169                     c, JCustomTooltip.this, point.x, point.y);
170             Window JavaDoc w = SwingUtilities.getWindowAncestor(JCustomTooltip.this);
171             w.addMouseListener(this);
172             w.setFocusableWindowState(true);
173             popup.show();
174             
175             showing = true;
176         }
177
178         public void mouseEntered(MouseEvent JavaDoc e) {
179 // Window ttip = SwingUtilities.getWindowAncestor(getParent());
180
// ttip.removeMouseListener(this);
181
// if ( ttip == null || !ttip.isVisible() ) {
182
// return;
183
// }
184
// ttip.getLocation(point);
185
// ttip.hide();
186
// getParent().remove(JCustomTooltip.this);
187
//
188
// JComponent c = getComponent();
189
// c.setToolTipText(null);
190
// c.removeMouseMotionListener(ToolTipManager.sharedInstance());
191
//
192
// popup = PopupFactory.getSharedInstance().getPopup(
193
// c, JCustomTooltip.this, point.x, point.y);
194
// Window w = SwingUtilities.getWindowAncestor(JCustomTooltip.this);
195
// w.addMouseListener(this);
196
// w.setFocusableWindowState(true);
197
// popup.show();
198
//
199
// showing = true;
200
}
201         
202         public void mouseExited(MouseEvent JavaDoc e) {
203             if ( !showing ) return;
204             int x = e.getX(), y = e.getY();
205             Component JavaDoc c = (Component JavaDoc)e.getSource();
206             if ( x < 0 || y < 0 || x > c.getWidth() || y > c.getHeight() )
207             {
208                 Window JavaDoc w = SwingUtilities.getWindowAncestor(JCustomTooltip.this);
209                 w.removeMouseListener(this);
210                 w.setFocusableWindowState(false);
211                 popup.hide();
212                 popup = null;
213                 getComponent().setToolTipText("?");
214                 showing = false;
215             }
216         }
217
218         public void ancestorMoved(AncestorEvent JavaDoc event) {
219         }
220         public void ancestorRemoved(AncestorEvent JavaDoc event) {
221         }
222     }
223     
224 } // end of class JCustomTooltip
225
Popular Tags