Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicToolTipUI.java
blobb561cce7174e2d9ffce3eee0ac7b96145aeec464
1 /* BasicToolTipUI.java --
2 Copyright (C) 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.basic;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Dimension;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Insets;
47 import java.awt.Rectangle;
49 import javax.swing.JComponent;
50 import javax.swing.JToolTip;
51 import javax.swing.SwingConstants;
52 import javax.swing.SwingUtilities;
53 import javax.swing.UIDefaults;
54 import javax.swing.UIManager;
55 import javax.swing.border.Border;
56 import javax.swing.plaf.ComponentUI;
57 import javax.swing.plaf.ToolTipUI;
59 /**
60 * This is the Basic Look and Feel UI class for JToolTip.
62 public class BasicToolTipUI extends ToolTipUI
64 /** The default Border around the JToolTip. */
65 private static Border defaultBorder = new Border()
67 // FIXME: This needs to go into Basic Look and Feel
68 // defaults.
70 /**
71 * This method returns the border insets.
73 * @param c The Component to find Border insets for.
75 * @return The Border insets.
76 */
77 public Insets getBorderInsets(Component c)
79 return new Insets(4, 4, 4, 4);
82 /**
83 * This method returns whether the border is opaque.
85 * @return Whether the border is opaque.
87 public boolean isBorderOpaque()
89 return false;
92 /**
93 * This method paints the border.
95 * @param c The Component to paint this border around.
96 * @param g The Graphics object to paint with.
97 * @param x The x coordinate to start painting at.
98 * @param y The y coordinate to start painting at.
99 * @param w The width of the Component.
100 * @param y The height of the Component.
102 public void paintBorder(Component c, Graphics g, int x, int y, int w,
103 int h)
105 Color saved = g.getColor();
106 g.setColor(Color.BLACK);
108 g.drawRect(0, 0, w - 1, h - 1);
110 g.setColor(saved);
114 /** The shared instance of BasicToolTipUI used for all ToolTips. */
115 private static BasicToolTipUI shared;
118 * Creates a new BasicToolTipUI object.
120 public BasicToolTipUI()
122 super();
126 * This method creates a new BasicToolTip UI for the given
127 * JComponent.
129 * @param c The JComponent to create a UI for.
131 * @return A BasicToolTipUI that can be used by the given JComponent.
133 public static ComponentUI createUI(JComponent c)
135 if (shared == null)
136 shared = new BasicToolTipUI();
137 return shared;
141 * This method returns the msximum size of the given JComponent.
143 * @param c The JComponent to find a maximum size for.
145 * @return The maximum size.
147 public Dimension getMaximumSize(JComponent c)
149 return getPreferredSize(c);
153 * This method returns the minimum size of the given JComponent.
155 * @param c The JComponent to find a minimum size for.
157 * @return The minimum size.
159 public Dimension getMinimumSize(JComponent c)
161 return getPreferredSize(c);
165 * This method returns the preferred size of the given JComponent.
167 * @param c The JComponent to find a preferred size for.
169 * @return The preferred size.
171 public Dimension getPreferredSize(JComponent c)
173 JToolTip tip = (JToolTip) c;
174 Rectangle vr = new Rectangle();
175 Rectangle ir = new Rectangle();
176 Rectangle tr = new Rectangle();
177 Insets insets = tip.getInsets();
178 FontMetrics fm = tip.getToolkit().getFontMetrics(tip.getFont());
179 SwingUtilities.layoutCompoundLabel(tip, fm, tip.getTipText(), null,
180 SwingConstants.CENTER,
181 SwingConstants.CENTER,
182 SwingConstants.CENTER,
183 SwingConstants.CENTER, vr, ir, tr, 0);
184 return new Dimension(insets.left + tr.width + insets.right,
185 insets.top + tr.height + insets.bottom);
189 * This method installs the defaults for the given JComponent.
191 * @param c The JComponent to install defaults for.
193 protected void installDefaults(JComponent c)
195 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
196 c.setBackground(defaults.getColor("ToolTip.background"));
197 c.setForeground(defaults.getColor("ToolTip.foreground"));
198 c.setFont(defaults.getFont("ToolTip.font"));
199 c.setBorder(defaultBorder);
203 * This method installs the listeners for the given JComponent.
205 * @param c The JComponent to install listeners for.
207 protected void installListeners(JComponent c)
212 * This method installs the UI for the given JComponent.
214 * @param c The JComponent to install the UI for.
216 public void installUI(JComponent c)
218 c.setOpaque(true);
219 installDefaults(c);
220 installListeners(c);
224 * This method paints the given JComponent with the given Graphics object.
226 * @param g The Graphics object to paint with.
227 * @param c The JComponent to paint.
229 public void paint(Graphics g, JComponent c)
231 JToolTip tip = (JToolTip) c;
233 String text = tip.getTipText();
234 if (text == null)
235 return;
237 Rectangle vr = new Rectangle();
238 vr = SwingUtilities.calculateInnerArea(tip, vr);
239 Rectangle ir = new Rectangle();
240 Rectangle tr = new Rectangle();
241 FontMetrics fm = tip.getToolkit().getFontMetrics(tip.getFont());
242 SwingUtilities.layoutCompoundLabel(tip, fm, tip.getTipText(), null,
243 SwingConstants.CENTER,
244 SwingConstants.CENTER,
245 SwingConstants.CENTER,
246 SwingConstants.CENTER, vr, ir, tr, 0);
248 Color saved = g.getColor();
249 g.setColor(Color.BLACK);
251 g.drawString(text, vr.x, vr.y + fm.getAscent());
253 g.setColor(saved);
257 * This method uninstalls the defaults for the given JComponent.
259 * @param c The JComponent to uninstall defaults for.
261 protected void uninstallDefaults(JComponent c)
263 c.setForeground(null);
264 c.setBackground(null);
265 c.setFont(null);
266 c.setBorder(null);
270 * This method uninstalls listeners for the given JComponent.
272 * @param c The JComponent to uninstall listeners for.
274 protected void uninstallListeners(JComponent c)
279 * This method uninstalls the UI for the given JComponent.
281 * @param c The JComponent to uninstall.
283 public void uninstallUI(JComponent c)
285 uninstallDefaults(c);
286 uninstallListeners(c);