2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.openide.awt;
46
/** EqualFlowLayout is a layout manager that works the same way as FlowLayout.
47
* The only difference is that it sizes the components so that they all have the same width
48
* (a width of widest component).
50
* @author Ian Formanek
51
* @version 1.00, Nov 12, 1998
52
* @deprecated This class is a relic of NetBeans 2.0. It is not used anywhere
53
* in the NetBeans codebase, but is retained for backward compatibility in the
54
* case it is used elsewhere.
56
public class EqualFlowLayout extends FlowLayout {
57
/** A JDK 1.1 serial version UID */
58
static final long serialVersionUID = -1996929627282401218L;
61
* Constructs a new Flow Layout with a centered alignment and a
62
* default 5-unit horizontal and vertical gap.
65
public EqualFlowLayout() {
70
* Constructs a new Flow Layout with the specified alignment and a
71
* default 5-unit horizontal and vertical gap.
72
* The value of the alignment argument must be one of
73
* <code>FlowLayout.LEFT</code>, <code>FlowLayout.RIGHT</code>,
74
* or <code>FlowLayout.CENTER</code>.
75
* @param align the alignment value
78
public EqualFlowLayout(int align) {
83
* Creates a new flow layout manager with the indicated alignment
84
* and the indicated horizontal and vertical gaps.
86
* The value of the alignment argument must be one of
87
* <code>FlowLayout.LEFT</code>, <code>FlowLayout.RIGHT</code>,
88
* or <code>FlowLayout.CENTER</code>.
89
* @param align the alignment value.
90
* @param hgap the horizontal gap between components.
91
* @param vgap the vertical gap between components.
94
public EqualFlowLayout(int align, int hgap, int vgap) {
95
super(align, hgap, vgap);
98
private int getMaximumWidth(Container target) {
101
synchronized (target.getTreeLock()) {
102
int nmembers = target.getComponentCount();
104
for (int i = 0; i < nmembers; i++) {
105
Component m = target.getComponent(i);
108
Dimension d = m.getPreferredSize();
109
maxWidth = Math.max(d.width, maxWidth);
118
* Returns the preferred dimensions for this layout given the components
119
* in the specified target container.
120
* @param target the component which needs to be laid out
121
* @return the preferred dimensions to lay out the
122
* subcomponents of the specified container.
124
* @see #minimumLayoutSize
125
* @see java.awt.Container#getPreferredSize
128
public Dimension preferredLayoutSize(Container target) {
129
int maxWidth = getMaximumWidth(target);
131
synchronized (target.getTreeLock()) {
132
Dimension dim = new Dimension(0, 0);
133
int nmembers = target.getComponentCount();
135
for (int i = 0; i < nmembers; i++) {
136
Component m = target.getComponent(i);
139
Dimension d = m.getPreferredSize();
140
dim.height = Math.max(dim.height, d.height);
143
dim.width += getHgap();
146
dim.width += maxWidth;
150
Insets insets = target.getInsets();
151
dim.width += (insets.left + insets.right + (getHgap() * 2));
152
dim.height += (insets.top + insets.bottom + (getVgap() * 2));
159
* Returns the minimum dimensions needed to layout the components
160
* contained in the specified target container.
161
* @param target the component which needs to be laid out
162
* @return the minimum dimensions to lay out the
163
* subcomponents of the specified container.
164
* @see #preferredLayoutSize
165
* @see java.awt.Container
166
* @see java.awt.Container#doLayout
169
public Dimension minimumLayoutSize(Container target) {
170
synchronized (target.getTreeLock()) {
171
Dimension dim = new Dimension(0, 0);
172
int nmembers = target.getComponentCount();
174
for (int i = 0; i < nmembers; i++) {
175
Component m = target.getComponent(i);
178
Dimension d = m.getMinimumSize();
179
dim.height = Math.max(dim.height, d.height);
182
dim.width += getHgap();
185
dim.width += d.width;
189
Insets insets = target.getInsets();
190
dim.width += (insets.left + insets.right + (getHgap() * 2));
191
dim.height += (insets.top + insets.bottom + (getVgap() * 2));
198
* Centers the elements in the specified row, if there is any slack.
199
* @param target the component which needs to be moved
200
* @param x the x coordinate
201
* @param y the y coordinate
202
* @param width the width dimensions
203
* @param height the height dimensions
204
* @param rowStart the beginning of the row
205
* @param rowEnd the the ending of the row
207
private void moveComponents2(Container target, int x, int y, int width, int height, int rowStart, int rowEnd) {
208
synchronized (target.getTreeLock()) {
209
switch (getAlignment()) {
224
for (int i = rowStart; i < rowEnd; i++) {
225
Component m = target.getComponent(i);
228
m.setLocation(x, y + ((height - m.getSize().height) / 2));
229
x += (getHgap() + m.getSize().width);
236
* Lays out the container. This method lets each component take
237
* its preferred size by reshaping the components in the
238
* target container in order to satisfy the constraints of
239
* this <code>FlowLayout</code> object.
240
* @param target the specified component being laid out.
242
* @see java.awt.Container#doLayout
245
public void layoutContainer(Container target) {
246
int maxWidth = getMaximumWidth(target);
248
synchronized (target.getTreeLock()) {
249
Insets insets = target.getInsets();
250
int maxwidth = target.getSize().width - (insets.left + insets.right + (getHgap() * 2));
251
int nmembers = target.getComponentCount();
253
int y = insets.top + getVgap();
257
for (int i = 0; i < nmembers; i++) {
258
Component m = target.getComponent(i);
261
Dimension d = m.getPreferredSize();
263
m.setSize(d.width, d.height);
265
if ((x == 0) || ((x + d.width) <= maxwidth)) {
271
rowh = Math.max(rowh, d.height);
273
moveComponents2(target, insets.left + getHgap(), y, maxwidth - x, rowh, start, i);
275
y += (getVgap() + rowh);
282
moveComponents2(target, insets.left + getHgap(), y, maxwidth - x, rowh, start, nmembers);