1. /*
  2. * @(#)JToolTip.java 1.45 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import javax.swing.plaf.*;
  9. import javax.accessibility.*;
  10. import java.io.ObjectOutputStream;
  11. import java.io.ObjectInputStream;
  12. import java.io.IOException;
  13. /**
  14. * Used to display a "Tip" for a Component. Typically components provide api
  15. * to automate the process of using <code>ToolTip</code>s.
  16. * For example, any Swing component can use the <code>JComponent</code>
  17. * <code>setToolTipText</code> method to specify the text
  18. * for a standard tooltip. A component that wants to create a custom
  19. * <code>ToolTip</code>
  20. * display can override <code>JComponent</code>'s <code>createToolTip</code>
  21. * method and use a subclass of this class.
  22. * <p>
  23. * For the keyboard keys used by this component in the standard Look and
  24. * Feel (L&F) renditions, see the
  25. * <a href="doc-files/Key-Index.html#JToolTip"><code>JToolTip</code> key assignments</a>.
  26. * <p>
  27. * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/tooltip.html">How to Use Tool Tips</a>
  28. * in <em>The Java Tutorial</em>
  29. * for further documentation.
  30. * <p>
  31. * <strong>Warning:</strong>
  32. * Serialized objects of this class will not be compatible with
  33. * future Swing releases. The current serialization support is
  34. * appropriate for short term storage or RMI between applications running
  35. * the same version of Swing. As of 1.4, support for long term storage
  36. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  37. * has been added to the <code>java.beans</code> package.
  38. * Please see {@link java.beans.XMLEncoder}.
  39. *
  40. * @see JComponent#setToolTipText
  41. * @see JComponent#createToolTip
  42. * @version 1.45 01/23/03
  43. * @author Dave Moore
  44. * @author Rich Shiavi
  45. */
  46. public class JToolTip extends JComponent implements Accessible {
  47. /**
  48. * @see #getUIClassID
  49. * @see #readObject
  50. */
  51. private static final String uiClassID = "ToolTipUI";
  52. String tipText;
  53. JComponent component;
  54. /** Creates a tool tip. */
  55. public JToolTip() {
  56. setOpaque(true);
  57. updateUI();
  58. }
  59. /**
  60. * Returns the L&F object that renders this component.
  61. *
  62. * @return the <code>ToolTipUI</code> object that renders this component
  63. */
  64. public ToolTipUI getUI() {
  65. return (ToolTipUI)ui;
  66. }
  67. /**
  68. * Resets the UI property to a value from the current look and feel.
  69. *
  70. * @see JComponent#updateUI
  71. */
  72. public void updateUI() {
  73. setUI((ToolTipUI)UIManager.getUI(this));
  74. }
  75. /**
  76. * Returns the name of the L&F class that renders this component.
  77. *
  78. * @return the string "ToolTipUI"
  79. * @see JComponent#getUIClassID
  80. * @see UIDefaults#getUI
  81. */
  82. public String getUIClassID() {
  83. return uiClassID;
  84. }
  85. /**
  86. * Sets the text to show when the tool tip is displayed.
  87. * The string <code>tipText</code> may be <code>null</code>.
  88. *
  89. * @param tipText the <code>String</code> to display
  90. * @beaninfo
  91. * preferred: true
  92. * bound: true
  93. * description: Sets the text of the tooltip
  94. */
  95. public void setTipText(String tipText) {
  96. String oldValue = this.tipText;
  97. this.tipText = tipText;
  98. firePropertyChange("tiptext", oldValue, tipText);
  99. }
  100. /**
  101. * Returns the text that is shown when the tool tip is displayed.
  102. * The returned value may be <code>null</code>.
  103. *
  104. * @return the <code>String</code> that is displayed
  105. */
  106. public String getTipText() {
  107. return tipText;
  108. }
  109. /**
  110. * Specifies the component that the tooltip describes.
  111. * The component <code>c</code> may be <code>null</code>
  112. * and will have no effect.
  113. * <p>
  114. * This is a bound property.
  115. *
  116. * @param c the <code>JComponent</code> being described
  117. * @see JComponent#createToolTip
  118. * @beaninfo
  119. * bound: true
  120. * description: Sets the component that the tooltip describes.
  121. */
  122. public void setComponent(JComponent c) {
  123. JComponent oldValue = this.component;
  124. component = c;
  125. firePropertyChange("component", oldValue, c);
  126. }
  127. /**
  128. * Returns the component the tooltip applies to.
  129. * The returned value may be <code>null</code>.
  130. *
  131. * @return the component that the tooltip describes
  132. *
  133. * @see JComponent#createToolTip
  134. */
  135. public JComponent getComponent() {
  136. return component;
  137. }
  138. /**
  139. * Always returns true since tooltips, by definition,
  140. * should always be on top of all other windows.
  141. */
  142. // package private
  143. boolean alwaysOnTop() {
  144. return true;
  145. }
  146. /**
  147. * See <code>readObject</code> and <code>writeObject</code>
  148. * in <code>JComponent</code> for more
  149. * information about serialization in Swing.
  150. */
  151. private void writeObject(ObjectOutputStream s) throws IOException {
  152. s.defaultWriteObject();
  153. if (getUIClassID().equals(uiClassID)) {
  154. byte count = JComponent.getWriteObjCounter(this);
  155. JComponent.setWriteObjCounter(this, --count);
  156. if (count == 0 && ui != null) {
  157. ui.installUI(this);
  158. }
  159. }
  160. }
  161. /**
  162. * Returns a string representation of this <code>JToolTip</code>.
  163. * This method
  164. * is intended to be used only for debugging purposes, and the
  165. * content and format of the returned string may vary between
  166. * implementations. The returned string may be empty but may not
  167. * be <code>null</code>.
  168. *
  169. * @return a string representation of this <code>JToolTip</code>
  170. */
  171. protected String paramString() {
  172. String tipTextString = (tipText != null ?
  173. tipText : "");
  174. return super.paramString() +
  175. ",tipText=" + tipTextString;
  176. }
  177. /////////////////
  178. // Accessibility support
  179. ////////////////
  180. /**
  181. * Gets the AccessibleContext associated with this JToolTip.
  182. * For tool tips, the AccessibleContext takes the form of an
  183. * AccessibleJToolTip.
  184. * A new AccessibleJToolTip instance is created if necessary.
  185. *
  186. * @return an AccessibleJToolTip that serves as the
  187. * AccessibleContext of this JToolTip
  188. */
  189. public AccessibleContext getAccessibleContext() {
  190. if (accessibleContext == null) {
  191. accessibleContext = new AccessibleJToolTip();
  192. }
  193. return accessibleContext;
  194. }
  195. /**
  196. * This class implements accessibility support for the
  197. * <code>JToolTip</code> class. It provides an implementation of the
  198. * Java Accessibility API appropriate to tool tip user-interface elements.
  199. * <p>
  200. * <strong>Warning:</strong>
  201. * Serialized objects of this class will not be compatible with
  202. * future Swing releases. The current serialization support is
  203. * appropriate for short term storage or RMI between applications running
  204. * the same version of Swing. As of 1.4, support for long term storage
  205. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  206. * has been added to the <code>java.beans</code> package.
  207. * Please see {@link java.beans.XMLEncoder}.
  208. */
  209. protected class AccessibleJToolTip extends AccessibleJComponent {
  210. /**
  211. * Get the accessible description of this object.
  212. *
  213. * @return a localized String describing this object.
  214. */
  215. public String getAccessibleDescription() {
  216. if (accessibleDescription != null) {
  217. return accessibleDescription;
  218. } else {
  219. return getTipText();
  220. }
  221. }
  222. /**
  223. * Get the role of this object.
  224. *
  225. * @return an instance of AccessibleRole describing the role of the
  226. * object
  227. */
  228. public AccessibleRole getAccessibleRole() {
  229. return AccessibleRole.TOOL_TIP;
  230. }
  231. }
  232. }