KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JToolTip


1 /*
2  * @(#)JToolTip.java 1.47 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package javax.swing;
10 import javax.swing.plaf.*;
11 import javax.accessibility.*;
12
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16
17
18 /**
19  * Used to display a "Tip" for a Component. Typically components provide api
20  * to automate the process of using <code>ToolTip</code>s.
21  * For example, any Swing component can use the <code>JComponent</code>
22  * <code>setToolTipText</code> method to specify the text
23  * for a standard tooltip. A component that wants to create a custom
24  * <code>ToolTip</code>
25  * display can override <code>JComponent</code>'s <code>createToolTip</code>
26  * method and use a subclass of this class.
27  * <p>
28  * See <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/tooltip.html">How to Use Tool Tips</a>
29  * in <em>The Java Tutorial</em>
30  * for further documentation.
31  * <p>
32  * <strong>Warning:</strong>
33  * Serialized objects of this class will not be compatible with
34  * future Swing releases. The current serialization support is
35  * appropriate for short term storage or RMI between applications running
36  * the same version of Swing. As of 1.4, support for long term storage
37  * of all JavaBeans<sup><font size="-2">TM</font></sup>
38  * has been added to the <code>java.beans</code> package.
39  * Please see {@link java.beans.XMLEncoder}.
40  *
41  * @see JComponent#setToolTipText
42  * @see JComponent#createToolTip
43  * @version 1.47 12/19/03
44  * @author Dave Moore
45  * @author Rich Shiavi
46  */

47 public class JToolTip extends JComponent JavaDoc implements Accessible {
48     /**
49      * @see #getUIClassID
50      * @see #readObject
51      */

52     private static final String JavaDoc uiClassID = "ToolTipUI";
53
54     String JavaDoc tipText;
55     JComponent JavaDoc component;
56
57     /** Creates a tool tip. */
58     public JToolTip() {
59         setOpaque(true);
60         updateUI();
61     }
62
63     /**
64      * Returns the L&F object that renders this component.
65      *
66      * @return the <code>ToolTipUI</code> object that renders this component
67      */

68     public ToolTipUI getUI() {
69         return (ToolTipUI)ui;
70     }
71
72     /**
73      * Resets the UI property to a value from the current look and feel.
74      *
75      * @see JComponent#updateUI
76      */

77     public void updateUI() {
78         setUI((ToolTipUI)UIManager.getUI(this));
79     }
80
81
82     /**
83      * Returns the name of the L&F class that renders this component.
84      *
85      * @return the string "ToolTipUI"
86      * @see JComponent#getUIClassID
87      * @see UIDefaults#getUI
88      */

89     public String JavaDoc getUIClassID() {
90         return uiClassID;
91     }
92
93
94     /**
95      * Sets the text to show when the tool tip is displayed.
96      * The string <code>tipText</code> may be <code>null</code>.
97      *
98      * @param tipText the <code>String</code> to display
99      * @beaninfo
100      * preferred: true
101      * bound: true
102      * description: Sets the text of the tooltip
103      */

104     public void setTipText(String JavaDoc tipText) {
105         String JavaDoc oldValue = this.tipText;
106         this.tipText = tipText;
107         firePropertyChange("tiptext", oldValue, tipText);
108     }
109
110     /**
111      * Returns the text that is shown when the tool tip is displayed.
112      * The returned value may be <code>null</code>.
113      *
114      * @return the <code>String</code> that is displayed
115      */

116     public String JavaDoc getTipText() {
117         return tipText;
118     }
119
120     /**
121      * Specifies the component that the tooltip describes.
122      * The component <code>c</code> may be <code>null</code>
123      * and will have no effect.
124      * <p>
125      * This is a bound property.
126      *
127      * @param c the <code>JComponent</code> being described
128      * @see JComponent#createToolTip
129      * @beaninfo
130      * bound: true
131      * description: Sets the component that the tooltip describes.
132      */

133     public void setComponent(JComponent JavaDoc c) {
134         JComponent JavaDoc oldValue = this.component;
135
136         component = c;
137         firePropertyChange("component", oldValue, c);
138     }
139
140     /**
141      * Returns the component the tooltip applies to.
142      * The returned value may be <code>null</code>.
143      *
144      * @return the component that the tooltip describes
145      *
146      * @see JComponent#createToolTip
147      */

148     public JComponent JavaDoc getComponent() {
149         return component;
150     }
151
152     /**
153      * Always returns true since tooltips, by definition,
154      * should always be on top of all other windows.
155      */

156     // package private
157
boolean alwaysOnTop() {
158     return true;
159     }
160
161
162     /**
163      * See <code>readObject</code> and <code>writeObject</code>
164      * in <code>JComponent</code> for more
165      * information about serialization in Swing.
166      */

167     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
168         s.defaultWriteObject();
169         if (getUIClassID().equals(uiClassID)) {
170             byte count = JComponent.getWriteObjCounter(this);
171             JComponent.setWriteObjCounter(this, --count);
172             if (count == 0 && ui != null) {
173                 ui.installUI(this);
174             }
175         }
176     }
177
178
179     /**
180      * Returns a string representation of this <code>JToolTip</code>.
181      * This method
182      * is intended to be used only for debugging purposes, and the
183      * content and format of the returned string may vary between
184      * implementations. The returned string may be empty but may not
185      * be <code>null</code>.
186      *
187      * @return a string representation of this <code>JToolTip</code>
188      */

189     protected String JavaDoc paramString() {
190         String JavaDoc tipTextString = (tipText != null ?
191                 tipText : "");
192
193         return super.paramString() +
194         ",tipText=" + tipTextString;
195     }
196
197
198 /////////////////
199
// Accessibility support
200
////////////////
201

202     /**
203      * Gets the AccessibleContext associated with this JToolTip.
204      * For tool tips, the AccessibleContext takes the form of an
205      * AccessibleJToolTip.
206      * A new AccessibleJToolTip instance is created if necessary.
207      *
208      * @return an AccessibleJToolTip that serves as the
209      * AccessibleContext of this JToolTip
210      */

211     public AccessibleContext getAccessibleContext() {
212         if (accessibleContext == null) {
213             accessibleContext = new AccessibleJToolTip();
214         }
215         return accessibleContext;
216     }
217
218     /**
219      * This class implements accessibility support for the
220      * <code>JToolTip</code> class. It provides an implementation of the
221      * Java Accessibility API appropriate to tool tip user-interface elements.
222      * <p>
223      * <strong>Warning:</strong>
224      * Serialized objects of this class will not be compatible with
225      * future Swing releases. The current serialization support is
226      * appropriate for short term storage or RMI between applications running
227      * the same version of Swing. As of 1.4, support for long term storage
228      * of all JavaBeans<sup><font size="-2">TM</font></sup>
229      * has been added to the <code>java.beans</code> package.
230      * Please see {@link java.beans.XMLEncoder}.
231      */

232     protected class AccessibleJToolTip extends AccessibleJComponent {
233
234         /**
235          * Get the accessible description of this object.
236          *
237          * @return a localized String describing this object.
238          */

239         public String JavaDoc getAccessibleDescription() {
240             if (accessibleDescription != null) {
241                 return accessibleDescription;
242             } else {
243                 return getTipText();
244             }
245         }
246
247         /**
248          * Get the role of this object.
249          *
250          * @return an instance of AccessibleRole describing the role of the
251          * object
252          */

253         public AccessibleRole getAccessibleRole() {
254             return AccessibleRole.TOOL_TIP;
255         }
256     }
257 }
258
Popular Tags