2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package javax.swing.plaf.metal;
41 import java.awt.Color;
42 import java.awt.Dimension;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.event.InputEvent;
47 import java.awt.event.KeyEvent;
49 import javax.swing.AbstractButton;
50 import javax.swing.JComponent;
51 import javax.swing.JMenuItem;
52 import javax.swing.JToolTip;
53 import javax.swing.KeyStroke;
54 import javax.swing.UIManager;
55 import javax.swing.border.Border;
56 import javax.swing.plaf.ComponentUI;
57 import javax.swing.plaf.UIResource;
58 import javax.swing.plaf.basic.BasicToolTipUI;
61 * A UI delegate for the {@link JToolTip} component.
63 public class MetalToolTipUI
64 extends BasicToolTipUI
67 * The amount of space between the tool tip text and the accelerator
68 * description (if visible).
70 public static final int padSpaceBetweenStrings = 12;
72 /** The shared UI instance. */
73 private static MetalToolTipUI instance;
75 /** A flag controlling the visibility of the accelerator (if there is one). */
76 private boolean isAcceleratorHidden;
78 /** A string representing the accelerator key for the component. */
79 private String acceleratorString;
82 * The delimiter for the accelerator string.
84 private String acceleratorDelimiter;
86 /** The font for the accelerator string. */
87 private Font acceleratorFont;
89 /** The color for the accelerator string. */
90 private Color acceleratorForeground;
92 /** The active border. */
93 private Border activeBorder;
95 /** The inactive border. */
96 private Border inactiveBorder;
99 * Constructs a new instance of <code>MetalToolTipUI</code>.
101 public MetalToolTipUI()
104 activeBorder = UIManager.getBorder("ToolTip.border");
105 inactiveBorder = UIManager.getBorder("ToolTip.borderInactive");
106 isAcceleratorHidden = UIManager.getBoolean("ToolTip.hideAccelerator");
107 acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
108 acceleratorForeground = UIManager.getColor("MenuItem.acceleratorForeground");
109 acceleratorDelimiter = UIManager.getString("MenuItem.acceleratorDelimiter");
113 * Returns a shared instance of the <code>MetalToolTipUI</code> class.
114 * Although this UI delegate does maintain state information, there is never
115 * more than one tool tip visible, so it is OK to use a shared instance.
117 * @param component the component (a {@link JToolTip}).
119 * @return A shared instance of the <code>MetalToolTipUI</code> class.
121 public static ComponentUI createUI(JComponent component)
123 if (instance == null)
124 instance = new MetalToolTipUI();
129 * Returns a string representing the accelerator key (if there is one) for
130 * the component that the tool tip belongs to.
132 * @return A string representing the accelerator key.
134 public String getAcceleratorString()
136 return acceleratorString;
140 * Installs the UI for the specified component (a {@link JToolTip}).
142 * @param c the {@link JToolTip} component.
144 public void installUI(JComponent c)
147 Border existingBorder = c.getBorder();
148 if (existingBorder == null || existingBorder instanceof UIResource)
151 c.setBorder(activeBorder);
153 c.setBorder(inactiveBorder);
158 * Clears the defaults set in {@link #installUI(JComponent)}.
160 * @param c the component.
162 public void uninstallUI(JComponent c)
164 super.uninstallUI(c);
165 if (c.getBorder() instanceof UIResource)
170 * Returns <code>true</code> if the accelerator string is hidden, and
171 * <code>false</code> otherwise. This setting is controlled by the
172 * <code>ToolTip.hideAccelerator</code> entry in the UI defaults table.
176 protected boolean isAcceleratorHidden()
178 return isAcceleratorHidden;
182 * Returns the preferred size for the {@link JToolTip} component.
184 * @param c the component (a {@link JToolTip}).
186 * @return The preferred size.
188 public Dimension getPreferredSize(JComponent c)
190 Dimension d = super.getPreferredSize(c);
191 String acc = getAcceleratorString();
192 if (acc != null && ! acc.equals(""))
194 FontMetrics fm = c.getFontMetrics(c.getFont());
195 d.width += fm.stringWidth(acc);
201 * Paints the tool tip.
203 * @param g the graphics context.
204 * @param c the {@link JToolTip} component.
206 public void paint(Graphics g, JComponent c)
209 // Somehow paint accelerator. Keep care for possible HTML rendering.
213 * Returns a string representing the accelerator for the component, or
214 * <code>null</code> if the component has no accelerator.
216 * @param c the component.
218 * @return A string representing the accelerator (possibly
219 * <code>null</code>).
221 private String fetchAcceleratorString(JComponent c)
223 String result = null;
224 if (c instanceof JToolTip)
226 JToolTip toolTip = (JToolTip) c;
227 JComponent component = toolTip.getComponent();
230 if (component instanceof JMenuItem)
232 JMenuItem item = (JMenuItem) component;
233 ks = item.getAccelerator();
235 mne = item.getMnemonic();
237 else if (component instanceof AbstractButton)
239 AbstractButton button = (AbstractButton) component;
240 mne = button.getMnemonic();
243 ks = KeyStroke.getKeyStroke(Character.toUpperCase((char) mne),
244 InputEvent.ALT_MASK, false);
246 result = acceleratorToString(ks);
252 * Returns a string representing an accelerator.
254 * @param accelerator the accelerator (<code>null</code> not permitted).
256 * @return A string representing an accelerator.
258 private String acceleratorToString(KeyStroke accelerator)
260 // convert keystroke into string format
261 String modifiersText = "";
262 int modifiers = accelerator.getModifiers();
263 char keyChar = accelerator.getKeyChar();
264 int keyCode = accelerator.getKeyCode();
267 modifiersText = KeyEvent.getKeyModifiersText(modifiers)
268 + acceleratorDelimiter;
270 if (keyCode == KeyEvent.VK_UNDEFINED)
271 return modifiersText + keyChar;
273 return modifiersText + KeyEvent.getKeyText(keyCode);