Source for javax.swing.plaf.ComponentUI

   1: /* ComponentUI.java --
   2:    Copyright (C) 2002, 2003, 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;
  40: 
  41: import java.awt.Dimension;
  42: import java.awt.Graphics;
  43: 
  44: import javax.accessibility.Accessible;
  45: import javax.swing.JComponent;
  46: 
  47: /**
  48:  * The abstract base class for all delegates that provide the
  49:  * pluggable look and feel for Swing components. User applications
  50:  * should not need to access this class; it is internal to Swing
  51:  * and the look-and-feel implementations.
  52:  *
  53:  * <p><img src="doc-files/ComponentUI-1.png" width="700" height="550"
  54:  * alt="[UML diagram illustrating the architecture for pluggable
  55:  * look and feels]" /></p>
  56:  *
  57:  * <p>Components such as {@link javax.swing.JSlider} do not directly
  58:  * implement operations related to the look and feel of the user
  59:  * interface, such as painting or layout. Instead, they use a delegate
  60:  * object for all such tasks. In the case of <code>JSlider</code>, the
  61:  * user interface would be provided by some concrete subclass of
  62:  * {@link javax.swing.plaf.SliderUI}.
  63:  *
  64:  * <p>Soon after its creation, a <code>ComponentUI</code> will be sent
  65:  * an {@link #installUI} message. The <code>ComponentUI</code> will
  66:  * react by setting properties such as the border or the background
  67:  * color of the <code>JComponent</code> for which it provides its
  68:  * services. Soon before the end of its lifecycle, the
  69:  * <code>ComponentUI</code> will receive an {@link #uninstallUI}
  70:  * message, at which time the <code>ComponentUI</code> is expected to
  71:  * undo any changes.</p>
  72:  *
  73:  * <p>Note that the <code>ui</code> of a <code>JComponent</code>
  74:  * changes whenever the user switches between look and feels.  For
  75:  * example, the <code>ui</code> property of a <code>JSlider</code>
  76:  * could change from an instance of <code>MetalSliderUI</code> to an
  77:  * instance of <code>FooSliderUI</code>. This switch can happen at any
  78:  * time, but it will always be performed from inside the Swing thread.</p>
  79:  *
  80:  * @author Sascha Brawer (brawer@dandelis.ch)
  81:  */
  82: public abstract class ComponentUI 
  83: {
  84:   /**
  85:    * Constructs a new UI delegate.
  86:    */
  87:   public ComponentUI()
  88:   {
  89:   }
  90:   
  91:   
  92:   /**
  93:    * Sets up the specified component so it conforms the the design
  94:    * guidelines of the implemented look and feel. When the look and
  95:    * feel changes, a <code>ComponentUI</code> delegate is created.
  96:    * The delegate object then receives an <code>installUI</code>
  97:    * message.
  98:    *
  99:    * <p>This method should perform the following tasks:</p>
 100:    *
 101:    * <ul>
 102:    * <li>Set visual properties such as borders, fonts, colors, or
 103:    *     icons. However, no change should be performed for those
 104:    *     properties whose values have been directly set by the client
 105:    *     application. To allow the distinction, LookAndFeels are expected
 106:    *     to use values that implement the {@link UIResource} marker
 107:    *     interface, such as {@link BorderUIResource} or {@link
 108:    *     ColorUIResource}.</li>
 109:    * <li>If necessary, install a {@link java.awt.LayoutManager}.</li>
 110:    * <li>Embed custom sub-components. For instance, the UI delegate
 111:    *     for a {@link javax.swing.JSplitPane} might install a special
 112:    *     component for the divider.</li>
 113:    * <li>Register event listeners.</li>
 114:    * <li>Set up properties related to keyborad navigation, such as
 115:    *     mnemonics or focus traversal policies.</li>
 116:    * </ul>
 117:    *
 118:    * @param c the component for which this delegate will provide
 119:    *        services.
 120:    *
 121:    * @see #uninstallUI
 122:    * @see javax.swing.JComponent#setUI
 123:    * @see javax.swing.JComponent#updateUI
 124:    */
 125:   public void installUI(JComponent c)
 126:   {
 127:     // The default implementation does not change any properties.
 128:   }
 129: 
 130: 
 131:   /**
 132:    * Puts the specified component into the state it had before
 133:    * {@link #installUI} was called.
 134:    *
 135:    * @param c the component for which this delegate has provided
 136:    *        services.
 137:    *
 138:    * @see #installUI
 139:    * @see javax.swing.JComponent#setUI
 140:    * @see javax.swing.JComponent#updateUI
 141:    */
 142:   public void uninstallUI(JComponent c)
 143:   {
 144:     // The default implementation does not change any properties.
 145:   }
 146:   
 147:   
 148:   /**
 149:    * Paints the component according to the design guidelines
 150:    * of the look and feel. Most subclasses will want to override
 151:    * this method.
 152:    *
 153:    * @param g the graphics for painting.
 154:    *
 155:    * @param c the component for which this delegate performs
 156:    *          services.
 157:    */
 158:   public void paint(Graphics g, JComponent c)
 159:   {
 160:   }
 161:   
 162:   
 163:   /**
 164:    * Fills the specified component with its background color
 165:    * (unless the <code>opaque</code> property is <code>false</code>)
 166:    * before calling {@link #paint}.
 167:    *
 168:    * <p>It is unlikely that a subclass needs to override this method.
 169:    * The actual rendering should be performed by the {@link #paint}
 170:    * method.
 171:    *
 172:    * @param g the graphics for painting.
 173:    *
 174:    * @param c the component for which this delegate performs
 175:    *          services.
 176:    *
 177:    * @see #paint
 178:    * @see javax.swing.JComponent#paintComponent
 179:    */
 180:   public void update(Graphics g, JComponent c)
 181:   {
 182:     if (c.isOpaque())
 183:     {
 184:       g.setColor(c.getBackground());
 185:       g.fillRect(0, 0, c.getWidth(), c.getHeight());
 186:     }
 187:     paint(g, c);
 188:   }
 189:   
 190:   
 191:   /**
 192:    * Determines the preferred size of a component. The default
 193:    * implementation returns <code>null</code>, which means that
 194:    * <code>c</code>&#x2019;s layout manager should be asked to
 195:    * calculate the preferred size.
 196:    *
 197:    * @param c the component for which this delegate performs services.
 198:    *
 199:    * @return the preferred size, or <code>null</code> to indicate that
 200:    *         <code>c</code>&#x2019;s layout manager should be asked
 201:    *         for the preferred size.
 202:    */
 203:   public Dimension getPreferredSize(JComponent c)
 204:   {
 205:     return null;
 206:   }
 207:   
 208:   
 209:   /**
 210:    * Determines the minimum size of a component. The default
 211:    * implementation calls {@link #getPreferredSize}, but subclasses
 212:    * might want to override this.
 213:    *
 214:    * @param c the component for which this delegate performs services.
 215:    *
 216:    * @return the minimum size, or <code>null</code> to indicate that
 217:    *         <code>c</code>&#x2019;s layout manager should be asked
 218:    *         to calculate the minimum size.
 219:    */
 220:   public Dimension getMinimumSize(JComponent c)
 221:   {
 222:     return getPreferredSize(c);
 223:   }
 224: 
 225: 
 226:   /**
 227:    * Determines the maximum size of a component. The default
 228:    * implementation calls {@link #getPreferredSize}, but subclasses
 229:    * might want to override this.
 230:    *
 231:    * @param c the component for which this delegate performs services.
 232:    *
 233:    * @return the maximum size, or <code>null</code> to indicate that
 234:    *         <code>c</code>&#x2019;s layout manager should be asked
 235:    *         to calculate the maximum size.
 236:    */
 237:   public Dimension getMaximumSize(JComponent c)
 238:   {
 239:     return getPreferredSize(c);
 240:   }
 241: 
 242: 
 243:   /**
 244:    * Determines whether a click into the component at a specified
 245:    * location is considered as having hit the component. The default
 246:    * implementation checks whether the point falls into the
 247:    * component&#x2019;s bounding rectangle. Some subclasses might want
 248:    * to override this, for example in the case of a rounded button.
 249:    *
 250:    * @param c the component for which this delegate performs services.
 251:    *
 252:    * @param x the x coordinate of the point, relative to the local
 253:    *        coordinate system of the component. Zero would be be
 254:    *        component&#x2019;s left edge, irrespective of the location
 255:    *        inside its parent.
 256:    *
 257:    * @param y the y coordinate of the point, relative to the local
 258:    *        coordinate system of the component. Zero would be be
 259:    *        component&#x2019;s top edge, irrespective of the location
 260:    *        inside its parent.
 261:    */
 262:   public boolean contains(JComponent c, int x, int y)
 263:   {    
 264:     /* JComponent.contains calls the ui delegate for hit
 265:      * testing. Therefore, endless mutual recursion would result if we
 266:      * called c.contains(x, y) here.
 267:      *
 268:      * The previous Classpath implementation called the deprecated
 269:      * method java.awt.Component.inside. In the Sun implementation, it
 270:      * can be observed that inside, other than contains, does not call
 271:      * the ui delegate.  But that inside() behaves different to
 272:      * contains() clearly is in violation of the method contract, and
 273:      * it is not something that a good implementation should rely upon
 274:      * -- even if Classpath ends up being forced to replicate this
 275:      * apparent bug of the Sun implementation.
 276:      */
 277:     return (x >= 0) && (x < c.getWidth())
 278:       && (y >= 0) && (y < c.getHeight());
 279:   }
 280:   
 281:   
 282:   /**
 283:    * Creates a delegate object for the specified component.  Users
 284:    * should use the <code>createUI</code> method of a suitable
 285:    * subclass. The implementation of <code>ComponentUI</code>
 286:    * always throws an error.
 287:    *
 288:    * @param c the component for which a UI delegate is requested.
 289:    */
 290:   public static ComponentUI createUI(JComponent c)
 291:   {
 292:     throw new Error(
 293:       "javax.swing.plaf.ComponentUI does not implement createUI; call "
 294:       + "createUI on a subclass.");
 295:   }
 296:   
 297: 
 298:   /**
 299:    * Counts the number of accessible children in the component.  The
 300:    * default implementation delegates the inquiry to the {@link
 301:    * javax.accessibility.AccessibleContext} of <code>c</code>.
 302:    *
 303:    * @param c the component whose accessible children
 304:    *        are to be counted.
 305:    */
 306:   public int getAccessibleChildrenCount(JComponent c)
 307:   {
 308:     return c.getAccessibleContext().getAccessibleChildrenCount();
 309:   }
 310: 
 311: 
 312:   /**
 313:    * Returns the specified accessible child of the component. The
 314:    * default implementation delegates the inquiry to the {@link
 315:    * javax.accessibility.AccessibleContext} of <code>c</code>.
 316:    *
 317:    * @param i the index of the accessible child, starting at zero.
 318:    *
 319:    * @param c the component whose <code>i</code>-th accessible child
 320:    *        is requested.
 321:    */
 322:   public Accessible getAccessibleChild(JComponent c, int i)
 323:   {
 324:     return c.getAccessibleContext().getAccessibleChild(i);
 325:   }
 326: }