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