1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
30
* (C) Copyright 2000-2005, by Object Refinery Limited.
32
* Original Author: David Gilbert (for Object Refinery Limited);
35
* $Id: CenterLayout.java,v 1.6 2005/11/16 15:58:40 taqua Exp $
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);
44
package org.jfree.layout;
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;
54
* A layout manager that displays a single component in the center of its
57
* @author David Gilbert
59
public class CenterLayout implements LayoutManager, Serializable {
61
/** For serialization. */
62
private static final long serialVersionUID = 469319532333015042L;
65
* Creates a new layout manager.
67
public CenterLayout() {
71
* Returns the preferred size.
73
* @param parent the parent.
75
* @return the preferred size.
77
public Dimension preferredLayoutSize(final Container parent) {
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();
85
(int) d.getWidth() + insets.left + insets.right,
86
(int) d.getHeight() + insets.top + insets.bottom
91
insets.left + insets.right, insets.top + insets.bottom
99
* Returns the minimum size.
101
* @param parent the parent.
103
* @return the minimum size.
105
public Dimension minimumLayoutSize(final Container parent) {
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);
116
return new Dimension(insets.left + insets.right,
117
insets.top + insets.bottom);
124
* Lays out the components.
126
* @param parent the parent.
128
public void layoutContainer(final Container parent) {
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)
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);
153
* @param comp the component.
155
public void addLayoutComponent(final Component comp) {
162
* @param comp the component.
164
public void removeLayoutComponent(final Component comp) {
171
* @param name the component name.
172
* @param comp the component.
174
public void addLayoutComponent(final String name, final Component comp) {
181
* @param name the component name.
182
* @param comp the component.
184
public void removeLayoutComponent(final String name, final Component comp) {