~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to openide/awt/src/org/openide/awt/EqualFlowLayout.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.openide.awt;
 
42
 
 
43
import java.awt.*;
 
44
 
 
45
 
 
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).
 
49
*
 
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.
 
55
*/
 
56
public class EqualFlowLayout extends FlowLayout {
 
57
    /** A JDK 1.1 serial version UID */
 
58
    static final long serialVersionUID = -1996929627282401218L;
 
59
 
 
60
    /**
 
61
    * Constructs a new Flow Layout with a centered alignment and a
 
62
    * default 5-unit horizontal and vertical gap.
 
63
    * @since JDK1.0
 
64
    */
 
65
    public EqualFlowLayout() {
 
66
        super();
 
67
    }
 
68
 
 
69
    /**
 
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
 
76
    * @since JDK1.0
 
77
    */
 
78
    public EqualFlowLayout(int align) {
 
79
        super(align);
 
80
    }
 
81
 
 
82
    /**
 
83
    * Creates a new flow layout manager with the indicated alignment
 
84
    * and the indicated horizontal and vertical gaps.
 
85
    * <p>
 
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.
 
92
    * @since      JDK1.0
 
93
    */
 
94
    public EqualFlowLayout(int align, int hgap, int vgap) {
 
95
        super(align, hgap, vgap);
 
96
    }
 
97
 
 
98
    private int getMaximumWidth(Container target) {
 
99
        int maxWidth = 0;
 
100
 
 
101
        synchronized (target.getTreeLock()) {
 
102
            int nmembers = target.getComponentCount();
 
103
 
 
104
            for (int i = 0; i < nmembers; i++) {
 
105
                Component m = target.getComponent(i);
 
106
 
 
107
                if (m.isVisible()) {
 
108
                    Dimension d = m.getPreferredSize();
 
109
                    maxWidth = Math.max(d.width, maxWidth);
 
110
                }
 
111
            }
 
112
        }
 
113
 
 
114
        return maxWidth;
 
115
    }
 
116
 
 
117
    /**
 
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.
 
123
    * @see Container
 
124
    * @see #minimumLayoutSize
 
125
    * @see       java.awt.Container#getPreferredSize
 
126
    * @since     JDK1.0
 
127
    */
 
128
    public Dimension preferredLayoutSize(Container target) {
 
129
        int maxWidth = getMaximumWidth(target);
 
130
 
 
131
        synchronized (target.getTreeLock()) {
 
132
            Dimension dim = new Dimension(0, 0);
 
133
            int nmembers = target.getComponentCount();
 
134
 
 
135
            for (int i = 0; i < nmembers; i++) {
 
136
                Component m = target.getComponent(i);
 
137
 
 
138
                if (m.isVisible()) {
 
139
                    Dimension d = m.getPreferredSize();
 
140
                    dim.height = Math.max(dim.height, d.height);
 
141
 
 
142
                    if (i > 0) {
 
143
                        dim.width += getHgap();
 
144
                    }
 
145
 
 
146
                    dim.width += maxWidth;
 
147
                }
 
148
            }
 
149
 
 
150
            Insets insets = target.getInsets();
 
151
            dim.width += (insets.left + insets.right + (getHgap() * 2));
 
152
            dim.height += (insets.top + insets.bottom + (getVgap() * 2));
 
153
 
 
154
            return dim;
 
155
        }
 
156
    }
 
157
 
 
158
    /**
 
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
 
167
    * @since     JDK1.0
 
168
    */
 
169
    public Dimension minimumLayoutSize(Container target) {
 
170
        synchronized (target.getTreeLock()) {
 
171
            Dimension dim = new Dimension(0, 0);
 
172
            int nmembers = target.getComponentCount();
 
173
 
 
174
            for (int i = 0; i < nmembers; i++) {
 
175
                Component m = target.getComponent(i);
 
176
 
 
177
                if (m.isVisible()) {
 
178
                    Dimension d = m.getMinimumSize();
 
179
                    dim.height = Math.max(dim.height, d.height);
 
180
 
 
181
                    if (i > 0) {
 
182
                        dim.width += getHgap();
 
183
                    }
 
184
 
 
185
                    dim.width += d.width;
 
186
                }
 
187
            }
 
188
 
 
189
            Insets insets = target.getInsets();
 
190
            dim.width += (insets.left + insets.right + (getHgap() * 2));
 
191
            dim.height += (insets.top + insets.bottom + (getVgap() * 2));
 
192
 
 
193
            return dim;
 
194
        }
 
195
    }
 
196
 
 
197
    /**
 
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
 
206
    */
 
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()) {
 
210
            case LEFT:
 
211
                break;
 
212
 
 
213
            case CENTER:
 
214
                x += (width / 2);
 
215
 
 
216
                break;
 
217
 
 
218
            case RIGHT:
 
219
                x += width;
 
220
 
 
221
                break;
 
222
            }
 
223
 
 
224
            for (int i = rowStart; i < rowEnd; i++) {
 
225
                Component m = target.getComponent(i);
 
226
 
 
227
                if (m.isVisible()) {
 
228
                    m.setLocation(x, y + ((height - m.getSize().height) / 2));
 
229
                    x += (getHgap() + m.getSize().width);
 
230
                }
 
231
            }
 
232
        }
 
233
    }
 
234
 
 
235
    /**
 
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.
 
241
    * @see Container
 
242
    * @see       java.awt.Container#doLayout
 
243
    * @since     JDK1.0
 
244
    */
 
245
    public void layoutContainer(Container target) {
 
246
        int maxWidth = getMaximumWidth(target);
 
247
 
 
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();
 
252
            int x = 0;
 
253
            int y = insets.top + getVgap();
 
254
            int rowh = 0;
 
255
            int start = 0;
 
256
 
 
257
            for (int i = 0; i < nmembers; i++) {
 
258
                Component m = target.getComponent(i);
 
259
 
 
260
                if (m.isVisible()) {
 
261
                    Dimension d = m.getPreferredSize();
 
262
                    d.width = maxWidth;
 
263
                    m.setSize(d.width, d.height);
 
264
 
 
265
                    if ((x == 0) || ((x + d.width) <= maxwidth)) {
 
266
                        if (x > 0) {
 
267
                            x += getHgap();
 
268
                        }
 
269
 
 
270
                        x += d.width;
 
271
                        rowh = Math.max(rowh, d.height);
 
272
                    } else {
 
273
                        moveComponents2(target, insets.left + getHgap(), y, maxwidth - x, rowh, start, i);
 
274
                        x = d.width;
 
275
                        y += (getVgap() + rowh);
 
276
                        rowh = d.height;
 
277
                        start = i;
 
278
                    }
 
279
                }
 
280
            }
 
281
 
 
282
            moveComponents2(target, insets.left + getHgap(), y, maxwidth - x, rowh, start, nmembers);
 
283
        }
 
284
    }
 
285
}