001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components;
042:
043: import java.awt.*;
044:
045: /** EqualFlowLayout is a layout manager that works the same way as FlowLayout.
046: * The only difference is that it sizes the components so that they all have the same width
047: * (a width of widest component).
048: *
049: * @author Ian Formanek
050: * @version 1.00, Nov 12, 1998
051: */
052: public class EqualFlowLayout extends FlowLayout {
053: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
054:
055: /** A JDK 1.1 serial version UID */
056: static final long serialVersionUID = -1996929627282401218L;
057:
058: //~ Constructors -------------------------------------------------------------------------------------------------------------
059:
060: /**
061: * Constructs a new Flow Layout with a centered alignment and a
062: * default 5-unit horizontal and vertical gap.
063: * @since JDK1.0
064: */
065: public EqualFlowLayout() {
066: super ();
067: }
068:
069: /**
070: * Constructs a new Flow Layout with the specified alignment and a
071: * default 5-unit horizontal and vertical gap.
072: * The value of the alignment argument must be one of
073: * <code>FlowLayout.LEFT</code>, <code>FlowLayout.RIGHT</code>,
074: * or <code>FlowLayout.CENTER</code>.
075: * @param align the alignment value
076: * @since JDK1.0
077: */
078: public EqualFlowLayout(int align) {
079: super (align);
080: }
081:
082: /**
083: * Creates a new flow layout manager with the indicated alignment
084: * and the indicated horizontal and vertical gaps.
085: * <p>
086: * The value of the alignment argument must be one of
087: * <code>FlowLayout.LEFT</code>, <code>FlowLayout.RIGHT</code>,
088: * or <code>FlowLayout.CENTER</code>.
089: * @param align the alignment value.
090: * @param hgap the horizontal gap between components.
091: * @param vgap the vertical gap between components.
092: * @since JDK1.0
093: */
094: public EqualFlowLayout(int align, int hgap, int vgap) {
095: super (align, hgap, vgap);
096: }
097:
098: //~ Methods ------------------------------------------------------------------------------------------------------------------
099:
100: /**
101: * Lays out the container. This method lets each component take
102: * its preferred size by reshaping the components in the
103: * target container in order to satisfy the constraints of
104: * this <code>FlowLayout</code> object.
105: * @param target the specified component being laid out.
106: * @see java.awt.Container
107: * @see java.awt.Container#doLayout
108: * @since JDK1.0
109: */
110: public void layoutContainer(Container target) {
111: int maxWidth = getMaximumWidth(target);
112:
113: synchronized (target.getTreeLock()) {
114: Insets insets = target.getInsets();
115: int maxwidth = target.getSize().width
116: - (insets.left + insets.right + (getHgap() * 2));
117: int nmembers = target.getComponentCount();
118: int x = 0;
119: int y = insets.top + getVgap();
120: int rowh = 0;
121: int start = 0;
122:
123: for (int i = 0; i < nmembers; i++) {
124: Component m = target.getComponent(i);
125:
126: if (m.isVisible()) {
127: Dimension d = m.getPreferredSize();
128: d.width = maxWidth;
129: m.setSize(d.width, d.height);
130:
131: if ((x == 0) || ((x + d.width) <= maxwidth)) {
132: if (x > 0) {
133: x += getHgap();
134: }
135:
136: x += d.width;
137: rowh = Math.max(rowh, d.height);
138: } else {
139: moveComponents2(target,
140: insets.left + getHgap(), y, maxwidth
141: - x, rowh, start, i);
142: x = d.width;
143: y += (getVgap() + rowh);
144: rowh = d.height;
145: start = i;
146: }
147: }
148: }
149:
150: moveComponents2(target, insets.left + getHgap(), y,
151: maxwidth - x, rowh, start, nmembers);
152: }
153: }
154:
155: /**
156: * Returns the minimum dimensions needed to layout the components
157: * contained in the specified target container.
158: * @param target the component which needs to be laid out
159: * @return the minimum dimensions to lay out the
160: * subcomponents of the specified container.
161: * @see #preferredLayoutSize
162: * @see java.awt.Container
163: * @see java.awt.Container#doLayout
164: * @since JDK1.0
165: */
166: public Dimension minimumLayoutSize(Container target) {
167: synchronized (target.getTreeLock()) {
168: Dimension dim = new Dimension(0, 0);
169: int nmembers = target.getComponentCount();
170:
171: for (int i = 0; i < nmembers; i++) {
172: Component m = target.getComponent(i);
173:
174: if (m.isVisible()) {
175: Dimension d = m.getMinimumSize();
176: dim.height = Math.max(dim.height, d.height);
177:
178: if (i > 0) {
179: dim.width += getHgap();
180: }
181:
182: dim.width += d.width;
183: }
184: }
185:
186: Insets insets = target.getInsets();
187: dim.width += (insets.left + insets.right + (getHgap() * 2));
188: dim.height += (insets.top + insets.bottom + (getVgap() * 2));
189:
190: return dim;
191: }
192: }
193:
194: /**
195: * Returns the preferred dimensions for this layout given the components
196: * in the specified target container.
197: * @param target the component which needs to be laid out
198: * @return the preferred dimensions to lay out the
199: * subcomponents of the specified container.
200: * @see java.awt.Container
201: * @see #minimumLayoutSize
202: * @see java.awt.Container#getPreferredSize
203: * @since JDK1.0
204: */
205: public Dimension preferredLayoutSize(Container target) {
206: int maxWidth = getMaximumWidth(target);
207:
208: synchronized (target.getTreeLock()) {
209: Dimension dim = new Dimension(0, 0);
210: int nmembers = target.getComponentCount();
211:
212: for (int i = 0; i < nmembers; i++) {
213: Component m = target.getComponent(i);
214:
215: if (m.isVisible()) {
216: Dimension d = m.getPreferredSize();
217: dim.height = Math.max(dim.height, d.height);
218:
219: if (i > 0) {
220: dim.width += getHgap();
221: }
222:
223: dim.width += maxWidth;
224: }
225: }
226:
227: Insets insets = target.getInsets();
228: dim.width += (insets.left + insets.right + (getHgap() * 2));
229: dim.height += (insets.top + insets.bottom + (getVgap() * 2));
230:
231: return dim;
232: }
233: }
234:
235: private static int getMaximumWidth(Container target) {
236: int maxWidth = 0;
237:
238: synchronized (target.getTreeLock()) {
239: int nmembers = target.getComponentCount();
240:
241: for (int i = 0; i < nmembers; i++) {
242: Component m = target.getComponent(i);
243:
244: if (m.isVisible()) {
245: Dimension d = m.getPreferredSize();
246: maxWidth = Math.max(d.width, maxWidth);
247: }
248: }
249: }
250:
251: return maxWidth;
252: }
253:
254: /**
255: * Centers the elements in the specified row, if there is any slack.
256: * @param target the component which needs to be moved
257: * @param x the x coordinate
258: * @param y the y coordinate
259: * @param width the width dimensions
260: * @param height the height dimensions
261: * @param rowStart the beginning of the row
262: * @param rowEnd the the ending of the row
263: */
264: private void moveComponents2(Container target, int x, int y,
265: int width, int height, int rowStart, int rowEnd) {
266: synchronized (target.getTreeLock()) {
267: switch (getAlignment()) {
268: case LEFT:
269: break;
270: case CENTER:
271: x += (width / 2);
272:
273: break;
274: case RIGHT:
275: x += width;
276:
277: break;
278: }
279:
280: for (int i = rowStart; i < rowEnd; i++) {
281: Component m = target.getComponent(i);
282:
283: if (m.isVisible()) {
284: m.setLocation(x, y
285: + ((height - m.getSize().height) / 2));
286: x += (getHgap() + m.getSize().width);
287: }
288: }
289: }
290: }
291: }
|