Home » openjdk-7 » javax » swing » border » [javadoc | source]

    1   /*
    2    * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   package javax.swing.border;
   26   
   27   import java.awt.Graphics;
   28   import java.awt.Insets;
   29   import java.awt.Component;
   30   import java.beans.ConstructorProperties;
   31   
   32   /**
   33    * A composite Border class used to compose two Border objects
   34    * into a single border by nesting an inside Border object within
   35    * the insets of an outside Border object.
   36    *
   37    * For example, this class may be used to add blank margin space
   38    * to a component with an existing decorative border:
   39    * <p>
   40    * <code><pre>
   41    *    Border border = comp.getBorder();
   42    *    Border margin = new EmptyBorder(10,10,10,10);
   43    *    comp.setBorder(new CompoundBorder(border, margin));
   44    * </pre></code>
   45    * <p>
   46    * <strong>Warning:</strong>
   47    * Serialized objects of this class will not be compatible with
   48    * future Swing releases. The current serialization support is
   49    * appropriate for short term storage or RMI between applications running
   50    * the same version of Swing.  As of 1.4, support for long term storage
   51    * of all JavaBeans<sup><font size="-2">TM</font></sup>
   52    * has been added to the <code>java.beans</code> package.
   53    * Please see {@link java.beans.XMLEncoder}.
   54    *
   55    * @author David Kloba
   56    */
   57   public class CompoundBorder extends AbstractBorder {
   58       protected Border outsideBorder;
   59       protected Border insideBorder;
   60   
   61       /**
   62        * Creates a compound border with null outside and inside borders.
   63        */
   64       public CompoundBorder() {
   65           this.outsideBorder = null;
   66           this.insideBorder = null;
   67       }
   68   
   69       /**
   70        * Creates a compound border with the specified outside and
   71        * inside borders.  Either border may be null.
   72        * @param outsideBorder the outside border
   73        * @param insideBorder the inside border to be nested
   74        */
   75       @ConstructorProperties({"outsideBorder", "insideBorder"})
   76       public CompoundBorder(Border outsideBorder, Border insideBorder) {
   77           this.outsideBorder = outsideBorder;
   78           this.insideBorder = insideBorder;
   79       }
   80   
   81       /**
   82        * Returns whether or not the compound border is opaque.
   83        *
   84        * @return {@code true} if the inside and outside borders
   85        *         are each either {@code null} or opaque;
   86        *         or {@code false} otherwise
   87        */
   88       @Override
   89       public boolean isBorderOpaque() {
   90           return (outsideBorder == null || outsideBorder.isBorderOpaque()) &&
   91                  (insideBorder == null || insideBorder.isBorderOpaque());
   92       }
   93   
   94       /**
   95        * Paints the compound border by painting the outside border
   96        * with the specified position and size and then painting the
   97        * inside border at the specified position and size offset by
   98        * the insets of the outside border.
   99        * @param c the component for which this border is being painted
  100        * @param g the paint graphics
  101        * @param x the x position of the painted border
  102        * @param y the y position of the painted border
  103        * @param width the width of the painted border
  104        * @param height the height of the painted border
  105        */
  106       public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  107           Insets  nextInsets;
  108           int px, py, pw, ph;
  109   
  110           px = x;
  111           py = y;
  112           pw = width;
  113           ph = height;
  114   
  115           if(outsideBorder != null) {
  116               outsideBorder.paintBorder(c, g, px, py, pw, ph);
  117   
  118               nextInsets = outsideBorder.getBorderInsets(c);
  119               px += nextInsets.left;
  120               py += nextInsets.top;
  121               pw = pw - nextInsets.right - nextInsets.left;
  122               ph = ph - nextInsets.bottom - nextInsets.top;
  123           }
  124           if(insideBorder != null)
  125               insideBorder.paintBorder(c, g, px, py, pw, ph);
  126   
  127       }
  128   
  129       /**
  130        * Reinitialize the insets parameter with this Border's current Insets.
  131        * @param c the component for which this border insets value applies
  132        * @param insets the object to be reinitialized
  133        */
  134       public Insets getBorderInsets(Component c, Insets insets) {
  135           Insets  nextInsets;
  136   
  137           insets.top = insets.left = insets.right = insets.bottom = 0;
  138           if(outsideBorder != null) {
  139               nextInsets = outsideBorder.getBorderInsets(c);
  140               insets.top += nextInsets.top;
  141               insets.left += nextInsets.left;
  142               insets.right += nextInsets.right;
  143               insets.bottom += nextInsets.bottom;
  144           }
  145           if(insideBorder != null) {
  146               nextInsets = insideBorder.getBorderInsets(c);
  147               insets.top += nextInsets.top;
  148               insets.left += nextInsets.left;
  149               insets.right += nextInsets.right;
  150               insets.bottom += nextInsets.bottom;
  151           }
  152           return insets;
  153       }
  154   
  155       /**
  156        * Returns the outside border object.
  157        */
  158       public Border getOutsideBorder() {
  159           return outsideBorder;
  160       }
  161   
  162       /**
  163        * Returns the inside border object.
  164        */
  165       public Border getInsideBorder() {
  166           return insideBorder;
  167       }
  168   }

Home » openjdk-7 » javax » swing » border » [javadoc | source]