Source for javax.swing.plaf.basic.BasicSeparatorUI

   1: /* BasicSeparatorUI.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.Dimension;
  43: import java.awt.Graphics;
  44: import java.awt.Rectangle;
  45: 
  46: import javax.swing.JComponent;
  47: import javax.swing.JSeparator;
  48: import javax.swing.SwingUtilities;
  49: import javax.swing.UIManager;
  50: import javax.swing.plaf.ComponentUI;
  51: import javax.swing.plaf.SeparatorUI;
  52: 
  53: /**
  54:  * The Basic Look and Feel UI delegate for JSeparator.
  55:  */
  56: public class BasicSeparatorUI extends SeparatorUI
  57: {
  58:   /** The shadow color. */
  59:   protected Color shadow;
  60: 
  61:   /** The highlight color. */
  62:   protected Color highlight;
  63: 
  64:   /**
  65:    * Creates a new UI delegate for the given JComponent.
  66:    *
  67:    * @param c The JComponent to create a delegate for.
  68:    *
  69:    * @return A new BasicSeparatorUI.
  70:    */
  71:   public static ComponentUI createUI(JComponent c)
  72:   {
  73:     return new BasicSeparatorUI();
  74:   }
  75: 
  76:   /**
  77:    * This method installs the UI for the given JComponent.
  78:    * This can include installing defaults, listeners, and
  79:    * initializing any instance data.
  80:    *
  81:    * @param c The JComponent that is having this UI installed.
  82:    */
  83:   public void installUI(JComponent c)
  84:   {
  85:     super.installUI(c);
  86: 
  87:     if (c instanceof JSeparator)
  88:       {
  89:     JSeparator s = (JSeparator) c;
  90: 
  91:     installDefaults(s);
  92:     installListeners(s);
  93:       }
  94:   }
  95: 
  96:   /**
  97:    * Uninstalls the UI for the given JComponent. This
  98:    * method reverses what was done when installing
  99:    * the UI on the JComponent.
 100:    *
 101:    * @param c The JComponent that is having this UI uninstalled.
 102:    */
 103:   public void uninstallUI(JComponent c)
 104:   {
 105:     if (c instanceof JSeparator)
 106:       {
 107:     JSeparator s = (JSeparator) c;
 108: 
 109:     uninstallListeners(s);
 110:     uninstallDefaults(s);
 111:       }
 112:   }
 113: 
 114:   /**
 115:    * This method installs the defaults that are given by
 116:    * the Basic Look and Feel.
 117:    *
 118:    * @param s The JSeparator that is being installed.
 119:    */
 120:   protected void installDefaults(JSeparator s)
 121:   {
 122:     shadow = UIManager.getColor("Separator.shadow");
 123:     highlight = UIManager.getColor("Separator.highlight");
 124:     s.setOpaque(false);
 125:   }
 126: 
 127:   /**
 128:    * This method removes the defaults that were given
 129:    * by the Basic Look and Feel.
 130:    *
 131:    * @param s The JSeparator that is being uninstalled.
 132:    */
 133:   protected void uninstallDefaults(JSeparator s)
 134:   {
 135:     shadow = null;
 136:     highlight = null;
 137:   }
 138: 
 139:   /**
 140:    * This method installs any listeners that need
 141:    * to be attached to the JSeparator or any of its 
 142:    * components.
 143:    *
 144:    * @param s The JSeparator that is being installed.
 145:    */
 146:   protected void installListeners(JSeparator s)
 147:   {
 148:     // Separators don't receive events.
 149:   }
 150: 
 151:   /**
 152:    * This method uninstalls any listeners that
 153:    * were installed during the install UI process.
 154:    *
 155:    * @param s The JSeparator that is being uninstalled.
 156:    */
 157:   protected void uninstallListeners(JSeparator s)
 158:   {
 159:     // Separators don't receive events.  
 160:   }
 161: 
 162:   /**
 163:    * The separator is made of two lines. The top line will be 
 164:    * the shadow color (or left line if it's vertical). The bottom 
 165:    * or right line will be the highlight color. The two lines will 
 166:    * be centered inside the bounds box. If the separator is horizontal, 
 167:    * then it will be vertically centered, or if it's vertical, it will 
 168:    * be horizontally centered.
 169:    *
 170:    * @param g The Graphics object to paint with
 171:    * @param c The JComponent to paint.
 172:    */
 173:   public void paint(Graphics g, JComponent c)
 174:   {
 175:     Rectangle r = new Rectangle();
 176:     SwingUtilities.calculateInnerArea(c, r);
 177:     Color saved = g.getColor();
 178:     
 179:     JSeparator s;
 180:     if (c instanceof JSeparator)
 181:       s = (JSeparator) c;
 182:     else
 183:       return;
 184:       
 185:     if (s.getOrientation() == JSeparator.HORIZONTAL)
 186:       {    
 187:         int midAB = r.height / 2;
 188:         g.setColor(shadow);
 189:         g.drawLine(r.x, r.y + midAB - 1, r.x + r.width, r.y + midAB - 1);
 190: 
 191:         g.setColor(highlight);
 192:         g.fillRect(r.x, r.y + midAB, r.x + r.width, r.y + midAB);
 193:       }
 194:       else
 195:       {
 196:         int midAD = r.height / 2 + r.y;
 197:         g.setColor(shadow);
 198:         g.drawLine(r.x, r.y, r.x, r.y + r.height);
 199: 
 200:         g.setColor(highlight);
 201:         g.fillRect(r.x + midAD, r.y + r.height, r.x + midAD, r.y + r.height);
 202:       }
 203:     g.setColor(saved);
 204:   }
 205: 
 206:   /**
 207:    * This method returns the preferred size of the 
 208:    * JComponent.
 209:    *
 210:    * @param c The JComponent to measure.
 211:    *
 212:    * @return The preferred size.
 213:    */
 214:   public Dimension getPreferredSize(JComponent c)
 215:   {
 216:     Dimension pref = new Dimension(2, 0);
 217:     if (c instanceof JSeparator)
 218:       {
 219:     JSeparator s = (JSeparator) c;
 220:     if (s.getOrientation() == JSeparator.HORIZONTAL)
 221:           pref = new Dimension(0, 2);
 222:       }
 223:     return pref;
 224:   }
 225: 
 226:   /**
 227:    * This method returns the minimum size of the
 228:    * JComponent.
 229:    *
 230:    * @param c The JComponent to measure.
 231:    *
 232:    * @return The minimum size.
 233:    */
 234:   public Dimension getMinimumSize(JComponent c)
 235:   {
 236:     return new Dimension(0, 0);
 237:   }
 238: 
 239:   /**
 240:    * This method returns the maximum size of the
 241:    * JComponent.
 242:    *
 243:    * @param c The JComponent to measure.
 244:    *
 245:    * @return The maximum size.
 246:    */
 247:   public Dimension getMaximumSize(JComponent c)
 248:   {
 249:     return new Dimension(Short.MAX_VALUE,
 250:                          Short.MAX_VALUE);
 251:   }
 252: }