Source for javax.swing.plaf.basic.BasicToolTipUI

   1: /* BasicToolTipUI.java --
   2:    Copyright (C) 2004, 2005 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.Dimension;
  43: import java.awt.FontMetrics;
  44: import java.awt.Graphics;
  45: import java.awt.Insets;
  46: import java.awt.Rectangle;
  47: import java.awt.Toolkit;
  48: 
  49: import javax.swing.JComponent;
  50: import javax.swing.JToolTip;
  51: import javax.swing.LookAndFeel;
  52: import javax.swing.SwingConstants;
  53: import javax.swing.SwingUtilities;
  54: import javax.swing.plaf.ComponentUI;
  55: import javax.swing.plaf.ToolTipUI;
  56: 
  57: /**
  58:  * This is the Basic Look and Feel UI class for JToolTip.
  59:  */
  60: public class BasicToolTipUI extends ToolTipUI
  61: {
  62: 
  63:   /** The shared instance of BasicToolTipUI used for all ToolTips. */
  64:   private static BasicToolTipUI shared;
  65: 
  66:   /** The tooltip's text */
  67:   private String text;
  68: 
  69:   /**
  70:    * Creates a new BasicToolTipUI object.
  71:    */
  72:   public BasicToolTipUI()
  73:   {
  74:     super();
  75:   }
  76: 
  77:   /**
  78:    * This method creates a new BasicToolTip UI for the given 
  79:    * JComponent.
  80:    *
  81:    * @param c The JComponent to create a UI for.
  82:    *
  83:    * @return A BasicToolTipUI that can be used by the given JComponent.
  84:    */
  85:   public static ComponentUI createUI(JComponent c)
  86:   {
  87:     if (shared == null)
  88:       shared = new BasicToolTipUI();
  89:     return shared;
  90:   }
  91: 
  92:   /**
  93:    * This method returns the msximum size of the given JComponent.
  94:    *
  95:    * @param c The JComponent to find a maximum size for.
  96:    *
  97:    * @return The maximum size.
  98:    */
  99:   public Dimension getMaximumSize(JComponent c)
 100:   {
 101:     return getPreferredSize(c);
 102:   }
 103: 
 104:   /**
 105:    * This method returns the minimum size of the given JComponent.
 106:    *
 107:    * @param c The JComponent to find a minimum size for.
 108:    *
 109:    * @return The minimum size.
 110:    */
 111:   public Dimension getMinimumSize(JComponent c)
 112:   {
 113:     return getPreferredSize(c);
 114:   }
 115: 
 116:   /**
 117:    * This method returns the preferred size of the given JComponent.
 118:    *
 119:    * @param c The JComponent to find a preferred size for.
 120:    *
 121:    * @return The preferred size.
 122:    */
 123:   public Dimension getPreferredSize(JComponent c)
 124:   {
 125:     JToolTip tip = (JToolTip) c;
 126:     FontMetrics fm;
 127:     Toolkit g = tip.getToolkit();
 128:     text = tip.getTipText();
 129:     
 130:     Rectangle vr = new Rectangle();
 131:     Rectangle ir = new Rectangle();
 132:     Rectangle tr = new Rectangle();
 133:     Insets insets = tip.getInsets();
 134:     fm = g.getFontMetrics(tip.getFont());
 135:     SwingUtilities.layoutCompoundLabel(tip, fm, text, null,
 136:                                        SwingConstants.CENTER,
 137:                                        SwingConstants.CENTER,
 138:                                        SwingConstants.CENTER,
 139:                                        SwingConstants.CENTER, vr, ir, tr, 0);
 140:     return new Dimension(insets.left + tr.width + insets.right,
 141:                          insets.top + tr.height + insets.bottom);
 142:   }
 143: 
 144:   /**
 145:    * This method installs the defaults for the given JComponent.
 146:    *
 147:    * @param c The JComponent to install defaults for.
 148:    */
 149:   protected void installDefaults(JComponent c)
 150:   {
 151:     LookAndFeel.installColorsAndFont(c, "ToolTip.background",
 152:                                      "ToolTip.foreground", "ToolTip.font");
 153:     LookAndFeel.installBorder(c, "ToolTip.border");
 154:   }
 155: 
 156:   /**
 157:    * This method installs the listeners for the given JComponent.
 158:    *
 159:    * @param c The JComponent to install listeners for.
 160:    */
 161:   protected void installListeners(JComponent c)
 162:   {
 163:     // TODO: Implement this properly.
 164:   }
 165: 
 166:   /**
 167:    * This method installs the UI for the given JComponent.
 168:    *
 169:    * @param c The JComponent to install the UI for.
 170:    */
 171:   public void installUI(JComponent c)
 172:   {
 173:     c.setOpaque(true);
 174:     installDefaults(c);
 175:     installListeners(c);
 176:   }
 177: 
 178:   /**
 179:    * This method paints the given JComponent with the given Graphics object.
 180:    *
 181:    * @param g The Graphics object to paint with.
 182:    * @param c The JComponent to paint.
 183:    */
 184:   public void paint(Graphics g, JComponent c)
 185:   {
 186:     JToolTip tip = (JToolTip) c;
 187: 
 188:     String text = tip.getTipText();
 189:     Toolkit t = tip.getToolkit();
 190:     if (text == null)
 191:       return;
 192: 
 193:     Rectangle vr = new Rectangle();
 194:     vr = SwingUtilities.calculateInnerArea(tip, vr);
 195:     Rectangle ir = new Rectangle();
 196:     Rectangle tr = new Rectangle();
 197:     FontMetrics fm = t.getFontMetrics(tip.getFont());
 198:     int ascent = fm.getAscent();
 199:     SwingUtilities.layoutCompoundLabel(tip, fm, text, null,
 200:                                        SwingConstants.CENTER,
 201:                                        SwingConstants.CENTER,
 202:                                        SwingConstants.CENTER,
 203:                                        SwingConstants.CENTER, vr, ir, tr, 0);
 204:     Color saved = g.getColor();
 205:     g.setColor(Color.BLACK);
 206: 
 207:     g.drawString(text, vr.x, vr.y + ascent); 
 208: 
 209:     g.setColor(saved);   
 210:   }
 211: 
 212:   /**
 213:    * This method uninstalls the defaults for the given JComponent.
 214:    *
 215:    * @param c The JComponent to uninstall defaults for.
 216:    */
 217:   protected void uninstallDefaults(JComponent c)
 218:   {
 219:     c.setForeground(null);
 220:     c.setBackground(null);
 221:     c.setFont(null);
 222:     c.setBorder(null);
 223:   }
 224: 
 225:   /**
 226:    * This method uninstalls listeners for the given JComponent.
 227:    *
 228:    * @param c The JComponent to uninstall listeners for.
 229:    */
 230:   protected void uninstallListeners(JComponent c)
 231:   {
 232:     // TODO: Implement this properly.
 233:   }
 234: 
 235:   /**
 236:    * This method uninstalls the UI for the given JComponent.
 237:    *
 238:    * @param c The JComponent to uninstall.
 239:    */
 240:   public void uninstallUI(JComponent c)
 241:   {
 242:     uninstallDefaults(c);
 243:     uninstallListeners(c);
 244:   }
 245: }