blob: 9e8a0ebd89ec125828ef01a877df8560454c68b4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26
27package javax.swing;
28import javax.swing.plaf.*;
29import javax.accessibility.*;
30
31import java.io.ObjectOutputStream;
32import java.io.ObjectInputStream;
33import java.io.IOException;
34
35
36/**
37 * Used to display a "Tip" for a Component. Typically components provide api
38 * to automate the process of using <code>ToolTip</code>s.
39 * For example, any Swing component can use the <code>JComponent</code>
40 * <code>setToolTipText</code> method to specify the text
41 * for a standard tooltip. A component that wants to create a custom
42 * <code>ToolTip</code>
43 * display can override <code>JComponent</code>'s <code>createToolTip</code>
44 * method and use a subclass of this class.
45 * <p>
46 * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/tooltip.html">How to Use Tool Tips</a>
47 * in <em>The Java Tutorial</em>
48 * for further documentation.
49 * <p>
50 * <strong>Warning:</strong> Swing is not thread safe. For more
51 * information see <a
52 * href="package-summary.html#threading">Swing's Threading
53 * Policy</a>.
54 * <p>
55 * <strong>Warning:</strong>
56 * Serialized objects of this class will not be compatible with
57 * future Swing releases. The current serialization support is
58 * appropriate for short term storage or RMI between applications running
59 * the same version of Swing. As of 1.4, support for long term storage
60 * of all JavaBeans<sup><font size="-2">TM</font></sup>
61 * has been added to the <code>java.beans</code> package.
62 * Please see {@link java.beans.XMLEncoder}.
63 *
64 * @see JComponent#setToolTipText
65 * @see JComponent#createToolTip
66 * @author Dave Moore
67 * @author Rich Shiavi
68 */
69public class JToolTip extends JComponent implements Accessible {
70 /**
71 * @see #getUIClassID
72 * @see #readObject
73 */
74 private static final String uiClassID = "ToolTipUI";
75
76 String tipText;
77 JComponent component;
78
79 /** Creates a tool tip. */
80 public JToolTip() {
81 setOpaque(true);
82 updateUI();
83 }
84
85 /**
86 * Returns the L&F object that renders this component.
87 *
88 * @return the <code>ToolTipUI</code> object that renders this component
89 */
90 public ToolTipUI getUI() {
91 return (ToolTipUI)ui;
92 }
93
94 /**
95 * Resets the UI property to a value from the current look and feel.
96 *
97 * @see JComponent#updateUI
98 */
99 public void updateUI() {
100 setUI((ToolTipUI)UIManager.getUI(this));
101 }
102
103
104 /**
105 * Returns the name of the L&F class that renders this component.
106 *
107 * @return the string "ToolTipUI"
108 * @see JComponent#getUIClassID
109 * @see UIDefaults#getUI
110 */
111 public String getUIClassID() {
112 return uiClassID;
113 }
114
115
116 /**
117 * Sets the text to show when the tool tip is displayed.
118 * The string <code>tipText</code> may be <code>null</code>.
119 *
120 * @param tipText the <code>String</code> to display
121 * @beaninfo
122 * preferred: true
123 * bound: true
124 * description: Sets the text of the tooltip
125 */
126 public void setTipText(String tipText) {
127 String oldValue = this.tipText;
128 this.tipText = tipText;
129 firePropertyChange("tiptext", oldValue, tipText);
130 }
131
132 /**
133 * Returns the text that is shown when the tool tip is displayed.
134 * The returned value may be <code>null</code>.
135 *
136 * @return the <code>String</code> that is displayed
137 */
138 public String getTipText() {
139 return tipText;
140 }
141
142 /**
143 * Specifies the component that the tooltip describes.
144 * The component <code>c</code> may be <code>null</code>
145 * and will have no effect.
146 * <p>
147 * This is a bound property.
148 *
149 * @param c the <code>JComponent</code> being described
150 * @see JComponent#createToolTip
151 * @beaninfo
152 * bound: true
153 * description: Sets the component that the tooltip describes.
154 */
155 public void setComponent(JComponent c) {
156 JComponent oldValue = this.component;
157
158 component = c;
159 firePropertyChange("component", oldValue, c);
160 }
161
162 /**
163 * Returns the component the tooltip applies to.
164 * The returned value may be <code>null</code>.
165 *
166 * @return the component that the tooltip describes
167 *
168 * @see JComponent#createToolTip
169 */
170 public JComponent getComponent() {
171 return component;
172 }
173
174 /**
175 * Always returns true since tooltips, by definition,
176 * should always be on top of all other windows.
177 */
178 // package private
179 boolean alwaysOnTop() {
180 return true;
181 }
182
183
184 /**
185 * See <code>readObject</code> and <code>writeObject</code>
186 * in <code>JComponent</code> for more
187 * information about serialization in Swing.
188 */
189 private void writeObject(ObjectOutputStream s) throws IOException {
190 s.defaultWriteObject();
191 if (getUIClassID().equals(uiClassID)) {
192 byte count = JComponent.getWriteObjCounter(this);
193 JComponent.setWriteObjCounter(this, --count);
194 if (count == 0 && ui != null) {
195 ui.installUI(this);
196 }
197 }
198 }
199
200
201 /**
202 * Returns a string representation of this <code>JToolTip</code>.
203 * This method
204 * is intended to be used only for debugging purposes, and the
205 * content and format of the returned string may vary between
206 * implementations. The returned string may be empty but may not
207 * be <code>null</code>.
208 *
209 * @return a string representation of this <code>JToolTip</code>
210 */
211 protected String paramString() {
212 String tipTextString = (tipText != null ?
213 tipText : "");
214
215 return super.paramString() +
216 ",tipText=" + tipTextString;
217 }
218
219
220/////////////////
221// Accessibility support
222////////////////
223
224 /**
225 * Gets the AccessibleContext associated with this JToolTip.
226 * For tool tips, the AccessibleContext takes the form of an
227 * AccessibleJToolTip.
228 * A new AccessibleJToolTip instance is created if necessary.
229 *
230 * @return an AccessibleJToolTip that serves as the
231 * AccessibleContext of this JToolTip
232 */
233 public AccessibleContext getAccessibleContext() {
234 if (accessibleContext == null) {
235 accessibleContext = new AccessibleJToolTip();
236 }
237 return accessibleContext;
238 }
239
240 /**
241 * This class implements accessibility support for the
242 * <code>JToolTip</code> class. It provides an implementation of the
243 * Java Accessibility API appropriate to tool tip user-interface elements.
244 * <p>
245 * <strong>Warning:</strong>
246 * Serialized objects of this class will not be compatible with
247 * future Swing releases. The current serialization support is
248 * appropriate for short term storage or RMI between applications running
249 * the same version of Swing. As of 1.4, support for long term storage
250 * of all JavaBeans<sup><font size="-2">TM</font></sup>
251 * has been added to the <code>java.beans</code> package.
252 * Please see {@link java.beans.XMLEncoder}.
253 */
254 protected class AccessibleJToolTip extends AccessibleJComponent {
255
256 /**
257 * Get the accessible description of this object.
258 *
259 * @return a localized String describing this object.
260 */
261 public String getAccessibleDescription() {
262 String description = accessibleDescription;
263
264 // fallback to client property
265 if (description == null) {
266 description = (String)getClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY);
267 }
268 if (description == null) {
269 description = getTipText();
270 }
271 return description;
272 }
273
274 /**
275 * Get the role of this object.
276 *
277 * @return an instance of AccessibleRole describing the role of the
278 * object
279 */
280 public AccessibleRole getAccessibleRole() {
281 return AccessibleRole.TOOL_TIP;
282 }
283 }
284}