home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / swing / JToolTip.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.6 KB  |  182 lines

  1. /*
  2.  * %W% %E%
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21.  
  22. package java.awt.swing;
  23. import java.awt.swing.plaf.*;
  24. import java.awt.accessibility.*;
  25.  
  26.  
  27. /**
  28.  * Used to display a "Tip" for a Component. Typically components provide api
  29.  * to automate the process of using ToolTips. For example, any Swing component
  30.  * can use the JComponent <code>setToolTipText</code> method to specify the text
  31.  * for a standard tooltip. A component that wants to create a custom ToolTip
  32.  * display can override JComponent's <code>createToolTip</code> method and use
  33.  * a subclass of this class.
  34.  * <p>
  35.  * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/tooltip.html">How to Use Tool Tips</a>
  36.  * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  37.  * for further documentation.
  38.  * <p>
  39.  * Warning: serialized objects of this class will not be compatible with
  40.  * future swing releases.  The current serialization support is appropriate
  41.  * for short term storage or RMI between Swing1.0 applications.  It will
  42.  * not be possible to load serialized Swing1.0 objects with future releases
  43.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  44.  * baseline for the serialized form of Swing objects.
  45.  *
  46.  * @see JComponent#setToolTipText
  47.  * @see JComponent#createToolTip
  48.  * @version %I% %G%
  49.  * @author Dave Moore
  50.  * @author Rich Shiavi
  51.  */
  52. public class JToolTip extends JComponent implements Accessible {
  53.     String tipText;
  54.     JComponent component;
  55.  
  56.     /** Creates a tool tip. */
  57.     public JToolTip() {
  58.         updateUI();
  59.     }
  60.  
  61.     /**
  62.      * Returns the L&F object that renders this component.
  63.      *
  64.      * @return the ToolTipUI object that renders this component
  65.      */
  66.     public ToolTipUI getUI() {
  67.         return (ToolTipUI)ui;
  68.     }
  69.  
  70.     /**
  71.      * Notification from the UIFactory that the L&F has changed. 
  72.      * Called to replace the UI with the latest version from the 
  73.      * UIFactory.
  74.      *
  75.      * @see JComponent#updateUI
  76.      */
  77.     public void updateUI() {
  78.         setUI((ToolTipUI)UIManager.getUI(this));
  79.     }
  80.  
  81.  
  82.     /**
  83.      * Returns the name of the L&F class that renders this component.
  84.      *
  85.      * @return "ToolTipUI"
  86.      * @see JComponent#getUIClassID
  87.      * @see UIDefaults#getUI
  88.      */
  89.     public String getUIClassID() {
  90.         return "ToolTipUI";
  91.     }
  92.  
  93.  
  94.     /**
  95.      * Sets the text to show when the tool tip is displayed.
  96.      *
  97.      * @param tipText the String to display
  98.      */
  99.     public void setTipText(String tipText) {
  100.         this.tipText = tipText;
  101.     }
  102.  
  103.     /**
  104.      * Returns the text that is shown when the tool tip is displayed.
  105.      *
  106.      * @return the String that is displayed
  107.      */
  108.     public String getTipText() {
  109.         return tipText;
  110.     }
  111.  
  112.     /**
  113.      * Specifies the component that the tooltip describes.
  114.      *
  115.      * @see JComponent#createToolTip
  116.      */
  117.     public void setComponent(JComponent c) {
  118.         component = c;
  119.     }
  120.  
  121.     /**
  122.      * @return the component that the tooltip describes.
  123.      *
  124.      * @see JComponent#createToolTip
  125.      */
  126.     public JComponent getComponent() {
  127.         return component;
  128.     }
  129.  
  130. /////////////////
  131. // Accessibility support
  132. ////////////////
  133.  
  134.     /**
  135.      * Get the AccessibleContext associated with this JComponent
  136.      *
  137.      * @return the AccessibleContext of this JComponent
  138.      */
  139.     public AccessibleContext getAccessibleContext() {
  140.         if (accessibleContext == null) {
  141.             accessibleContext = new AccessibleJToolTip();
  142.         }
  143.         return accessibleContext;
  144.     }
  145.  
  146.     /**
  147.      * The class used to obtain the accessible role for this object.
  148.      * <p>
  149.      * Warning: serialized objects of this class will not be compatible with
  150.      * future swing releases.  The current serialization support is appropriate
  151.      * for short term storage or RMI between Swing1.0 applications.  It will
  152.      * not be possible to load serialized Swing1.0 objects with future releases
  153.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  154.      * baseline for the serialized form of Swing objects.
  155.      */
  156.     protected class AccessibleJToolTip extends AccessibleJComponent {
  157.  
  158.         /**
  159.          * Get the accessible description of this object.
  160.          *
  161.          * @return a localized String describing this object.
  162.          */
  163.         public String getAccessibleDescription() {
  164.             if (accessibleDescription != null) {
  165.                 return accessibleDescription;
  166.             } else {
  167.                 return getTipText();
  168.             }
  169.         }
  170.  
  171.         /**
  172.          * Get the role of this object.
  173.          *
  174.          * @return an instance of AccessibleRole describing the role of the 
  175.          * object
  176.          */
  177.         public AccessibleRole getAccessibleRole() {
  178.             return AccessibleRole.TOOL_TIP;
  179.         }
  180.     }
  181. }
  182.