Source for org.jfree.layout.CenterLayout

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * -----------------
  28:  * CenterLayout.java
  29:  * -----------------
  30:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: CenterLayout.java,v 1.6 2005/11/16 15:58:40 taqua Exp $
  36:  *
  37:  * Changes (from 5-Nov-2001)
  38:  * -------------------------
  39:  * 05-Nov-2001 : Changed package to com.jrefinery.layout.* (DG);
  40:  * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.layout;
  45: 
  46: import java.awt.Component;
  47: import java.awt.Container;
  48: import java.awt.Dimension;
  49: import java.awt.Insets;
  50: import java.awt.LayoutManager;
  51: import java.io.Serializable;
  52: 
  53: /**
  54:  * A layout manager that displays a single component in the center of its 
  55:  * container.
  56:  *
  57:  * @author David Gilbert
  58:  */
  59: public class CenterLayout implements LayoutManager, Serializable {
  60: 
  61:     /** For serialization. */
  62:     private static final long serialVersionUID = 469319532333015042L;
  63:     
  64:     /**
  65:      * Creates a new layout manager.
  66:      */
  67:     public CenterLayout() {
  68:     }
  69: 
  70:     /**
  71:      * Returns the preferred size.
  72:      *
  73:      * @param parent  the parent.
  74:      *
  75:      * @return the preferred size.
  76:      */
  77:     public Dimension preferredLayoutSize(final Container parent) {
  78: 
  79:         synchronized (parent.getTreeLock()) {
  80:             final Insets insets = parent.getInsets();
  81:             if (parent.getComponentCount() > 0) {
  82:                 final Component component = parent.getComponent(0);
  83:                 final Dimension d = component.getPreferredSize();
  84:                 return new Dimension(
  85:                     (int) d.getWidth() + insets.left + insets.right,
  86:                     (int) d.getHeight() + insets.top + insets.bottom
  87:                 );
  88:             }
  89:             else {
  90:                 return new Dimension(
  91:                     insets.left + insets.right, insets.top + insets.bottom
  92:                 );
  93:             }
  94:         }
  95: 
  96:     }
  97: 
  98:     /**
  99:      * Returns the minimum size.
 100:      *
 101:      * @param parent  the parent.
 102:      *
 103:      * @return the minimum size.
 104:      */
 105:     public Dimension minimumLayoutSize(final Container parent) {
 106: 
 107:         synchronized (parent.getTreeLock()) {
 108:             final Insets insets = parent.getInsets();
 109:             if (parent.getComponentCount() > 0) {
 110:                 final Component component = parent.getComponent(0);
 111:                 final Dimension d = component.getMinimumSize();
 112:                 return new Dimension(d.width + insets.left + insets.right,
 113:                                  d.height + insets.top + insets.bottom);
 114:             }
 115:             else {
 116:               return new Dimension(insets.left + insets.right,
 117:                                    insets.top + insets.bottom);
 118:             }
 119:         }
 120: 
 121:     }
 122: 
 123:     /**
 124:      * Lays out the components.
 125:      *
 126:      * @param parent  the parent.
 127:      */
 128:     public void layoutContainer(final Container parent) {
 129: 
 130:         synchronized (parent.getTreeLock()) {
 131:             if (parent.getComponentCount() > 0) {
 132:                 final Insets insets = parent.getInsets();
 133:                 final Dimension parentSize = parent.getSize();
 134:                 final Component component = parent.getComponent(0);
 135:                 final Dimension componentSize = component.getPreferredSize();
 136:                 final int xx = insets.left + (
 137:                     Math.max((parentSize.width - insets.left - insets.right
 138:                                       - componentSize.width) / 2, 0)
 139:                 );
 140:                 final int yy = insets.top + (
 141:                     Math.max((parentSize.height - insets.top - insets.bottom
 142:                                       - componentSize.height) / 2, 0));
 143:                 component.setBounds(xx, yy, componentSize.width, 
 144:                         componentSize.height);
 145:             }
 146:         }
 147: 
 148:     }
 149: 
 150:     /**
 151:      * Not used.
 152:      *
 153:      * @param comp  the component.
 154:      */
 155:     public void addLayoutComponent(final Component comp) {
 156:         // not used.
 157:     }
 158: 
 159:     /**
 160:      * Not used.
 161:      *
 162:      * @param comp  the component.
 163:      */
 164:     public void removeLayoutComponent(final Component comp) {
 165:         // not used
 166:     }
 167: 
 168:     /**
 169:      * Not used.
 170:      *
 171:      * @param name  the component name.
 172:      * @param comp  the component.
 173:      */
 174:     public void addLayoutComponent(final String name, final Component comp) {
 175:         // not used
 176:     }
 177: 
 178:     /**
 179:      * Not used.
 180:      *
 181:      * @param name  the component name.
 182:      * @param comp  the component.
 183:      */
 184:     public void removeLayoutComponent(final String name, final Component comp) {
 185:         // not used
 186:     }
 187: 
 188: }