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:
044: package org.jfree.layout;
045:
046: import java.awt.Component;
047: import java.awt.Container;
048: import java.awt.Dimension;
049: import java.awt.Insets;
050: import java.awt.LayoutManager;
051: import 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: */
059: public 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((int) d.getWidth() + insets.left
085: + insets.right, (int) d.getHeight()
086: + insets.top + insets.bottom);
087: } else {
088: return new Dimension(insets.left + insets.right,
089: insets.top + insets.bottom);
090: }
091: }
092:
093: }
094:
095: /**
096: * Returns the minimum size.
097: *
098: * @param parent the parent.
099: *
100: * @return the minimum size.
101: */
102: public Dimension minimumLayoutSize(final Container parent) {
103:
104: synchronized (parent.getTreeLock()) {
105: final Insets insets = parent.getInsets();
106: if (parent.getComponentCount() > 0) {
107: final Component component = parent.getComponent(0);
108: final Dimension d = component.getMinimumSize();
109: return new Dimension(d.width + insets.left
110: + insets.right, d.height + insets.top
111: + insets.bottom);
112: } else {
113: return new Dimension(insets.left + insets.right,
114: insets.top + insets.bottom);
115: }
116: }
117:
118: }
119:
120: /**
121: * Lays out the components.
122: *
123: * @param parent the parent.
124: */
125: public void layoutContainer(final Container parent) {
126:
127: synchronized (parent.getTreeLock()) {
128: if (parent.getComponentCount() > 0) {
129: final Insets insets = parent.getInsets();
130: final Dimension parentSize = parent.getSize();
131: final Component component = parent.getComponent(0);
132: final Dimension componentSize = component
133: .getPreferredSize();
134: final int xx = insets.left
135: + (Math
136: .max(
137: (parentSize.width - insets.left
138: - insets.right - componentSize.width) / 2,
139: 0));
140: final int yy = insets.top
141: + (Math
142: .max(
143: (parentSize.height - insets.top
144: - insets.bottom - componentSize.height) / 2,
145: 0));
146: component.setBounds(xx, yy, componentSize.width,
147: componentSize.height);
148: }
149: }
150:
151: }
152:
153: /**
154: * Not used.
155: *
156: * @param comp the component.
157: */
158: public void addLayoutComponent(final Component comp) {
159: // not used.
160: }
161:
162: /**
163: * Not used.
164: *
165: * @param comp the component.
166: */
167: public void removeLayoutComponent(final Component comp) {
168: // not used
169: }
170:
171: /**
172: * Not used.
173: *
174: * @param name the component name.
175: * @param comp the component.
176: */
177: public void addLayoutComponent(final String name,
178: final Component comp) {
179: // not used
180: }
181:
182: /**
183: * Not used.
184: *
185: * @param name the component name.
186: * @param comp the component.
187: */
188: public void removeLayoutComponent(final String name,
189: final Component comp) {
190: // not used
191: }
192:
193: }
|