Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / plaf / metal / MetalToolTipUI.java
blobf183ed5a14963da734b3c47fd0e10d16a3c4a657
1 /* MetalToolTipUI.java
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)
9 any later version.
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
19 02110-1301 USA.
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.
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;
43 import java.awt.Font;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Insets;
47 import java.awt.Rectangle;
48 import java.awt.Toolkit;
49 import java.awt.event.InputEvent;
50 import java.awt.event.KeyEvent;
52 import javax.swing.AbstractButton;
53 import javax.swing.JComponent;
54 import javax.swing.JMenuItem;
55 import javax.swing.JToolTip;
56 import javax.swing.KeyStroke;
57 import javax.swing.SwingConstants;
58 import javax.swing.SwingUtilities;
59 import javax.swing.UIManager;
60 import javax.swing.border.Border;
61 import javax.swing.plaf.ComponentUI;
62 import javax.swing.plaf.UIResource;
63 import javax.swing.plaf.basic.BasicToolTipUI;
65 /**
66 * A UI delegate for the {@link JToolTip} component.
68 public class MetalToolTipUI
69 extends BasicToolTipUI
71 /**
72 * The amount of space between the tool tip text and the accelerator
73 * description (if visible).
75 public static final int padSpaceBetweenStrings = 12;
77 /** The shared UI instance. */
78 private static MetalToolTipUI instance = null;
80 /** A flag controlling the visibility of the accelerator (if there is one). */
81 private boolean isAcceleratorHidden;
83 /** A string representing the accelerator key for the component. */
84 private String acceleratorString;
86 /**
87 * The delimiter for the accelerator string.
89 private String acceleratorDelimiter;
91 /** The font for the accelerator string. */
92 private Font acceleratorFont;
94 /** The color for the accelerator string. */
95 private Color acceleratorForeground;
97 /** The active border. */
98 private Border activeBorder;
100 /** The inactive border. */
101 private Border inactiveBorder;
104 * Constructs a new instance of <code>MetalToolTipUI</code>.
106 public MetalToolTipUI()
108 super();
109 activeBorder = UIManager.getBorder("ToolTip.border");
110 inactiveBorder = UIManager.getBorder("ToolTip.borderInactive");
111 isAcceleratorHidden = UIManager.getBoolean("ToolTip.hideAccelerator");
112 acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
113 acceleratorForeground = UIManager.getColor("MenuItem.acceleratorForeground");
114 acceleratorDelimiter = UIManager.getString("MenuItem.acceleratorDelimiter");
118 * Returns a shared instance of the <code>MetalToolTipUI</code> class.
119 * Although this UI delegate does maintain state information, there is never
120 * more than one tool tip visible, so it is OK to use a shared instance.
122 * @param component the component (a {@link JToolTip}).
124 * @return A shared instance of the <code>MetalToolTipUI</code> class.
126 public static ComponentUI createUI(JComponent component)
128 if (instance == null)
129 instance = new MetalToolTipUI();
130 return instance;
134 * Returns a string representing the accelerator key (if there is one) for
135 * the component that the tool tip belongs to.
137 * @return A string representing the accelerator key.
139 public String getAcceleratorString()
141 return acceleratorString;
145 * Installs the UI for the specified component (a {@link JToolTip}).
147 * @param c the {@link JToolTip} component.
149 public void installUI(JComponent c)
151 super.installUI(c);
152 Border existingBorder = c.getBorder();
153 if (existingBorder == null || existingBorder instanceof UIResource)
155 if (c.isEnabled())
156 c.setBorder(activeBorder);
157 else
158 c.setBorder(inactiveBorder);
163 * Clears the defaults set in {@link #installUI(JComponent)}.
165 * @param c the component.
167 public void uninstallUI(JComponent c)
169 super.uninstallUI(c);
170 if (c.getBorder() instanceof UIResource)
171 c.setBorder(null);
175 * Returns <code>true</code> if the accelerator string is hidden, and
176 * <code>false</code> otherwise. This setting is controlled by the
177 * <code>ToolTip.hideAccelerator</code> entry in the UI defaults table.
179 * @return A boolean.
181 protected boolean isAcceleratorHidden()
183 return isAcceleratorHidden;
187 * Returns the preferred size for the {@link JToolTip} component.
189 * @param c the component (a {@link JToolTip}).
191 * @return The preferred size.
193 public Dimension getPreferredSize(JComponent c)
195 if (isAcceleratorHidden())
196 return super.getPreferredSize(c);
197 else
199 Insets insets = c.getInsets();
200 JToolTip tt = (JToolTip) c;
201 String tipText = tt.getTipText();
202 if (tipText != null)
204 FontMetrics fm = c.getFontMetrics(c.getFont());
205 int prefH = fm.getHeight() + insets.top + insets.bottom;
206 int prefW = fm.stringWidth(tipText) + insets.left + insets.right;
208 // this seems to be the first opportunity we have to get the
209 // accelerator string from the component (if it has one)
210 acceleratorString = fetchAcceleratorString(c);
211 if (acceleratorString != null)
213 prefW += padSpaceBetweenStrings;
214 fm = c.getFontMetrics(acceleratorFont);
215 prefW += fm.stringWidth(acceleratorString);
217 return new Dimension(prefW, prefH);
219 else return new Dimension(0, 0);
224 * Paints the tool tip.
226 * @param g the graphics context.
227 * @param c the {@link JToolTip} component.
229 public void paint(Graphics g, JComponent c)
231 JToolTip tip = (JToolTip) c;
233 String text = tip.getTipText();
234 Toolkit t = tip.getToolkit();
235 if (text == null)
236 return;
238 Rectangle vr = new Rectangle();
239 vr = SwingUtilities.calculateInnerArea(tip, vr);
240 Rectangle ir = new Rectangle();
241 Rectangle tr = new Rectangle();
242 FontMetrics fm = t.getFontMetrics(tip.getFont());
243 int ascent = fm.getAscent();
244 SwingUtilities.layoutCompoundLabel(tip, fm, text, null,
245 SwingConstants.CENTER, SwingConstants.LEFT,
246 SwingConstants.CENTER, SwingConstants.CENTER, vr, ir, tr, 0);
247 Color saved = g.getColor();
248 g.setColor(Color.BLACK);
250 g.drawString(text, vr.x, vr.y + ascent);
252 // paint accelerator
253 if (acceleratorString != null)
255 g.setFont(acceleratorFont);
256 g.setColor(acceleratorForeground);
257 fm = t.getFontMetrics(acceleratorFont);
258 int width = fm.stringWidth(acceleratorString);
259 g.drawString(acceleratorString, vr.x + vr.width - width - padSpaceBetweenStrings/2,
260 vr.y + vr.height - fm.getDescent());
263 g.setColor(saved);
267 * Returns a string representing the accelerator for the component, or
268 * <code>null</code> if the component has no accelerator.
270 * @param c the component.
272 * @return A string representing the accelerator (possibly
273 * <code>null</code>).
275 private String fetchAcceleratorString(JComponent c)
277 String result = null;
278 if (c instanceof JToolTip)
280 JToolTip toolTip = (JToolTip) c;
281 JComponent component = toolTip.getComponent();
282 KeyStroke ks = null;
283 int mne = 0;
284 if (component instanceof JMenuItem)
286 JMenuItem item = (JMenuItem) component;
287 ks = item.getAccelerator();
288 if (ks == null)
289 mne = item.getMnemonic();
291 else if (component instanceof AbstractButton)
293 AbstractButton button = (AbstractButton) component;
294 mne = button.getMnemonic();
296 if (mne > 0)
297 ks = KeyStroke.getKeyStroke(Character.toUpperCase((char) mne),
298 InputEvent.ALT_MASK, false);
299 if (ks != null)
300 result = acceleratorToString(ks);
302 return result;
306 * Returns a string representing an accelerator.
308 * @param accelerator the accelerator (<code>null</code> not permitted).
310 * @return A string representing an accelerator.
312 private String acceleratorToString(KeyStroke accelerator)
314 // convert keystroke into string format
315 String modifiersText = "";
316 int modifiers = accelerator.getModifiers();
317 char keyChar = accelerator.getKeyChar();
318 int keyCode = accelerator.getKeyCode();
320 if (modifiers != 0)
321 modifiersText = KeyEvent.getKeyModifiersText(modifiers)
322 + acceleratorDelimiter;
324 if (keyCode == KeyEvent.VK_UNDEFINED)
325 return modifiersText + keyChar;
326 else
327 return modifiersText + KeyEvent.getKeyText(keyCode);