001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------
028: * FormatLayout.java
029: * -----------------
030: * (C) Copyright 2000-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: FormatLayout.java,v 1.4 2005/10/18 13:16:50 mungady Exp $
036: *
037: * Changes (from 26-Oct-2001)
038: * --------------------------
039: * 26-Oct-2001 : Changed package to com.jrefinery.layout.* (DG);
040: * 26-Jun-2002 : Removed redundant code (DG);
041: * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
042: *
043: */
044:
045: package org.jfree.layout;
046:
047: import java.awt.Component;
048: import java.awt.Container;
049: import java.awt.Dimension;
050: import java.awt.Insets;
051: import java.awt.LayoutManager;
052: import java.io.Serializable;
053:
054: /**
055: * A layout manager that spaces components over six columns in seven different
056: * formats.
057: *
058: * @author David Gilbert
059: */
060: public class FormatLayout implements LayoutManager, Serializable {
061:
062: /** For serialization. */
063: private static final long serialVersionUID = 2866692886323930722L;
064:
065: /** A useful constant representing layout format 1. */
066: public static final int C = 1;
067:
068: /** A useful constant representing layout format 2. */
069: public static final int LC = 2;
070:
071: /** A useful constant representing layout format 3. */
072: public static final int LCB = 3;
073:
074: /** A useful constant representing layout format 4. */
075: public static final int LCLC = 4;
076:
077: /** A useful constant representing layout format 5. */
078: public static final int LCLCB = 5;
079:
080: /** A useful constant representing layout format 6. */
081: public static final int LCBLC = 6;
082:
083: /** A useful constant representing layout format 7. */
084: public static final int LCBLCB = 7;
085:
086: /** The layout format for each row. */
087: private int[] rowFormats;
088:
089: /** The gap between the rows. */
090: private int rowGap;
091:
092: /**
093: * The gaps between the columns (gap[0] is the gap following column zero).
094: */
095: private int[] columnGaps;
096:
097: /** Working array for recording the height of each row. */
098: private int[] rowHeights;
099:
100: /** The total height of the layout. */
101: private int totalHeight;
102:
103: /** Working array for recording the width of each column. */
104: private int[] columnWidths;
105:
106: /** The total width of the layout. */
107: private int totalWidth;
108:
109: /** Combined width of columns 1 and 2. */
110: private int columns1and2Width;
111:
112: /** Combined width of columns 4 and 5. */
113: private int columns4and5Width;
114:
115: /** Combined width of columns 1 to 4. */
116: private int columns1to4Width;
117:
118: /** Combined width of columns 1 to 5. */
119: private int columns1to5Width;
120:
121: /** Combined width of columns 0 to 5. */
122: private int columns0to5Width;
123:
124: /**
125: * Constructs a new layout manager that can be used to create input forms.
126: * The layout manager works by arranging components in rows using six
127: * columns (some components will use more than one column).
128: * <P>
129: * Any component can be added, but I think of them in terms of Labels,
130: * Components, and Buttons.
131: * The formats available are: C, LC, LCB, LCLC, LCLCB, LCBLC or LCBLCB.
132: * <table>
133: * <tr>
134: * <td>C</td>
135: * <td>1 component in this row (spread across all six columns).</td>
136: * </tr>
137: * <tr>
138: * <td>LC</td>
139: * <td>2 components, a label in the 1st column, and a component using the
140: * remaining 5 columns).</td>
141: * </tr>
142: * <tr>
143: * <td>LCB</td>
144: * <td>3 components, a label in the 1st column, a component spread across
145: * the next 4, and a button in the last column.</td>
146: * </tr>
147: * <tr>
148: * <td>LCLC</td>
149: * <td>4 components, a label in column 1, a component in 2-3, a label in
150: * 4 and a component in 5-6.</td>
151: * </tr>
152: * <tr>
153: * <td>LCLCB</td>
154: * <td>5 components, a label in column 1, a component in 2-3, a label
155: * in 4, a component in 5 and a button in 6.</td>
156: * </tr>
157: * <tr>
158: * <td>LCBLC</td>
159: * <td>5 components, a label in column 1, a component in 2, a button in 3,
160: * a label in 4, a component in 5-6.</td>
161: * </tr>
162: * <tr>
163: * <td>LCBLCB</td>
164: * <td>6 components, one in each column.</td>
165: * </tr>
166: * </table>
167: * <P>
168: * Columns 1 and 4 expand to accommodate the widest label, and 3 and 6 to
169: * accommodate the widest button.
170: * <P>
171: * Each row will contain the number of components indicated by the format.
172: * Be sure to specify enough row formats to cover all the components you
173: * add to the layout.
174: *
175: * @param rowCount the number of rows.
176: * @param rowFormats the row formats.
177: */
178: public FormatLayout(final int rowCount, final int[] rowFormats) {
179:
180: this .rowFormats = rowFormats;
181: this .rowGap = 2;
182: this .columnGaps = new int[5];
183: this .columnGaps[0] = 10;
184: this .columnGaps[1] = 5;
185: this .columnGaps[2] = 5;
186: this .columnGaps[3] = 10;
187: this .columnGaps[4] = 5;
188:
189: // working structures...
190: this .rowHeights = new int[rowCount];
191: this .columnWidths = new int[6];
192: }
193:
194: /**
195: * Returns the preferred size of the component using this layout manager.
196: *
197: * @param parent the parent.
198: *
199: * @return the preferred size of the component.
200: */
201: public Dimension preferredLayoutSize(final Container parent) {
202:
203: Component c0, c1, c2, c3, c4, c5;
204:
205: synchronized (parent.getTreeLock()) {
206: final Insets insets = parent.getInsets();
207: int componentIndex = 0;
208: final int rowCount = this .rowHeights.length;
209: for (int i = 0; i < this .columnWidths.length; i++) {
210: this .columnWidths[i] = 0;
211: }
212: this .columns1and2Width = 0;
213: this .columns4and5Width = 0;
214: this .columns1to4Width = 0;
215: this .columns1to5Width = 0;
216: this .columns0to5Width = 0;
217:
218: this .totalHeight = 0;
219: for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
220: final int format = this .rowFormats[rowIndex
221: % this .rowFormats.length];
222: switch (format) {
223: case FormatLayout.C:
224: c0 = parent.getComponent(componentIndex);
225: updateC(rowIndex, c0.getPreferredSize());
226: componentIndex = componentIndex + 1;
227: break;
228: case FormatLayout.LC:
229: c0 = parent.getComponent(componentIndex);
230: c1 = parent.getComponent(componentIndex + 1);
231: updateLC(rowIndex, c0.getPreferredSize(), c1
232: .getPreferredSize());
233: componentIndex = componentIndex + 2;
234: break;
235: case FormatLayout.LCB:
236: c0 = parent.getComponent(componentIndex);
237: c1 = parent.getComponent(componentIndex + 1);
238: c2 = parent.getComponent(componentIndex + 2);
239: updateLCB(rowIndex, c0.getPreferredSize(), c1
240: .getPreferredSize(), c2.getPreferredSize());
241: componentIndex = componentIndex + 3;
242: break;
243: case FormatLayout.LCLC:
244: c0 = parent.getComponent(componentIndex);
245: c1 = parent.getComponent(componentIndex + 1);
246: c2 = parent.getComponent(componentIndex + 2);
247: c3 = parent.getComponent(componentIndex + 3);
248: updateLCLC(rowIndex, c0.getPreferredSize(), c1
249: .getPreferredSize(), c2.getPreferredSize(),
250: c3.getPreferredSize());
251: componentIndex = componentIndex + 4;
252: break;
253: case FormatLayout.LCBLC:
254: c0 = parent.getComponent(componentIndex);
255: c1 = parent.getComponent(componentIndex + 1);
256: c2 = parent.getComponent(componentIndex + 2);
257: c3 = parent.getComponent(componentIndex + 3);
258: c4 = parent.getComponent(componentIndex + 4);
259: updateLCBLC(rowIndex, c0.getPreferredSize(), c1
260: .getPreferredSize(), c2.getPreferredSize(),
261: c3.getPreferredSize(), c4
262: .getPreferredSize());
263: componentIndex = componentIndex + 5;
264: break;
265: case FormatLayout.LCLCB:
266: c0 = parent.getComponent(componentIndex);
267: c1 = parent.getComponent(componentIndex + 1);
268: c2 = parent.getComponent(componentIndex + 2);
269: c3 = parent.getComponent(componentIndex + 3);
270: c4 = parent.getComponent(componentIndex + 4);
271: updateLCLCB(rowIndex, c0.getPreferredSize(), c1
272: .getPreferredSize(), c2.getPreferredSize(),
273: c3.getPreferredSize(), c4
274: .getPreferredSize());
275: componentIndex = componentIndex + 5;
276: break;
277: case FormatLayout.LCBLCB:
278: c0 = parent.getComponent(componentIndex);
279: c1 = parent.getComponent(componentIndex + 1);
280: c2 = parent.getComponent(componentIndex + 2);
281: c3 = parent.getComponent(componentIndex + 3);
282: c4 = parent.getComponent(componentIndex + 4);
283: c5 = parent.getComponent(componentIndex + 5);
284: updateLCBLCB(rowIndex, c0.getPreferredSize(), c1
285: .getPreferredSize(), c2.getPreferredSize(),
286: c3.getPreferredSize(), c4
287: .getPreferredSize(), c5
288: .getPreferredSize());
289: componentIndex = componentIndex + 6;
290: break;
291: }
292: }
293: complete();
294: return new Dimension(this .totalWidth + insets.left
295: + insets.right, this .totalHeight + (rowCount - 1)
296: * this .rowGap + insets.top + insets.bottom);
297: }
298: }
299:
300: /**
301: * Returns the minimum size of the component using this layout manager.
302: *
303: * @param parent the parent.
304: *
305: * @return the minimum size of the component
306: */
307: public Dimension minimumLayoutSize(final Container parent) {
308:
309: Component c0, c1, c2, c3, c4, c5;
310:
311: synchronized (parent.getTreeLock()) {
312: final Insets insets = parent.getInsets();
313: int componentIndex = 0;
314: final int rowCount = this .rowHeights.length;
315: for (int i = 0; i < this .columnWidths.length; i++) {
316: this .columnWidths[i] = 0;
317: }
318: this .columns1and2Width = 0;
319: this .columns4and5Width = 0;
320: this .columns1to4Width = 0;
321: this .columns1to5Width = 0;
322: this .columns0to5Width = 0;
323: final int totalHeight = 0;
324: for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
325:
326: final int format = this .rowFormats[rowIndex
327: % this .rowFormats.length];
328:
329: switch (format) {
330: case FormatLayout.C:
331: c0 = parent.getComponent(componentIndex);
332: this .columns0to5Width = Math.max(
333: this .columns0to5Width,
334: c0.getMinimumSize().width);
335: componentIndex = componentIndex + 1;
336: break;
337: case FormatLayout.LC:
338: c0 = parent.getComponent(componentIndex);
339: c1 = parent.getComponent(componentIndex + 1);
340: updateLC(rowIndex, c0.getMinimumSize(), c1
341: .getMinimumSize());
342: componentIndex = componentIndex + 2;
343: break;
344: case FormatLayout.LCB:
345: c0 = parent.getComponent(componentIndex);
346: c1 = parent.getComponent(componentIndex + 1);
347: c2 = parent.getComponent(componentIndex + 2);
348: updateLCB(rowIndex, c0.getMinimumSize(), c1
349: .getMinimumSize(), c2.getMinimumSize());
350: componentIndex = componentIndex + 3;
351: break;
352: case FormatLayout.LCLC:
353: c0 = parent.getComponent(componentIndex);
354: c1 = parent.getComponent(componentIndex + 1);
355: c2 = parent.getComponent(componentIndex + 2);
356: c3 = parent.getComponent(componentIndex + 3);
357: updateLCLC(rowIndex, c0.getMinimumSize(), c1
358: .getMinimumSize(), c2.getMinimumSize(), c3
359: .getMinimumSize());
360: componentIndex = componentIndex + 3;
361: break;
362: case FormatLayout.LCBLC:
363: c0 = parent.getComponent(componentIndex);
364: c1 = parent.getComponent(componentIndex + 1);
365: c2 = parent.getComponent(componentIndex + 2);
366: c3 = parent.getComponent(componentIndex + 3);
367: c4 = parent.getComponent(componentIndex + 4);
368: updateLCBLC(rowIndex, c0.getMinimumSize(), c1
369: .getMinimumSize(), c2.getMinimumSize(), c3
370: .getMinimumSize(), c4.getMinimumSize());
371: componentIndex = componentIndex + 4;
372: break;
373: case FormatLayout.LCLCB:
374: c0 = parent.getComponent(componentIndex);
375: c1 = parent.getComponent(componentIndex + 1);
376: c2 = parent.getComponent(componentIndex + 2);
377: c3 = parent.getComponent(componentIndex + 3);
378: c4 = parent.getComponent(componentIndex + 4);
379: updateLCLCB(rowIndex, c0.getMinimumSize(), c1
380: .getMinimumSize(), c2.getMinimumSize(), c3
381: .getMinimumSize(), c4.getMinimumSize());
382: componentIndex = componentIndex + 4;
383: break;
384: case FormatLayout.LCBLCB:
385: c0 = parent.getComponent(componentIndex);
386: c1 = parent.getComponent(componentIndex + 1);
387: c2 = parent.getComponent(componentIndex + 2);
388: c3 = parent.getComponent(componentIndex + 3);
389: c4 = parent.getComponent(componentIndex + 4);
390: c5 = parent.getComponent(componentIndex + 5);
391: updateLCBLCB(rowIndex, c0.getMinimumSize(), c1
392: .getMinimumSize(), c2.getMinimumSize(), c3
393: .getMinimumSize(), c4.getMinimumSize(), c5
394: .getMinimumSize());
395: componentIndex = componentIndex + 5;
396: break;
397: }
398: }
399: complete();
400: return new Dimension(this .totalWidth + insets.left
401: + insets.right, totalHeight + (rowCount - 1)
402: * this .rowGap + insets.top + insets.bottom);
403: }
404: }
405:
406: /**
407: * Performs the layout of the container.
408: *
409: * @param parent the parent.
410: */
411: public void layoutContainer(final Container parent) {
412: Component c0, c1, c2, c3, c4, c5;
413:
414: synchronized (parent.getTreeLock()) {
415: final Insets insets = parent.getInsets();
416: int componentIndex = 0;
417: final int rowCount = this .rowHeights.length;
418: for (int i = 0; i < this .columnWidths.length; i++) {
419: this .columnWidths[i] = 0;
420: }
421: this .columns1and2Width = 0;
422: this .columns4and5Width = 0;
423: this .columns1to4Width = 0;
424: this .columns1to5Width = 0;
425: this .columns0to5Width = parent.getBounds().width
426: - insets.left - insets.right;
427:
428: this .totalHeight = 0;
429: for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
430: final int format = this .rowFormats[rowIndex
431: % this .rowFormats.length];
432: switch (format) {
433: case FormatLayout.C:
434: c0 = parent.getComponent(componentIndex);
435: updateC(rowIndex, c0.getPreferredSize());
436: componentIndex = componentIndex + 1;
437: break;
438: case FormatLayout.LC:
439: c0 = parent.getComponent(componentIndex);
440: c1 = parent.getComponent(componentIndex + 1);
441: updateLC(rowIndex, c0.getPreferredSize(), c1
442: .getPreferredSize());
443: componentIndex = componentIndex + 2;
444: break;
445: case FormatLayout.LCB:
446: c0 = parent.getComponent(componentIndex);
447: c1 = parent.getComponent(componentIndex + 1);
448: c2 = parent.getComponent(componentIndex + 2);
449: updateLCB(rowIndex, c0.getPreferredSize(), c1
450: .getPreferredSize(), c2.getPreferredSize());
451: componentIndex = componentIndex + 3;
452: break;
453: case FormatLayout.LCLC:
454: c0 = parent.getComponent(componentIndex);
455: c1 = parent.getComponent(componentIndex + 1);
456: c2 = parent.getComponent(componentIndex + 2);
457: c3 = parent.getComponent(componentIndex + 3);
458: updateLCLC(rowIndex, c0.getPreferredSize(), c1
459: .getPreferredSize(), c2.getPreferredSize(),
460: c3.getPreferredSize());
461: componentIndex = componentIndex + 4;
462: break;
463: case FormatLayout.LCBLC:
464: c0 = parent.getComponent(componentIndex);
465: c1 = parent.getComponent(componentIndex + 1);
466: c2 = parent.getComponent(componentIndex + 2);
467: c3 = parent.getComponent(componentIndex + 3);
468: c4 = parent.getComponent(componentIndex + 4);
469: updateLCBLC(rowIndex, c0.getPreferredSize(), c1
470: .getPreferredSize(), c2.getPreferredSize(),
471: c3.getPreferredSize(), c4
472: .getPreferredSize());
473: componentIndex = componentIndex + 5;
474: break;
475: case FormatLayout.LCLCB:
476: c0 = parent.getComponent(componentIndex);
477: c1 = parent.getComponent(componentIndex + 1);
478: c2 = parent.getComponent(componentIndex + 2);
479: c3 = parent.getComponent(componentIndex + 3);
480: c4 = parent.getComponent(componentIndex + 4);
481: updateLCLCB(rowIndex, c0.getPreferredSize(), c1
482: .getPreferredSize(), c2.getPreferredSize(),
483: c3.getPreferredSize(), c4
484: .getPreferredSize());
485: componentIndex = componentIndex + 5;
486: break;
487: case FormatLayout.LCBLCB:
488: c0 = parent.getComponent(componentIndex);
489: c1 = parent.getComponent(componentIndex + 1);
490: c2 = parent.getComponent(componentIndex + 2);
491: c3 = parent.getComponent(componentIndex + 3);
492: c4 = parent.getComponent(componentIndex + 4);
493: c5 = parent.getComponent(componentIndex + 5);
494: updateLCBLCB(rowIndex, c0.getPreferredSize(), c1
495: .getPreferredSize(), c2.getPreferredSize(),
496: c3.getPreferredSize(), c4
497: .getPreferredSize(), c5
498: .getPreferredSize());
499: componentIndex = componentIndex + 6;
500: break;
501: }
502: }
503: complete();
504:
505: componentIndex = 0;
506: int rowY = insets.top;
507: final int[] rowX = new int[6];
508: rowX[0] = insets.left;
509: rowX[1] = rowX[0] + this .columnWidths[0]
510: + this .columnGaps[0];
511: rowX[2] = rowX[1] + this .columnWidths[1]
512: + this .columnGaps[1];
513: rowX[3] = rowX[2] + this .columnWidths[2]
514: + this .columnGaps[2];
515: rowX[4] = rowX[3] + this .columnWidths[3]
516: + this .columnGaps[3];
517: rowX[5] = rowX[4] + this .columnWidths[4]
518: + this .columnGaps[4];
519: final int w1to2 = this .columnWidths[1] + this .columnGaps[1]
520: + this .columnWidths[2];
521: final int w4to5 = this .columnWidths[4] + this .columnGaps[4]
522: + this .columnWidths[5];
523: final int w1to4 = w1to2 + this .columnGaps[2]
524: + this .columnWidths[3] + this .columnGaps[3]
525: + this .columnWidths[4];
526: final int w1to5 = w1to4 + this .columnGaps[4]
527: + this .columnWidths[5];
528: final int w0to5 = w1to5 + this .columnWidths[0]
529: + this .columnGaps[0];
530: for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
531: final int format = this .rowFormats[rowIndex
532: % this .rowFormats.length];
533:
534: switch (format) {
535: case FormatLayout.C:
536: c0 = parent.getComponent(componentIndex);
537: c0.setBounds(rowX[0], rowY, w0to5, c0
538: .getPreferredSize().height);
539: componentIndex = componentIndex + 1;
540: break;
541: case FormatLayout.LC:
542: c0 = parent.getComponent(componentIndex);
543: c0.setBounds(rowX[0], rowY
544: + (this .rowHeights[rowIndex] - c0
545: .getPreferredSize().height) / 2,
546: this .columnWidths[0],
547: c0.getPreferredSize().height);
548: c1 = parent.getComponent(componentIndex + 1);
549: c1.setBounds(rowX[1], rowY
550: + (this .rowHeights[rowIndex] - c1
551: .getPreferredSize().height) / 2,
552: w1to5, c1.getPreferredSize().height);
553: componentIndex = componentIndex + 2;
554: break;
555: case FormatLayout.LCB:
556: c0 = parent.getComponent(componentIndex);
557: c0.setBounds(rowX[0], rowY
558: + (this .rowHeights[rowIndex] - c0
559: .getPreferredSize().height) / 2,
560: this .columnWidths[0],
561: c0.getPreferredSize().height);
562: c1 = parent.getComponent(componentIndex + 1);
563: c1.setBounds(rowX[1], rowY
564: + (this .rowHeights[rowIndex] - c1
565: .getPreferredSize().height) / 2,
566: w1to4, c1.getPreferredSize().height);
567: c2 = parent.getComponent(componentIndex + 2);
568: c2.setBounds(rowX[5], rowY
569: + (this .rowHeights[rowIndex] - c2
570: .getPreferredSize().height) / 2,
571: this .columnWidths[5],
572: c2.getPreferredSize().height);
573: componentIndex = componentIndex + 3;
574: break;
575: case FormatLayout.LCLC:
576: c0 = parent.getComponent(componentIndex);
577: c0.setBounds(rowX[0], rowY
578: + (this .rowHeights[rowIndex] - c0
579: .getPreferredSize().height) / 2,
580: this .columnWidths[0],
581: c0.getPreferredSize().height);
582: c1 = parent.getComponent(componentIndex + 1);
583: c1.setBounds(rowX[1], rowY
584: + (this .rowHeights[rowIndex] - c1
585: .getPreferredSize().height) / 2,
586: w1to2, c1.getPreferredSize().height);
587: c2 = parent.getComponent(componentIndex + 2);
588: c2.setBounds(rowX[3], rowY
589: + (this .rowHeights[rowIndex] - c2
590: .getPreferredSize().height) / 2,
591: this .columnWidths[3],
592: c2.getPreferredSize().height);
593: c3 = parent.getComponent(componentIndex + 3);
594: c3.setBounds(rowX[4], rowY
595: + (this .rowHeights[rowIndex] - c3
596: .getPreferredSize().height) / 2,
597: w4to5, c3.getPreferredSize().height);
598: componentIndex = componentIndex + 4;
599: break;
600: case FormatLayout.LCBLC:
601: c0 = parent.getComponent(componentIndex);
602: c0.setBounds(rowX[0], rowY
603: + (this .rowHeights[rowIndex] - c0
604: .getPreferredSize().height) / 2,
605: this .columnWidths[0],
606: c0.getPreferredSize().height);
607: c1 = parent.getComponent(componentIndex + 1);
608: c1.setBounds(rowX[1], rowY
609: + (this .rowHeights[rowIndex] - c1
610: .getPreferredSize().height) / 2,
611: this .columnWidths[1],
612: c1.getPreferredSize().height);
613: c2 = parent.getComponent(componentIndex + 2);
614: c2.setBounds(rowX[2], rowY
615: + (this .rowHeights[rowIndex] - c2
616: .getPreferredSize().height) / 2,
617: this .columnWidths[2],
618: c2.getPreferredSize().height);
619: c3 = parent.getComponent(componentIndex + 3);
620: c3.setBounds(rowX[3], rowY
621: + (this .rowHeights[rowIndex] - c3
622: .getPreferredSize().height) / 2,
623: this .columnWidths[3],
624: c3.getPreferredSize().height);
625: c4 = parent.getComponent(componentIndex + 4);
626: c4.setBounds(rowX[4], rowY
627: + (this .rowHeights[rowIndex] - c4
628: .getPreferredSize().height) / 2,
629: w4to5, c4.getPreferredSize().height);
630: componentIndex = componentIndex + 5;
631: break;
632: case FormatLayout.LCLCB:
633: c0 = parent.getComponent(componentIndex);
634: c0.setBounds(rowX[0], rowY
635: + (this .rowHeights[rowIndex] - c0
636: .getPreferredSize().height) / 2,
637: this .columnWidths[0],
638: c0.getPreferredSize().height);
639: c1 = parent.getComponent(componentIndex + 1);
640: c1.setBounds(rowX[1], rowY
641: + (this .rowHeights[rowIndex] - c1
642: .getPreferredSize().height) / 2,
643: w1to2, c1.getPreferredSize().height);
644: c2 = parent.getComponent(componentIndex + 2);
645: c2.setBounds(rowX[3], rowY
646: + (this .rowHeights[rowIndex] - c2
647: .getPreferredSize().height) / 2,
648: this .columnWidths[3],
649: c2.getPreferredSize().height);
650: c3 = parent.getComponent(componentIndex + 3);
651: c3.setBounds(rowX[4], rowY
652: + (this .rowHeights[rowIndex] - c3
653: .getPreferredSize().height) / 2,
654: this .columnWidths[4],
655: c3.getPreferredSize().height);
656: c4 = parent.getComponent(componentIndex + 4);
657: c4.setBounds(rowX[5], rowY
658: + (this .rowHeights[rowIndex] - c4
659: .getPreferredSize().height) / 2,
660: this .columnWidths[5],
661: c4.getPreferredSize().height);
662: componentIndex = componentIndex + 5;
663: break;
664: case FormatLayout.LCBLCB:
665: c0 = parent.getComponent(componentIndex);
666: c0.setBounds(rowX[0], rowY
667: + (this .rowHeights[rowIndex] - c0
668: .getPreferredSize().height) / 2,
669: this .columnWidths[0],
670: c0.getPreferredSize().height);
671: c1 = parent.getComponent(componentIndex + 1);
672: c1.setBounds(rowX[1], rowY
673: + (this .rowHeights[rowIndex] - c1
674: .getPreferredSize().height) / 2,
675: this .columnWidths[1],
676: c1.getPreferredSize().height);
677: c2 = parent.getComponent(componentIndex + 2);
678: c2.setBounds(rowX[2], rowY
679: + (this .rowHeights[rowIndex] - c2
680: .getPreferredSize().height) / 2,
681: this .columnWidths[2],
682: c2.getPreferredSize().height);
683: c3 = parent.getComponent(componentIndex + 3);
684: c3.setBounds(rowX[3], rowY
685: + (this .rowHeights[rowIndex] - c3
686: .getPreferredSize().height) / 2,
687: this .columnWidths[3],
688: c3.getPreferredSize().height);
689: c4 = parent.getComponent(componentIndex + 4);
690: c4.setBounds(rowX[4], rowY
691: + (this .rowHeights[rowIndex] - c4
692: .getPreferredSize().height) / 2,
693: this .columnWidths[4],
694: c4.getPreferredSize().height);
695: c5 = parent.getComponent(componentIndex + 5);
696: c5.setBounds(rowX[5], rowY
697: + (this .rowHeights[rowIndex] - c5
698: .getPreferredSize().height) / 2,
699: this .columnWidths[5],
700: c5.getPreferredSize().height);
701: componentIndex = componentIndex + 6;
702: break;
703: }
704: rowY = rowY + this .rowHeights[rowIndex] + this .rowGap;
705: }
706: }
707: }
708:
709: /**
710: * Processes a row in 'C' format.
711: *
712: * @param rowIndex the row index.
713: * @param d0 dimension 0.
714: */
715: protected void updateC(final int rowIndex, final Dimension d0) {
716: this .rowHeights[rowIndex] = d0.height;
717: this .totalHeight = this .totalHeight + this .rowHeights[rowIndex];
718: this .columns0to5Width = Math.max(this .columns0to5Width,
719: d0.width);
720: }
721:
722: /**
723: * Processes a row in 'LC' format.
724: *
725: * @param rowIndex the row index.
726: * @param d0 dimension 0.
727: * @param d1 dimension 1.
728: */
729: protected void updateLC(final int rowIndex, final Dimension d0,
730: final Dimension d1) {
731:
732: this .rowHeights[rowIndex] = Math.max(d0.height, d1.height);
733: this .totalHeight = this .totalHeight + this .rowHeights[rowIndex];
734: this .columnWidths[0] = Math.max(this .columnWidths[0], d0.width);
735: this .columns1to5Width = Math.max(this .columns1to5Width,
736: d1.width);
737:
738: }
739:
740: /**
741: * Processes a row in 'LCB' format.
742: *
743: * @param rowIndex the row index.
744: * @param d0 dimension 0.
745: * @param d1 dimension 1.
746: * @param d2 dimension 2.
747: */
748: protected void updateLCB(final int rowIndex, final Dimension d0,
749: final Dimension d1, final Dimension d2) {
750:
751: this .rowHeights[rowIndex] = Math.max(d0.height, Math.max(
752: d1.height, d2.height));
753: this .totalHeight = this .totalHeight + this .rowHeights[rowIndex];
754: this .columnWidths[0] = Math.max(this .columnWidths[0], d0.width);
755: this .columns1to4Width = Math.max(this .columns1to4Width,
756: d1.width);
757: this .columnWidths[5] = Math.max(this .columnWidths[5], d2.width);
758:
759: }
760:
761: /**
762: * Processes a row in 'LCLC' format.
763: *
764: * @param rowIndex the row index.
765: * @param d0 dimension 0.
766: * @param d1 dimension 1.
767: * @param d2 dimension 2.
768: * @param d3 dimension 3.
769: */
770: protected void updateLCLC(final int rowIndex, final Dimension d0,
771: final Dimension d1, final Dimension d2, final Dimension d3) {
772:
773: this .rowHeights[rowIndex] = Math.max(Math.max(d0.height,
774: d1.height), Math.max(d2.height, d3.height));
775: this .totalHeight = this .totalHeight + this .rowHeights[rowIndex];
776: this .columnWidths[0] = Math.max(this .columnWidths[0], d0.width);
777: this .columns1and2Width = Math.max(this .columns1and2Width,
778: d1.width);
779: this .columnWidths[3] = Math.max(this .columnWidths[3], d2.width);
780: this .columns4and5Width = Math.max(this .columns4and5Width,
781: d3.width);
782: }
783:
784: /**
785: * Processes a row in 'LCBLC' format.
786: *
787: * @param rowIndex the row index.
788: * @param d0 dimension 0.
789: * @param d1 dimension 1.
790: * @param d2 dimension 2.
791: * @param d3 dimension 3.
792: * @param d4 dimension 4.
793: */
794: protected void updateLCBLC(final int rowIndex, final Dimension d0,
795: final Dimension d1, final Dimension d2, final Dimension d3,
796: final Dimension d4) {
797:
798: this .rowHeights[rowIndex] = (Math.max(d0.height, Math.max(Math
799: .max(d1.height, d2.height), Math.max(d3.height,
800: d4.height))));
801: this .totalHeight = this .totalHeight + this .rowHeights[rowIndex];
802: this .columnWidths[0] = Math.max(this .columnWidths[0], d0.width);
803: this .columnWidths[1] = Math.max(this .columnWidths[1], d1.width);
804: this .columnWidths[2] = Math.max(this .columnWidths[2], d2.width);
805: this .columnWidths[3] = Math.max(this .columnWidths[3], d3.width);
806: this .columns4and5Width = Math.max(this .columns4and5Width,
807: d4.width);
808:
809: }
810:
811: /**
812: * Processes a row in 'LCLCB' format.
813: *
814: * @param rowIndex the row index.
815: * @param d0 dimension 0.
816: * @param d1 dimension 1.
817: * @param d2 dimension 2.
818: * @param d3 dimension 3.
819: * @param d4 dimension 4.
820: */
821: protected void updateLCLCB(final int rowIndex, final Dimension d0,
822: final Dimension d1, final Dimension d2, final Dimension d3,
823: final Dimension d4) {
824:
825: this .rowHeights[rowIndex] = (Math.max(d0.height, Math.max(Math
826: .max(d1.height, d2.height), Math.max(d3.height,
827: d4.height))));
828: this .totalHeight = this .totalHeight + this .rowHeights[rowIndex];
829: this .columnWidths[0] = Math.max(this .columnWidths[0], d0.width);
830: this .columns1and2Width = Math.max(this .columns1and2Width,
831: d1.width);
832: this .columnWidths[3] = Math.max(this .columnWidths[3], d2.width);
833: this .columnWidths[4] = Math.max(this .columnWidths[4], d3.width);
834: this .columnWidths[5] = Math.max(this .columnWidths[5], d4.width);
835:
836: }
837:
838: /**
839: * Processes a row in 'LCBLCB' format.
840: *
841: * @param rowIndex the row index.
842: * @param d0 dimension 0.
843: * @param d1 dimension 1.
844: * @param d2 dimension 2.
845: * @param d3 dimension 3.
846: * @param d4 dimension 4.
847: * @param d5 dimension 5.
848: */
849: protected void updateLCBLCB(final int rowIndex, final Dimension d0,
850: final Dimension d1, final Dimension d2, final Dimension d3,
851: final Dimension d4, final Dimension d5) {
852:
853: this .rowHeights[rowIndex] = Math.max(Math.max(d0.height,
854: d1.height), Math.max(Math.max(d2.height, d3.height),
855: Math.max(d4.height, d5.height)));
856: this .totalHeight = this .totalHeight + this .rowHeights[rowIndex];
857: this .columnWidths[0] = Math.max(this .columnWidths[0], d0.width);
858: this .columnWidths[1] = Math.max(this .columnWidths[1], d1.width);
859: this .columnWidths[2] = Math.max(this .columnWidths[2], d2.width);
860: this .columnWidths[3] = Math.max(this .columnWidths[3], d3.width);
861: this .columnWidths[4] = Math.max(this .columnWidths[4], d4.width);
862: this .columnWidths[5] = Math.max(this .columnWidths[5], d5.width);
863:
864: }
865:
866: /**
867: * Finishes of the processing.
868: */
869: public void complete() {
870:
871: this .columnWidths[1] = Math.max(this .columnWidths[1],
872: this .columns1and2Width - this .columnGaps[1]
873: - this .columnWidths[2]);
874:
875: this .columnWidths[4] = Math.max(this .columnWidths[4], Math.max(
876: this .columns4and5Width - this .columnGaps[4]
877: - this .columnWidths[5], Math.max(
878: this .columns1to4Width - this .columnGaps[1]
879: - this .columnGaps[2]
880: - this .columnGaps[3]
881: - this .columnWidths[1]
882: - this .columnWidths[2]
883: - this .columnWidths[3],
884: this .columns1to5Width - this .columnGaps[1]
885: - this .columnGaps[2]
886: - this .columnGaps[3]
887: - this .columnWidths[1]
888: - this .columnWidths[2]
889: - this .columnWidths[3]
890: - this .columnGaps[4])));
891:
892: int leftWidth = this .columnWidths[0] + this .columnGaps[0]
893: + this .columnWidths[1] + this .columnGaps[1]
894: + this .columnWidths[2];
895:
896: int rightWidth = this .columnWidths[3] + this .columnGaps[3]
897: + this .columnWidths[4] + this .columnGaps[4]
898: + this .columnWidths[5];
899:
900: if (splitLayout()) {
901: if (leftWidth > rightWidth) {
902: final int mismatch = leftWidth - rightWidth;
903: this .columnWidths[4] = this .columnWidths[4] + mismatch;
904: rightWidth = rightWidth + mismatch;
905: } else {
906: final int mismatch = rightWidth - leftWidth;
907: this .columnWidths[1] = this .columnWidths[1] + mismatch;
908: leftWidth = leftWidth + mismatch;
909: }
910: }
911:
912: this .totalWidth = leftWidth + this .columnGaps[2] + rightWidth;
913:
914: if (this .columns0to5Width > this .totalWidth) {
915: final int spaceToAdd = (this .columns0to5Width - this .totalWidth);
916: if (splitLayout()) {
917: final int halfSpaceToAdd = spaceToAdd / 2;
918: this .columnWidths[1] = this .columnWidths[1]
919: + halfSpaceToAdd;
920: this .columnWidths[4] = this .columnWidths[4]
921: + spaceToAdd - halfSpaceToAdd;
922: this .totalWidth = this .totalWidth + spaceToAdd;
923: } else {
924: this .columnWidths[1] = this .columnWidths[1]
925: + spaceToAdd;
926: this .totalWidth = this .totalWidth + spaceToAdd;
927: }
928: }
929:
930: }
931:
932: /**
933: * Returns true if this layout involves a split into two sections.
934: *
935: * @return <code>true</code> if this layout involves a split into two
936: * sections.
937: */
938: private boolean splitLayout() {
939: for (int i = 0; i < this .rowFormats.length; i++) {
940: if (this .rowFormats[i] > FormatLayout.LCB) {
941: return true;
942: }
943: }
944: return false;
945: }
946:
947: /**
948: * Not used.
949: *
950: * @param comp the component.
951: */
952: public void addLayoutComponent(final Component comp) {
953: // not used
954: }
955:
956: /**
957: * Not used.
958: *
959: * @param comp the component.
960: */
961: public void removeLayoutComponent(final Component comp) {
962: // not used
963: }
964:
965: /**
966: * Not used.
967: *
968: * @param name the component name.
969: * @param comp the component.
970: */
971: public void addLayoutComponent(final String name,
972: final Component comp) {
973: // not used
974: }
975:
976: /**
977: * Not used.
978: *
979: * @param name the component name.
980: * @param comp the component.
981: */
982: public void removeLayoutComponent(final String name,
983: final Component comp) {
984: // not used
985: }
986:
987: }
|