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.Font;
  44: import java.awt.FontMetrics;
  45: import java.awt.Graphics;
  46: import java.awt.Insets;
  47: import java.awt.Rectangle;
  48: import java.beans.PropertyChangeEvent;
  49: import java.beans.PropertyChangeListener;
  50: 
  51: import javax.swing.JComponent;
  52: import javax.swing.JToolTip;
  53: import javax.swing.LookAndFeel;
  54: import javax.swing.plaf.ComponentUI;
  55: import javax.swing.plaf.ToolTipUI;
  56: import javax.swing.text.View;
  57: 
  58: /**
  59:  * This is the Basic Look and Feel UI class for JToolTip.
  60:  */
  61: public class BasicToolTipUI extends ToolTipUI
  62: {
  63: 
  64:   /**
  65:    * Receives notification when a property of the JToolTip changes.
  66:    * This updates the HTML renderer if appropriate.
  67:    */
  68:   private class PropertyChangeHandler
  69:     implements PropertyChangeListener
  70:   {
  71: 
  72:     public void propertyChange(PropertyChangeEvent e)
  73:     {
  74:       String prop = e.getPropertyName();
  75:       if (prop.equals("tiptext") || prop.equals("font")
  76:           || prop.equals("foreground"))
  77:         {
  78:           JToolTip tip = (JToolTip) e.getSource();
  79:           String text = tip.getTipText();
  80:           BasicHTML.updateRenderer(tip, text);
  81:         }
  82:     }
  83:     
  84:   }
  85: 
  86:   /** The shared instance of BasicToolTipUI used for all ToolTips. */
  87:   private static BasicToolTipUI shared;
  88: 
  89:   /** The tooltip's text */
  90:   private String text;
  91: 
  92:   /**
  93:    * Handles property changes.
  94:    */
  95:   private PropertyChangeListener propertyChangeHandler;
  96: 
  97:   /**
  98:    * Creates a new BasicToolTipUI object.
  99:    */
 100:   public BasicToolTipUI()
 101:   {
 102:     super();
 103:   }
 104: 
 105:   /**
 106:    * This method creates a new BasicToolTip UI for the given 
 107:    * JComponent.
 108:    *
 109:    * @param c The JComponent to create a UI for.
 110:    *
 111:    * @return A BasicToolTipUI that can be used by the given JComponent.
 112:    */
 113:   public static ComponentUI createUI(JComponent c)
 114:   {
 115:     if (shared == null)
 116:       shared = new BasicToolTipUI();
 117:     return shared;
 118:   }
 119: 
 120:   /**
 121:    * This method returns the msximum size of the given JComponent.
 122:    *
 123:    * @param c The JComponent to find a maximum size for.
 124:    *
 125:    * @return The maximum size.
 126:    */
 127:   public Dimension getMaximumSize(JComponent c)
 128:   {
 129:     Dimension d = getPreferredSize(c);
 130:     View view = (View) c.getClientProperty(BasicHTML.propertyKey);
 131:     if (view != null)
 132:       d.width += view.getMaximumSpan(View.X_AXIS)
 133:                  - view.getPreferredSpan(View.X_AXIS);
 134:     return d;
 135:   }
 136: 
 137:   /**
 138:    * This method returns the minimum size of the given JComponent.
 139:    *
 140:    * @param c The JComponent to find a minimum size for.
 141:    *
 142:    * @return The minimum size.
 143:    */
 144:   public Dimension getMinimumSize(JComponent c)
 145:   {
 146:     Dimension d = getPreferredSize(c);
 147:     View view = (View) c.getClientProperty(BasicHTML.propertyKey);
 148:     if (view != null)
 149:       d.width -= view.getPreferredSpan(View.X_AXIS)
 150:                  - view.getMinimumSpan(View.X_AXIS);
 151:     return d;
 152:   }
 153: 
 154:   /**
 155:    * This method returns the preferred size of the given JComponent.
 156:    *
 157:    * @param c The JComponent to find a preferred size for.
 158:    *
 159:    * @return The preferred size.
 160:    */
 161:   public Dimension getPreferredSize(JComponent c)
 162:   {
 163:     JToolTip tip = (JToolTip) c;
 164:     String str = tip.getTipText();
 165:     FontMetrics fm = c.getFontMetrics(c.getFont());
 166:     Insets i = c.getInsets();
 167:     Dimension d = new Dimension(i.left + i.right, i.top + i.bottom);
 168:     if (str != null && ! str.equals(""))
 169:       {
 170:         View view = (View) c.getClientProperty(BasicHTML.propertyKey);
 171:         if (view != null)
 172:           {
 173:             d.width += (int) view.getPreferredSpan(View.X_AXIS);
 174:             d.height += (int) view.getPreferredSpan(View.Y_AXIS);
 175:           }
 176:         else
 177:           {
 178:             d.width += fm.stringWidth(str) + 6;
 179:             d.height += fm.getHeight();
 180:           }
 181:       }
 182:     return d;
 183:   }
 184: 
 185:   /**
 186:    * This method installs the defaults for the given JComponent.
 187:    *
 188:    * @param c The JComponent to install defaults for.
 189:    */
 190:   protected void installDefaults(JComponent c)
 191:   {
 192:     LookAndFeel.installColorsAndFont(c, "ToolTip.background",
 193:                                      "ToolTip.foreground", "ToolTip.font");
 194:     LookAndFeel.installBorder(c, "ToolTip.border");
 195:   }
 196: 
 197:   /**
 198:    * This method installs the listeners for the given JComponent.
 199:    *
 200:    * @param c The JComponent to install listeners for.
 201:    */
 202:   protected void installListeners(JComponent c)
 203:   {
 204:     propertyChangeHandler = new PropertyChangeHandler();
 205:     c.addPropertyChangeListener(propertyChangeHandler);
 206:   }
 207: 
 208:   /**
 209:    * This method installs the UI for the given JComponent.
 210:    *
 211:    * @param c The JComponent to install the UI for.
 212:    */
 213:   public void installUI(JComponent c)
 214:   {
 215:     c.setOpaque(true);
 216:     installDefaults(c);
 217:     BasicHTML.updateRenderer(c, ((JToolTip) c).getTipText());
 218:     installListeners(c);
 219:   }
 220: 
 221:   /**
 222:    * This method paints the given JComponent with the given Graphics object.
 223:    *
 224:    * @param g The Graphics object to paint with.
 225:    * @param c The JComponent to paint.
 226:    */
 227:   public void paint(Graphics g, JComponent c)
 228:   {
 229:     JToolTip tip = (JToolTip) c;
 230: 
 231:     String text = tip.getTipText();
 232:     Font font = c.getFont();
 233:     FontMetrics fm = c.getFontMetrics(font);
 234:     int ascent = fm.getAscent();
 235:     Insets i = c.getInsets();
 236:     Dimension size = c.getSize();
 237:     Rectangle paintR = new Rectangle(i.left, i.top,
 238:                                      size.width - i.left - i.right,
 239:                                      size.height - i.top - i.bottom);
 240:     Color saved = g.getColor();
 241:     Font oldFont = g.getFont();
 242:     g.setColor(Color.BLACK);
 243: 
 244:     View view = (View) c.getClientProperty(BasicHTML.propertyKey);
 245:     if (view != null)
 246:       view.paint(g, paintR);
 247:     else
 248:       g.drawString(text, paintR.x + 3, paintR.y + ascent); 
 249: 
 250:     g.setFont(oldFont);
 251:     g.setColor(saved);   
 252:   }
 253: 
 254:   /**
 255:    * This method uninstalls the defaults for the given JComponent.
 256:    *
 257:    * @param c The JComponent to uninstall defaults for.
 258:    */
 259:   protected void uninstallDefaults(JComponent c)
 260:   {
 261:     c.setForeground(null);
 262:     c.setBackground(null);
 263:     c.setFont(null);
 264:     c.setBorder(null);
 265:   }
 266: 
 267:   /**
 268:    * This method uninstalls listeners for the given JComponent.
 269:    *
 270:    * @param c The JComponent to uninstall listeners for.
 271:    */
 272:   protected void uninstallListeners(JComponent c)
 273:   {
 274:     if (propertyChangeHandler != null)
 275:       {
 276:         c.removePropertyChangeListener(propertyChangeHandler);
 277:         propertyChangeHandler = null;
 278:       }
 279:   }
 280: 
 281:   /**
 282:    * This method uninstalls the UI for the given JComponent.
 283:    *
 284:    * @param c The JComponent to uninstall.
 285:    */
 286:   public void uninstallUI(JComponent c)
 287:   {
 288:     uninstallDefaults(c);
 289:     BasicHTML.updateRenderer(c, "");
 290:     uninstallListeners(c);
 291:   }
 292: }