Source for javax.swing.plaf.metal.MetalScrollBarUI

   1: /* MetalScrollBarUI.java
   2:    Copyright (C) 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.metal;
  40: 
  41: import java.awt.Dimension;
  42: import java.awt.Graphics;
  43: import java.awt.Rectangle;
  44: import java.util.HashMap;
  45: 
  46: import javax.swing.JComponent;
  47: import javax.swing.UIDefaults;
  48: import javax.swing.UIManager;
  49: import javax.swing.plaf.ComponentUI;
  50: import javax.swing.plaf.basic.BasicScrollBarUI;
  51: 
  52: public class MetalScrollBarUI
  53:   extends BasicScrollBarUI
  54: {
  55: 
  56:   /** The minimum thumb size */
  57:   private static final Dimension MIN_THUMB_SIZE = new Dimension(18, 18);
  58: 
  59:   // FIXME: maybe replace by a Map of instances when this becomes stateful
  60:   /** The shared UI instance for JScrollBars. */
  61:   private static HashMap instances = null;
  62: 
  63:   /**
  64:    * Constructs a new instance of MetalScrollBarUI.
  65:    */
  66:   public MetalScrollBarUI()
  67:   {
  68:     super();
  69:   }
  70: 
  71:   /**
  72:    * Returns an instance of MetalScrollBarUI.
  73:    *
  74:    * @param component the component for which we return an UI instance
  75:    *
  76:    * @return an instance of MetalScrollBarUI
  77:    */
  78:   public static ComponentUI createUI(JComponent component)
  79:   {
  80:     if (instances == null)
  81:       instances = new HashMap();
  82: 
  83:     Object o = instances.get(component);
  84:     MetalScrollBarUI instance;
  85:     if (o == null)
  86:       {
  87:     instance = new MetalScrollBarUI();
  88:     instances.put(component, instance);
  89:       }
  90:     else
  91:       instance = (MetalScrollBarUI) o;
  92: 
  93:     return instance;
  94:   }
  95: 
  96:   /**
  97:    * Paints the slider button of the ScrollBar.
  98:    *
  99:    * @param g the Graphics context to use
 100:    * @param c the JComponent on which we paint
 101:    * @param thumbBounds the rectangle that is the slider button
 102:    */
 103:   protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
 104:   {
 105:     // first we fill the background
 106:     g.setColor(thumbColor);
 107:     g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width,
 108:                thumbBounds.height);
 109: 
 110:     // draw the outer dark line
 111:     g.setColor(thumbDarkShadowColor);
 112:     g.drawRect(thumbBounds.x, thumbBounds.y, thumbBounds.width - 1,
 113:                thumbBounds.height - 1);
 114: 
 115:     // draw the inner light line
 116:     g.setColor(thumbHighlightColor);
 117:     g.drawLine(thumbBounds.x + 1, thumbBounds.y + 1,
 118:                thumbBounds.x + thumbBounds.width - 2,
 119:                thumbBounds.y + 1);
 120:     g.drawLine(thumbBounds.x + 1, thumbBounds.y + 1,
 121:                thumbBounds.x + 1,
 122:                thumbBounds.y + thumbBounds.height - 2);
 123: 
 124:     // draw the shadow line
 125:     UIDefaults def = UIManager.getLookAndFeelDefaults();
 126:     g.setColor(def.getColor("ScrollBar.shadow"));
 127:     g.drawLine(thumbBounds.x + 1, thumbBounds.y + thumbBounds.height,
 128:                thumbBounds.x + thumbBounds.width,
 129:                thumbBounds.y + thumbBounds.height);
 130: 
 131:     // draw the pattern
 132:     MetalUtils.fillMetalPattern(g, thumbBounds.x + 3, thumbBounds.y + 3,
 133:                                 thumbBounds.width - 6, thumbBounds.height - 6,
 134:                                 thumbHighlightColor, thumbDarkShadowColor);
 135:   }
 136: 
 137:   /**
 138:    * This method returns the minimum thumb size.
 139:    *
 140:    * @return The minimum thumb size.
 141:    */
 142:   protected Dimension getMinimumThumbSize()
 143:   {
 144:     return MIN_THUMB_SIZE;
 145:   }
 146: }