KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > EqualFlowLayout


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.awt;
20
21 import java.awt.*;
22
23
24 /** EqualFlowLayout is a layout manager that works the same way as FlowLayout.
25 * The only difference is that it sizes the components so that they all have the same width
26 * (a width of widest component).
27 *
28 * @author Ian Formanek
29 * @version 1.00, Nov 12, 1998
30 * @deprecated This class is a relic of NetBeans 2.0. It is not used anywhere
31 * in the NetBeans codebase, but is retained for backward compatibility in the
32 * case it is used elsewhere.
33 */

34 public class EqualFlowLayout extends FlowLayout {
35     /** A JDK 1.1 serial version UID */
36     static final long serialVersionUID = -1996929627282401218L;
37
38     /**
39     * Constructs a new Flow Layout with a centered alignment and a
40     * default 5-unit horizontal and vertical gap.
41     * @since JDK1.0
42     */

43     public EqualFlowLayout() {
44         super();
45     }
46
47     /**
48     * Constructs a new Flow Layout with the specified alignment and a
49     * default 5-unit horizontal and vertical gap.
50     * The value of the alignment argument must be one of
51     * <code>FlowLayout.LEFT</code>, <code>FlowLayout.RIGHT</code>,
52     * or <code>FlowLayout.CENTER</code>.
53     * @param align the alignment value
54     * @since JDK1.0
55     */

56     public EqualFlowLayout(int align) {
57         super(align);
58     }
59
60     /**
61     * Creates a new flow layout manager with the indicated alignment
62     * and the indicated horizontal and vertical gaps.
63     * <p>
64     * The value of the alignment argument must be one of
65     * <code>FlowLayout.LEFT</code>, <code>FlowLayout.RIGHT</code>,
66     * or <code>FlowLayout.CENTER</code>.
67     * @param align the alignment value.
68     * @param hgap the horizontal gap between components.
69     * @param vgap the vertical gap between components.
70     * @since JDK1.0
71     */

72     public EqualFlowLayout(int align, int hgap, int vgap) {
73         super(align, hgap, vgap);
74     }
75
76     private int getMaximumWidth(Container target) {
77         int maxWidth = 0;
78
79         synchronized (target.getTreeLock()) {
80             int nmembers = target.getComponentCount();
81
82             for (int i = 0; i < nmembers; i++) {
83                 Component m = target.getComponent(i);
84
85                 if (m.isVisible()) {
86                     Dimension d = m.getPreferredSize();
87                     maxWidth = Math.max(d.width, maxWidth);
88                 }
89             }
90         }
91
92         return maxWidth;
93     }
94
95     /**
96     * Returns the preferred dimensions for this layout given the components
97     * in the specified target container.
98     * @param target the component which needs to be laid out
99     * @return the preferred dimensions to lay out the
100     * subcomponents of the specified container.
101     * @see Container
102     * @see #minimumLayoutSize
103     * @see java.awt.Container#getPreferredSize
104     * @since JDK1.0
105     */

106     public Dimension preferredLayoutSize(Container target) {
107         int maxWidth = getMaximumWidth(target);
108
109         synchronized (target.getTreeLock()) {
110             Dimension dim = new Dimension(0, 0);
111             int nmembers = target.getComponentCount();
112
113             for (int i = 0; i < nmembers; i++) {
114                 Component m = target.getComponent(i);
115
116                 if (m.isVisible()) {
117                     Dimension d = m.getPreferredSize();
118                     dim.height = Math.max(dim.height, d.height);
119
120                     if (i > 0) {
121                         dim.width += getHgap();
122                     }
123
124                     dim.width += maxWidth;
125                 }
126             }
127
128             Insets insets = target.getInsets();
129             dim.width += (insets.left + insets.right + (getHgap() * 2));
130             dim.height += (insets.top + insets.bottom + (getVgap() * 2));
131
132             return dim;
133         }
134     }
135
136     /**
137     * Returns the minimum dimensions needed to layout the components
138     * contained in the specified target container.
139     * @param target the component which needs to be laid out
140     * @return the minimum dimensions to lay out the
141     * subcomponents of the specified container.
142     * @see #preferredLayoutSize
143     * @see java.awt.Container
144     * @see java.awt.Container#doLayout
145     * @since JDK1.0
146     */

147     public Dimension minimumLayoutSize(Container target) {
148         synchronized (target.getTreeLock()) {
149             Dimension dim = new Dimension(0, 0);
150             int nmembers = target.getComponentCount();
151
152             for (int i = 0; i < nmembers; i++) {
153                 Component m = target.getComponent(i);
154
155                 if (m.isVisible()) {
156                     Dimension d = m.getMinimumSize();
157                     dim.height = Math.max(dim.height, d.height);
158
159                     if (i > 0) {
160                         dim.width += getHgap();
161                     }
162
163                     dim.width += d.width;
164                 }
165             }
166
167             Insets insets = target.getInsets();
168             dim.width += (insets.left + insets.right + (getHgap() * 2));
169             dim.height += (insets.top + insets.bottom + (getVgap() * 2));
170
171             return dim;
172         }
173     }
174
175     /**
176     * Centers the elements in the specified row, if there is any slack.
177     * @param target the component which needs to be moved
178     * @param x the x coordinate
179     * @param y the y coordinate
180     * @param width the width dimensions
181     * @param height the height dimensions
182     * @param rowStart the beginning of the row
183     * @param rowEnd the the ending of the row
184     */

185     private void moveComponents2(Container target, int x, int y, int width, int height, int rowStart, int rowEnd) {
186         synchronized (target.getTreeLock()) {
187             switch (getAlignment()) {
188             case LEFT:
189                 break;
190
191             case CENTER:
192                 x += (width / 2);
193
194                 break;
195
196             case RIGHT:
197                 x += width;
198
199                 break;
200             }
201
202             for (int i = rowStart; i < rowEnd; i++) {
203                 Component m = target.getComponent(i);
204
205                 if (m.isVisible()) {
206                     m.setLocation(x, y + ((height - m.getSize().height) / 2));
207                     x += (getHgap() + m.getSize().width);
208                 }
209             }
210         }
211     }
212
213     /**
214     * Lays out the container. This method lets each component take
215     * its preferred size by reshaping the components in the
216     * target container in order to satisfy the constraints of
217     * this <code>FlowLayout</code> object.
218     * @param target the specified component being laid out.
219     * @see Container
220     * @see java.awt.Container#doLayout
221     * @since JDK1.0
222     */

223     public void layoutContainer(Container target) {
224         int maxWidth = getMaximumWidth(target);
225
226         synchronized (target.getTreeLock()) {
227             Insets insets = target.getInsets();
228             int maxwidth = target.getSize().width - (insets.left + insets.right + (getHgap() * 2));
229             int nmembers = target.getComponentCount();
230             int x = 0;
231             int y = insets.top + getVgap();
232             int rowh = 0;
233             int start = 0;
234
235             for (int i = 0; i < nmembers; i++) {
236                 Component m = target.getComponent(i);
237
238                 if (m.isVisible()) {
239                     Dimension d = m.getPreferredSize();
240                     d.width = maxWidth;
241                     m.setSize(d.width, d.height);
242
243                     if ((x == 0) || ((x + d.width) <= maxwidth)) {
244                         if (x > 0) {
245                             x += getHgap();
246                         }
247
248                         x += d.width;
249                         rowh = Math.max(rowh, d.height);
250                     } else {
251                         moveComponents2(target, insets.left + getHgap(), y, maxwidth - x, rowh, start, i);
252                         x = d.width;
253                         y += (getVgap() + rowh);
254                         rowh = d.height;
255                         start = i;
256                     }
257                 }
258             }
259
260             moveComponents2(target, insets.left + getHgap(), y, maxwidth - x, rowh, start, nmembers);
261         }
262     }
263 }
264
Popular Tags