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