Source for javax.swing.JToolTip

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