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