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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.css.visual.ui;
042:
043: import java.awt.*;
044:
045: // Grid Layout which allows components of differrent sizes
046: public class FlexibleGridLayout extends GridLayout {
047: public FlexibleGridLayout() {
048: this (1, 0, 0, 0);
049: }
050:
051: public FlexibleGridLayout(int rows, int cols) {
052: this (rows, cols, 0, 0);
053: }
054:
055: public FlexibleGridLayout(int rows, int cols, int hgap, int vgap) {
056: super (rows, cols, hgap, vgap);
057: }
058:
059: public Dimension preferredLayoutSize(Container parent) {
060: //System.err.println("preferredLayoutSize");
061: synchronized (parent.getTreeLock()) {
062: Insets insets = parent.getInsets();
063: int ncomponents = parent.getComponentCount();
064: int nrows = getRows();
065: int ncols = getColumns();
066: if (nrows > 0) {
067: ncols = (ncomponents + nrows - 1) / nrows;
068: } else {
069: nrows = (ncomponents + ncols - 1) / ncols;
070: }
071: int[] w = new int[ncols];
072: int[] h = new int[nrows];
073: for (int i = 0; i < ncomponents; i++) {
074: int r = i / ncols;
075: int c = i % ncols;
076: Component comp = parent.getComponent(i);
077: Dimension d = comp.getPreferredSize();
078: if (w[c] < d.width) {
079: w[c] = d.width;
080: }
081: if (h[r] < d.height) {
082: h[r] = d.height;
083: }
084: }
085: int nw = 0;
086: for (int j = 0; j < ncols; j++) {
087: nw += w[j];
088: }
089: int nh = 0;
090: for (int i = 0; i < nrows; i++) {
091: nh += h[i];
092: }
093: return new Dimension(insets.left + insets.right + nw
094: + (ncols - 1) * getHgap(), insets.top
095: + insets.bottom + nh + (nrows - 1) * getVgap());
096: }
097: }
098:
099: public Dimension minimumLayoutSize(Container parent) {
100: System.err.println("minimumLayoutSize");
101: synchronized (parent.getTreeLock()) {
102: Insets insets = parent.getInsets();
103: int ncomponents = parent.getComponentCount();
104: int nrows = getRows();
105: int ncols = getColumns();
106: if (nrows > 0) {
107: ncols = (ncomponents + nrows - 1) / nrows;
108: } else {
109: nrows = (ncomponents + ncols - 1) / ncols;
110: }
111: int[] w = new int[ncols];
112: int[] h = new int[nrows];
113: for (int i = 0; i < ncomponents; i++) {
114: int r = i / ncols;
115: int c = i % ncols;
116: Component comp = parent.getComponent(i);
117: Dimension d = comp.getMinimumSize();
118: if (w[c] < d.width) {
119: w[c] = d.width;
120: }
121: if (h[r] < d.height) {
122: h[r] = d.height;
123: }
124: }
125: int nw = 0;
126: for (int j = 0; j < ncols; j++) {
127: nw += w[j];
128: }
129: int nh = 0;
130: for (int i = 0; i < nrows; i++) {
131: nh += h[i];
132: }
133: return new Dimension(insets.left + insets.right + nw
134: + (ncols - 1) * getHgap(), insets.top
135: + insets.bottom + nh + (nrows - 1) * getVgap());
136: }
137: }
138:
139: public void layoutContainer(Container parent) {
140: //System.err.println("layoutContainer");
141: synchronized (parent.getTreeLock()) {
142: Insets insets = parent.getInsets();
143: int ncomponents = parent.getComponentCount();
144: int nrows = getRows();
145: int ncols = getColumns();
146: if (ncomponents == 0) {
147: return;
148: }
149: if (nrows > 0) {
150: ncols = (ncomponents + nrows - 1) / nrows;
151: } else {
152: nrows = (ncomponents + ncols - 1) / ncols;
153: }
154: int hgap = getHgap();
155: int vgap = getVgap();
156: // scaling factors
157: Dimension pd = preferredLayoutSize(parent);
158: double sw = (1.0 * parent.getWidth()) / pd.width;
159: double sh = (1.0 * parent.getHeight()) / pd.height;
160: // scale
161: int[] w = new int[ncols];
162: int[] h = new int[nrows];
163: for (int i = 0; i < ncomponents; i++) {
164: int r = i / ncols;
165: int c = i % ncols;
166: Component comp = parent.getComponent(i);
167: Dimension d = comp.getPreferredSize();
168: d.width = (int) (sw * d.width);
169: d.height = (int) (sh * d.height);
170: if (w[c] < d.width) {
171: w[c] = d.width;
172: }
173: if (h[r] < d.height) {
174: h[r] = d.height;
175: }
176: }
177: for (int c = 0, x = insets.left; c < ncols; c++) {
178: for (int r = 0, y = insets.top; r < nrows; r++) {
179: int i = r * ncols + c;
180: if (i < ncomponents) {
181: parent.getComponent(i).setBounds(x, y, w[c],
182: h[r]);
183: }
184: y += h[r] + vgap;
185: }
186: x += w[c] + hgap;
187: }
188: }
189: }
190: }
|