GNU Classpath (0.17) | ||
Frames | No Frames |
1: /* BasicToolTipUI.java -- 2: Copyright (C) 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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. 10: 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. 15: 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. 20: 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. 25: 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. */ 37: 38: 39: package javax.swing.plaf.basic; 40: 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; 48: 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; 58: 59: /** 60: * This is the Basic Look and Feel UI class for JToolTip. 61: */ 62: public class BasicToolTipUI extends ToolTipUI 63: { 64: /** The default Border around the JToolTip. */ 65: private static Border defaultBorder = new Border() 66: { 67: // FIXME: This needs to go into Basic Look and Feel 68: // defaults. 69: 70: /** 71: * This method returns the border insets. 72: * 73: * @param c The Component to find Border insets for. 74: * 75: * @return The Border insets. 76: */ 77: public Insets getBorderInsets(Component c) 78: { 79: return new Insets(4, 4, 4, 4); 80: } 81: 82: /** 83: * This method returns whether the border is opaque. 84: * 85: * @return Whether the border is opaque. 86: */ 87: public boolean isBorderOpaque() 88: { 89: return false; 90: } 91: 92: /** 93: * This method paints the border. 94: * 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 h The height of the Component. 101: */ 102: public void paintBorder(Component c, Graphics g, int x, int y, int w, 103: int h) 104: { 105: Color saved = g.getColor(); 106: g.setColor(Color.BLACK); 107: 108: g.drawRect(0, 0, w - 1, h - 1); 109: 110: g.setColor(saved); 111: } 112: }; 113: 114: /** The shared instance of BasicToolTipUI used for all ToolTips. */ 115: private static BasicToolTipUI shared; 116: 117: /** 118: * Creates a new BasicToolTipUI object. 119: */ 120: public BasicToolTipUI() 121: { 122: super(); 123: } 124: 125: /** 126: * This method creates a new BasicToolTip UI for the given 127: * JComponent. 128: * 129: * @param c The JComponent to create a UI for. 130: * 131: * @return A BasicToolTipUI that can be used by the given JComponent. 132: */ 133: public static ComponentUI createUI(JComponent c) 134: { 135: if (shared == null) 136: shared = new BasicToolTipUI(); 137: return shared; 138: } 139: 140: /** 141: * This method returns the msximum size of the given JComponent. 142: * 143: * @param c The JComponent to find a maximum size for. 144: * 145: * @return The maximum size. 146: */ 147: public Dimension getMaximumSize(JComponent c) 148: { 149: return getPreferredSize(c); 150: } 151: 152: /** 153: * This method returns the minimum size of the given JComponent. 154: * 155: * @param c The JComponent to find a minimum size for. 156: * 157: * @return The minimum size. 158: */ 159: public Dimension getMinimumSize(JComponent c) 160: { 161: return getPreferredSize(c); 162: } 163: 164: /** 165: * This method returns the preferred size of the given JComponent. 166: * 167: * @param c The JComponent to find a preferred size for. 168: * 169: * @return The preferred size. 170: */ 171: public Dimension getPreferredSize(JComponent c) 172: { 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); 186: } 187: 188: /** 189: * This method installs the defaults for the given JComponent. 190: * 191: * @param c The JComponent to install defaults for. 192: */ 193: protected void installDefaults(JComponent c) 194: { 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); 200: } 201: 202: /** 203: * This method installs the listeners for the given JComponent. 204: * 205: * @param c The JComponent to install listeners for. 206: */ 207: protected void installListeners(JComponent c) 208: { 209: } 210: 211: /** 212: * This method installs the UI for the given JComponent. 213: * 214: * @param c The JComponent to install the UI for. 215: */ 216: public void installUI(JComponent c) 217: { 218: c.setOpaque(true); 219: installDefaults(c); 220: installListeners(c); 221: } 222: 223: /** 224: * This method paints the given JComponent with the given Graphics object. 225: * 226: * @param g The Graphics object to paint with. 227: * @param c The JComponent to paint. 228: */ 229: public void paint(Graphics g, JComponent c) 230: { 231: JToolTip tip = (JToolTip) c; 232: 233: String text = tip.getTipText(); 234: if (text == null) 235: return; 236: 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); 247: 248: Color saved = g.getColor(); 249: g.setColor(Color.BLACK); 250: 251: g.drawString(text, vr.x, vr.y + fm.getAscent()); 252: 253: g.setColor(saved); 254: } 255: 256: /** 257: * This method uninstalls the defaults for the given JComponent. 258: * 259: * @param c The JComponent to uninstall defaults for. 260: */ 261: protected void uninstallDefaults(JComponent c) 262: { 263: c.setForeground(null); 264: c.setBackground(null); 265: c.setFont(null); 266: c.setBorder(null); 267: } 268: 269: /** 270: * This method uninstalls listeners for the given JComponent. 271: * 272: * @param c The JComponent to uninstall listeners for. 273: */ 274: protected void uninstallListeners(JComponent c) 275: { 276: } 277: 278: /** 279: * This method uninstalls the UI for the given JComponent. 280: * 281: * @param c The JComponent to uninstall. 282: */ 283: public void uninstallUI(JComponent c) 284: { 285: uninstallDefaults(c); 286: uninstallListeners(c); 287: } 288: }
GNU Classpath (0.17) |