001 /* JToolTip.java -- 002 Copyright (C) 2002, 2004 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package javax.swing; 040 041 import gnu.java.lang.CPStringBuilder; 042 043 import java.awt.AWTEvent; 044 import java.beans.PropertyChangeEvent; 045 046 import javax.accessibility.Accessible; 047 import javax.accessibility.AccessibleContext; 048 import javax.accessibility.AccessibleRole; 049 import javax.swing.plaf.ToolTipUI; 050 051 /** 052 * This class is used to display ToolTips. ToolTips are small floating windows 053 * that display text when the mouse comes to rest over a Component. ToolTips 054 * are set for JComponents using JComponent.setToolTipText(String). 055 */ 056 public class JToolTip extends JComponent implements Accessible 057 { 058 059 private static final long serialVersionUID = -1138929898906751643L; 060 061 /** 062 * Provides the accessibility features for the <code>JToolTip</code> 063 * component. 064 */ 065 protected class AccessibleJToolTip extends AccessibleJComponent 066 { 067 private static final long serialVersionUID = -6222548177795408476L; 068 069 /** 070 * Creates a new AccessibleJToolTip object. 071 */ 072 protected AccessibleJToolTip() 073 { 074 // Nothing to do here. 075 } 076 077 /** 078 * Returns a description for the accessible component. 079 * 080 * @return A description for the accessible component. 081 */ 082 public String getAccessibleDescription() 083 { 084 String result = super.getAccessibleDescription(); 085 if (result == null) 086 result = text; 087 return result; 088 } 089 090 /** 091 * Returns the accessible role for the <code>JToolTip</code> component. 092 * 093 * @return {@link AccessibleRole#TOOL_TIP}. 094 */ 095 public AccessibleRole getAccessibleRole() 096 { 097 return AccessibleRole.TOOL_TIP; 098 } 099 } 100 101 /** The text to display in the JToolTip. */ 102 String text; 103 104 /** The component that the tool tip is associated with. */ 105 JComponent component; 106 107 /** 108 * Creates a new <code>JToolTip</code> instance. 109 */ 110 public JToolTip() 111 { 112 disableEvents(AWTEvent.MOUSE_EVENT_MASK); 113 updateUI(); 114 } 115 116 /** 117 * Returns the text displayed by the tool tip. 118 * 119 * @return The text (possibly <code>null</code>). 120 * 121 * @see #setTipText(String) 122 */ 123 public String getTipText() 124 { 125 return text; 126 } 127 128 /** 129 * Returns the object that provides accessibility features for this 130 * <code>JToolTip</code> component. 131 * 132 * @return The accessible context (an instance of {@link AccessibleJToolTip}). 133 */ 134 public AccessibleContext getAccessibleContext() 135 { 136 if (accessibleContext == null) 137 accessibleContext = new AccessibleJToolTip(); 138 return accessibleContext; 139 } 140 141 /** 142 * Returns the component that the tool tip is associated with. 143 * 144 * @return The component (possibly <code>null</code>). 145 * 146 * @see #setComponent(JComponent) 147 */ 148 public JComponent getComponent() 149 { 150 return component; 151 } 152 153 /** 154 * Returns the current UI delegate for this component. 155 * 156 * @return The UI delegate. 157 */ 158 public ToolTipUI getUI() 159 { 160 return (ToolTipUI) ui; 161 } 162 163 /** 164 * Returns the string suffix used to identify the UI class, in this case 165 * <code>"ToolTipUI"</code>. 166 * 167 * @return <code>"ToolTipUI"</code>. 168 */ 169 public String getUIClassID() 170 { 171 return "ToolTipUI"; 172 } 173 174 /** 175 * Returns a string describing the attributes for the <code>JToolTip</code> 176 * component, for use in debugging. The return value is guaranteed to be 177 * non-<code>null</code>, but the format of the string may vary between 178 * implementations. 179 * 180 * @return A string describing the attributes of the <code>JToolTip</code>. 181 */ 182 protected String paramString() 183 { 184 CPStringBuilder sb = new CPStringBuilder(super.paramString()); 185 sb.append(",tiptext="); 186 if (text != null) 187 sb.append(text); 188 return sb.toString(); 189 } 190 191 /** 192 * Sets the component that the tool tip is associated with and sends a 193 * {@link PropertyChangeEvent} (with the property name 'component') to all 194 * registered listeners. 195 * 196 * @param c the component (<code>null</code> permitted). 197 * 198 * @see #getComponent() 199 */ 200 public void setComponent(JComponent c) 201 { 202 JComponent oldValue = component; 203 component = c; 204 firePropertyChange("component", oldValue, c); 205 } 206 207 /** 208 * Sets the text to be displayed by the tool tip and sends a 209 * {@link PropertyChangeEvent} (with the property name 'tiptext') to all 210 * registered listeners. 211 * 212 * @param tipText the text (<code>null</code> permitted). 213 * 214 * @see #getTipText() 215 */ 216 public void setTipText(String tipText) 217 { 218 String oldValue = text; 219 text = tipText; 220 firePropertyChange("tiptext", oldValue, tipText); 221 } 222 223 /** 224 * This method resets the UI used to the Look and Feel default. 225 */ 226 public void updateUI() 227 { 228 setUI((ToolTipUI) UIManager.getUI(this)); 229 } 230 231 /** 232 * Returns <code>true</code> if the component is guaranteed to be painted 233 * on top of others. This returns false by default and is overridden by 234 * components like JMenuItem, JPopupMenu and JToolTip to return true for 235 * added efficiency. 236 * 237 * @return <code>true</code> if the component is guaranteed to be painted 238 * on top of others 239 */ 240 boolean onTop() 241 { 242 return true; 243 } 244 }