2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
package javax.swing;
|
|
28 |
import javax.swing.plaf.*;
|
|
29 |
import javax.accessibility.*;
|
|
30 |
|
|
31 |
import java.io.ObjectOutputStream;
|
|
32 |
import java.io.ObjectInputStream;
|
|
33 |
import 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 |
*/
|
11268
|
69 |
@SuppressWarnings("serial")
|
2
|
70 |
public class JToolTip extends JComponent implements Accessible {
|
|
71 |
/**
|
|
72 |
* @see #getUIClassID
|
|
73 |
* @see #readObject
|
|
74 |
*/
|
|
75 |
private static final String uiClassID = "ToolTipUI";
|
|
76 |
|
|
77 |
String tipText;
|
|
78 |
JComponent component;
|
|
79 |
|
|
80 |
/** Creates a tool tip. */
|
|
81 |
public JToolTip() {
|
|
82 |
setOpaque(true);
|
|
83 |
updateUI();
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Returns the L&F object that renders this component.
|
|
88 |
*
|
|
89 |
* @return the <code>ToolTipUI</code> object that renders this component
|
|
90 |
*/
|
|
91 |
public ToolTipUI getUI() {
|
|
92 |
return (ToolTipUI)ui;
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Resets the UI property to a value from the current look and feel.
|
|
97 |
*
|
|
98 |
* @see JComponent#updateUI
|
|
99 |
*/
|
|
100 |
public void updateUI() {
|
|
101 |
setUI((ToolTipUI)UIManager.getUI(this));
|
|
102 |
}
|
|
103 |
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Returns the name of the L&F class that renders this component.
|
|
107 |
*
|
|
108 |
* @return the string "ToolTipUI"
|
|
109 |
* @see JComponent#getUIClassID
|
|
110 |
* @see UIDefaults#getUI
|
|
111 |
*/
|
|
112 |
public String getUIClassID() {
|
|
113 |
return uiClassID;
|
|
114 |
}
|
|
115 |
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Sets the text to show when the tool tip is displayed.
|
|
119 |
* The string <code>tipText</code> may be <code>null</code>.
|
|
120 |
*
|
|
121 |
* @param tipText the <code>String</code> to display
|
|
122 |
* @beaninfo
|
|
123 |
* preferred: true
|
|
124 |
* bound: true
|
|
125 |
* description: Sets the text of the tooltip
|
|
126 |
*/
|
|
127 |
public void setTipText(String tipText) {
|
|
128 |
String oldValue = this.tipText;
|
|
129 |
this.tipText = tipText;
|
|
130 |
firePropertyChange("tiptext", oldValue, tipText);
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Returns the text that is shown when the tool tip is displayed.
|
|
135 |
* The returned value may be <code>null</code>.
|
|
136 |
*
|
|
137 |
* @return the <code>String</code> that is displayed
|
|
138 |
*/
|
|
139 |
public String getTipText() {
|
|
140 |
return tipText;
|
|
141 |
}
|
|
142 |
|
|
143 |
/**
|
|
144 |
* Specifies the component that the tooltip describes.
|
|
145 |
* The component <code>c</code> may be <code>null</code>
|
|
146 |
* and will have no effect.
|
|
147 |
* <p>
|
|
148 |
* This is a bound property.
|
|
149 |
*
|
|
150 |
* @param c the <code>JComponent</code> being described
|
|
151 |
* @see JComponent#createToolTip
|
|
152 |
* @beaninfo
|
|
153 |
* bound: true
|
|
154 |
* description: Sets the component that the tooltip describes.
|
|
155 |
*/
|
|
156 |
public void setComponent(JComponent c) {
|
|
157 |
JComponent oldValue = this.component;
|
|
158 |
|
|
159 |
component = c;
|
|
160 |
firePropertyChange("component", oldValue, c);
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns the component the tooltip applies to.
|
|
165 |
* The returned value may be <code>null</code>.
|
|
166 |
*
|
|
167 |
* @return the component that the tooltip describes
|
|
168 |
*
|
|
169 |
* @see JComponent#createToolTip
|
|
170 |
*/
|
|
171 |
public JComponent getComponent() {
|
|
172 |
return component;
|
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* Always returns true since tooltips, by definition,
|
|
177 |
* should always be on top of all other windows.
|
|
178 |
*/
|
|
179 |
// package private
|
|
180 |
boolean alwaysOnTop() {
|
|
181 |
return true;
|
|
182 |
}
|
|
183 |
|
|
184 |
|
|
185 |
/**
|
|
186 |
* See <code>readObject</code> and <code>writeObject</code>
|
|
187 |
* in <code>JComponent</code> for more
|
|
188 |
* information about serialization in Swing.
|
|
189 |
*/
|
|
190 |
private void writeObject(ObjectOutputStream s) throws IOException {
|
|
191 |
s.defaultWriteObject();
|
|
192 |
if (getUIClassID().equals(uiClassID)) {
|
|
193 |
byte count = JComponent.getWriteObjCounter(this);
|
|
194 |
JComponent.setWriteObjCounter(this, --count);
|
|
195 |
if (count == 0 && ui != null) {
|
|
196 |
ui.installUI(this);
|
|
197 |
}
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Returns a string representation of this <code>JToolTip</code>.
|
|
204 |
* This method
|
|
205 |
* is intended to be used only for debugging purposes, and the
|
|
206 |
* content and format of the returned string may vary between
|
|
207 |
* implementations. The returned string may be empty but may not
|
|
208 |
* be <code>null</code>.
|
|
209 |
*
|
|
210 |
* @return a string representation of this <code>JToolTip</code>
|
|
211 |
*/
|
|
212 |
protected String paramString() {
|
|
213 |
String tipTextString = (tipText != null ?
|
|
214 |
tipText : "");
|
|
215 |
|
|
216 |
return super.paramString() +
|
|
217 |
",tipText=" + tipTextString;
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
/////////////////
|
|
222 |
// Accessibility support
|
|
223 |
////////////////
|
|
224 |
|
|
225 |
/**
|
|
226 |
* Gets the AccessibleContext associated with this JToolTip.
|
|
227 |
* For tool tips, the AccessibleContext takes the form of an
|
|
228 |
* AccessibleJToolTip.
|
|
229 |
* A new AccessibleJToolTip instance is created if necessary.
|
|
230 |
*
|
|
231 |
* @return an AccessibleJToolTip that serves as the
|
|
232 |
* AccessibleContext of this JToolTip
|
|
233 |
*/
|
|
234 |
public AccessibleContext getAccessibleContext() {
|
|
235 |
if (accessibleContext == null) {
|
|
236 |
accessibleContext = new AccessibleJToolTip();
|
|
237 |
}
|
|
238 |
return accessibleContext;
|
|
239 |
}
|
|
240 |
|
|
241 |
/**
|
|
242 |
* This class implements accessibility support for the
|
|
243 |
* <code>JToolTip</code> class. It provides an implementation of the
|
|
244 |
* Java Accessibility API appropriate to tool tip user-interface elements.
|
|
245 |
* <p>
|
|
246 |
* <strong>Warning:</strong>
|
|
247 |
* Serialized objects of this class will not be compatible with
|
|
248 |
* future Swing releases. The current serialization support is
|
|
249 |
* appropriate for short term storage or RMI between applications running
|
|
250 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
251 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
252 |
* has been added to the <code>java.beans</code> package.
|
|
253 |
* Please see {@link java.beans.XMLEncoder}.
|
|
254 |
*/
|
11268
|
255 |
@SuppressWarnings("serial")
|
2
|
256 |
protected class AccessibleJToolTip extends AccessibleJComponent {
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Get the accessible description of this object.
|
|
260 |
*
|
|
261 |
* @return a localized String describing this object.
|
|
262 |
*/
|
|
263 |
public String getAccessibleDescription() {
|
|
264 |
String description = accessibleDescription;
|
|
265 |
|
|
266 |
// fallback to client property
|
|
267 |
if (description == null) {
|
|
268 |
description = (String)getClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY);
|
|
269 |
}
|
|
270 |
if (description == null) {
|
|
271 |
description = getTipText();
|
|
272 |
}
|
|
273 |
return description;
|
|
274 |
}
|
|
275 |
|
|
276 |
/**
|
|
277 |
* Get the role of this object.
|
|
278 |
*
|
|
279 |
* @return an instance of AccessibleRole describing the role of the
|
|
280 |
* object
|
|
281 |
*/
|
|
282 |
public AccessibleRole getAccessibleRole() {
|
|
283 |
return AccessibleRole.TOOL_TIP;
|
|
284 |
}
|
|
285 |
}
|
|
286 |
}
|