1. /*
  2. * %W% %E%
  3. *
  4. * Copyright 2002 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 ToolTips. For example, any Swing component
  16. * can use the JComponent <code>setToolTipText</code> method to specify the text
  17. * for a standard tooltip. A component that wants to create a custom ToolTip
  18. * display can override JComponent's <code>createToolTip</code> method and use
  19. * a subclass of this class.
  20. * <p>
  21. * For the keyboard keys used by this component in the standard Look and
  22. * Feel (L&F) renditions, see the
  23. * <a href="doc-files/Key-Index.html#JToolTip">JToolTip</a> key assignments.
  24. * <p>
  25. * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/tooltip.html">How to Use Tool Tips</a>
  26. * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  27. * for further documentation.
  28. * <p>
  29. * <strong>Warning:</strong>
  30. * Serialized objects of this class will not be compatible with
  31. * future Swing releases. The current serialization support is appropriate
  32. * for short term storage or RMI between applications running the same
  33. * version of Swing. A future release of Swing will provide support for
  34. * long term persistence.
  35. *
  36. * @see JComponent#setToolTipText
  37. * @see JComponent#createToolTip
  38. * @version %I% %G%
  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. updateUI();
  53. }
  54. /**
  55. * Returns the L&F object that renders this component.
  56. *
  57. * @return the ToolTipUI object that renders this component
  58. */
  59. public ToolTipUI getUI() {
  60. return (ToolTipUI)ui;
  61. }
  62. /**
  63. * Notification from the UIFactory that the L&F has changed.
  64. * Called to replace the UI with the latest version from the
  65. * UIFactory.
  66. *
  67. * @see JComponent#updateUI
  68. */
  69. public void updateUI() {
  70. setUI((ToolTipUI)UIManager.getUI(this));
  71. }
  72. /**
  73. * Returns the name of the L&F class that renders this component.
  74. *
  75. * @return "ToolTipUI"
  76. * @see JComponent#getUIClassID
  77. * @see UIDefaults#getUI
  78. */
  79. public String getUIClassID() {
  80. return uiClassID;
  81. }
  82. /**
  83. * Sets the text to show when the tool tip is displayed.
  84. *
  85. * @param tipText the String 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. *
  99. * @return the String that is displayed
  100. */
  101. public String getTipText() {
  102. return tipText;
  103. }
  104. /**
  105. * Specifies the component that the tooltip describes.
  106. *
  107. * @see JComponent#createToolTip
  108. */
  109. public void setComponent(JComponent c) {
  110. component = c;
  111. }
  112. /**
  113. * Returns the component the tooltip applies to.
  114. * @return the component that the tooltip describes
  115. *
  116. * @see JComponent#createToolTip
  117. */
  118. public JComponent getComponent() {
  119. return component;
  120. }
  121. /**
  122. * Always return true since tooltips, by definition,
  123. * should always be on top of all other windows.
  124. */
  125. // package private
  126. boolean alwaysOnTop() {
  127. return true;
  128. }
  129. /**
  130. * See readObject() and writeObject() in JComponent for more
  131. * information about serialization in Swing.
  132. */
  133. private void writeObject(ObjectOutputStream s) throws IOException {
  134. s.defaultWriteObject();
  135. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  136. ui.installUI(this);
  137. }
  138. }
  139. /**
  140. * Returns a string representation of this JToolTip. This method
  141. * is intended to be used only for debugging purposes, and the
  142. * content and format of the returned string may vary between
  143. * implementations. The returned string may be empty but may not
  144. * be <code>null</code>.
  145. *
  146. * @return a string representation of this JToolTip.
  147. */
  148. protected String paramString() {
  149. String tipTextString = (tipText != null ?
  150. tipText : "");
  151. return super.paramString() +
  152. ",tipText=" + tipTextString;
  153. }
  154. /////////////////
  155. // Accessibility support
  156. ////////////////
  157. /**
  158. * Get the AccessibleContext associated with this JComponent
  159. *
  160. * @return the AccessibleContext of this JComponent
  161. */
  162. public AccessibleContext getAccessibleContext() {
  163. if (accessibleContext == null) {
  164. accessibleContext = new AccessibleJToolTip();
  165. }
  166. return accessibleContext;
  167. }
  168. /**
  169. * The class used to obtain the accessible role for this object.
  170. * <p>
  171. * <strong>Warning:</strong>
  172. * Serialized objects of this class will not be compatible with
  173. * future Swing releases. The current serialization support is appropriate
  174. * for short term storage or RMI between applications running the same
  175. * version of Swing. A future release of Swing will provide support for
  176. * long term persistence.
  177. */
  178. protected class AccessibleJToolTip extends AccessibleJComponent {
  179. /**
  180. * Get the accessible description of this object.
  181. *
  182. * @return a localized String describing this object.
  183. */
  184. public String getAccessibleDescription() {
  185. if (accessibleDescription != null) {
  186. return accessibleDescription;
  187. } else {
  188. return getTipText();
  189. }
  190. }
  191. /**
  192. * Get the role of this object.
  193. *
  194. * @return an instance of AccessibleRole describing the role of the
  195. * object
  196. */
  197. public AccessibleRole getAccessibleRole() {
  198. return AccessibleRole.TOOL_TIP;
  199. }
  200. }
  201. }