JToolTip.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* JToolTip.java --
  2. Copyright (C) 2002, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.awt.AWTEvent;
  34. import java.beans.PropertyChangeEvent;
  35. import javax.accessibility.Accessible;
  36. import javax.accessibility.AccessibleContext;
  37. import javax.accessibility.AccessibleRole;
  38. import javax.swing.plaf.ToolTipUI;
  39. /**
  40. * This class is used to display ToolTips. ToolTips are small floating windows
  41. * that display text when the mouse comes to rest over a Component. ToolTips
  42. * are set for JComponents using JComponent.setToolTipText(String).
  43. */
  44. public class JToolTip extends JComponent implements Accessible
  45. {
  46. private static final long serialVersionUID = -1138929898906751643L;
  47. /**
  48. * Provides the accessibility features for the <code>JToolTip</code>
  49. * component.
  50. */
  51. protected class AccessibleJToolTip extends AccessibleJComponent
  52. {
  53. private static final long serialVersionUID = -6222548177795408476L;
  54. /**
  55. * Creates a new AccessibleJToolTip object.
  56. */
  57. protected AccessibleJToolTip()
  58. {
  59. // Nothing to do here.
  60. }
  61. /**
  62. * Returns a description for the accessible component.
  63. *
  64. * @return A description for the accessible component.
  65. */
  66. public String getAccessibleDescription()
  67. {
  68. String result = super.getAccessibleDescription();
  69. if (result == null)
  70. result = text;
  71. return result;
  72. }
  73. /**
  74. * Returns the accessible role for the <code>JToolTip</code> component.
  75. *
  76. * @return {@link AccessibleRole#TOOL_TIP}.
  77. */
  78. public AccessibleRole getAccessibleRole()
  79. {
  80. return AccessibleRole.TOOL_TIP;
  81. }
  82. }
  83. /** The text to display in the JToolTip. */
  84. String text;
  85. /** The component that the tool tip is associated with. */
  86. JComponent component;
  87. /**
  88. * Creates a new <code>JToolTip</code> instance.
  89. */
  90. public JToolTip()
  91. {
  92. disableEvents(AWTEvent.MOUSE_EVENT_MASK);
  93. updateUI();
  94. }
  95. /**
  96. * Returns the text displayed by the tool tip.
  97. *
  98. * @return The text (possibly <code>null</code>).
  99. *
  100. * @see #setTipText(String)
  101. */
  102. public String getTipText()
  103. {
  104. return text;
  105. }
  106. /**
  107. * Returns the object that provides accessibility features for this
  108. * <code>JToolTip</code> component.
  109. *
  110. * @return The accessible context (an instance of {@link AccessibleJToolTip}).
  111. */
  112. public AccessibleContext getAccessibleContext()
  113. {
  114. if (accessibleContext == null)
  115. accessibleContext = new AccessibleJToolTip();
  116. return accessibleContext;
  117. }
  118. /**
  119. * Returns the component that the tool tip is associated with.
  120. *
  121. * @return The component (possibly <code>null</code>).
  122. *
  123. * @see #setComponent(JComponent)
  124. */
  125. public JComponent getComponent()
  126. {
  127. return component;
  128. }
  129. /**
  130. * Returns the current UI delegate for this component.
  131. *
  132. * @return The UI delegate.
  133. */
  134. public ToolTipUI getUI()
  135. {
  136. return (ToolTipUI) ui;
  137. }
  138. /**
  139. * Returns the string suffix used to identify the UI class, in this case
  140. * <code>"ToolTipUI"</code>.
  141. *
  142. * @return <code>"ToolTipUI"</code>.
  143. */
  144. public String getUIClassID()
  145. {
  146. return "ToolTipUI";
  147. }
  148. /**
  149. * Returns a string describing the attributes for the <code>JToolTip</code>
  150. * component, for use in debugging. The return value is guaranteed to be
  151. * non-<code>null</code>, but the format of the string may vary between
  152. * implementations.
  153. *
  154. * @return A string describing the attributes of the <code>JToolTip</code>.
  155. */
  156. protected String paramString()
  157. {
  158. CPStringBuilder sb = new CPStringBuilder(super.paramString());
  159. sb.append(",tiptext=");
  160. if (text != null)
  161. sb.append(text);
  162. return sb.toString();
  163. }
  164. /**
  165. * Sets the component that the tool tip is associated with and sends a
  166. * {@link PropertyChangeEvent} (with the property name 'component') to all
  167. * registered listeners.
  168. *
  169. * @param c the component (<code>null</code> permitted).
  170. *
  171. * @see #getComponent()
  172. */
  173. public void setComponent(JComponent c)
  174. {
  175. JComponent oldValue = component;
  176. component = c;
  177. firePropertyChange("component", oldValue, c);
  178. }
  179. /**
  180. * Sets the text to be displayed by the tool tip and sends a
  181. * {@link PropertyChangeEvent} (with the property name 'tiptext') to all
  182. * registered listeners.
  183. *
  184. * @param tipText the text (<code>null</code> permitted).
  185. *
  186. * @see #getTipText()
  187. */
  188. public void setTipText(String tipText)
  189. {
  190. String oldValue = text;
  191. text = tipText;
  192. firePropertyChange("tiptext", oldValue, tipText);
  193. }
  194. /**
  195. * This method resets the UI used to the Look and Feel default.
  196. */
  197. public void updateUI()
  198. {
  199. setUI((ToolTipUI) UIManager.getUI(this));
  200. }
  201. /**
  202. * Returns <code>true</code> if the component is guaranteed to be painted
  203. * on top of others. This returns false by default and is overridden by
  204. * components like JMenuItem, JPopupMenu and JToolTip to return true for
  205. * added efficiency.
  206. *
  207. * @return <code>true</code> if the component is guaranteed to be painted
  208. * on top of others
  209. */
  210. boolean onTop()
  211. {
  212. return true;
  213. }
  214. }