    001/* ========================================================================
    002 * JCommon : a free general purpose class library for the Java(tm) platform
    003 * ========================================================================
    004 *
    005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006 *
    007 * Project Info:  http://www.jfree.org/jcommon/index.html
    008 *
    009 * This library is free software; you can redistribute it and/or modify it
    010 * under the terms of the GNU Lesser General Public License as published by
    011 * the Free Software Foundation; either version 2.1 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This library is distributed in the hope that it will be useful, but
    015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017 * License for more details.
    018 *
    019 * You should have received a copy of the GNU Lesser General Public
    020 * License along with this library; if not, write to the Free Software
    021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022 * USA.
    023 *
    024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025 * in the United States and other countries.]
    026 *
    027 * -----------------
    028 * CenterLayout.java
    029 * -----------------
    030 * (C) Copyright 2000-2005, by Object Refinery Limited.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   -;
    034 *
    035 * $Id: CenterLayout.java,v 1.6 2005/11/16 15:58:40 taqua Exp $
    036 *
    037 * Changes (from 5-Nov-2001)
    038 * -------------------------
    039 * 05-Nov-2001 : Changed package to com.jrefinery.layout.* (DG);
    040 * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    041 *
    042 */
    043
    044package org.jfree.layout;
    045
    046import java.awt.Component;
    047import java.awt.Container;
    048import java.awt.Dimension;
    049import java.awt.Insets;
    050import java.awt.LayoutManager;
    051import java.io.Serializable;
    052
    053/**
    054 * A layout manager that displays a single component in the center of its
    055 * container.
    056 *
    057 * @author David Gilbert
    058 */
    059public class CenterLayout implements LayoutManager, Serializable {
    060
    061    /** For serialization. */
    062    private static final long serialVersionUID = 469319532333015042L;
    063
    064    /**
    065     * Creates a new layout manager.
    066     */
    067    public CenterLayout() {
    068    }
    069
    070    /**
    071     * Returns the preferred size.
    072     *
    073     * @param parent  the parent.
    074     *
    075     * @return the preferred size.
    076     */
    077    public Dimension preferredLayoutSize(final Container parent) {
    078
    079        synchronized (parent.getTreeLock()) {
    080            final Insets insets = parent.getInsets();
    081            if (parent.getComponentCount() > 0) {
    082                final Component component = parent.getComponent(0);
    083                final Dimension d = component.getPreferredSize();
    084                return new Dimension(
    085                    (int) d.getWidth() + insets.left + insets.right,
    086                    (int) d.getHeight() + insets.top + insets.bottom
    087                );
    088            }
    089            else {
    090                return new Dimension(
    091                    insets.left + insets.right, insets.top + insets.bottom
    092                );
    093            }
    094        }
    095
    096    }
    097
    098    /**
    099     * 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}