KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > gui > TwoColumnLayout


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2002 Elmar Grom
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.gui;
23
24 import java.awt.*;
25 import java.util.Vector JavaDoc;
26
27 /**
28  * This class implements a layout manager that generally lays out components in two columns. <BR>
29  * <BR>
30  * The design goal for this layout manager was to lay out forms for data entry, where there are
31  * several rows of entry fields with associated labels. The goal was to have the beginning off all
32  * labels line up, as well as the left edge of all the data entry fields. This leads to a situation
33  * where all components are essentially laid out in two columns. The columns adjust to accommodate
34  * components of various sizes. This means that components that are added are laid out top to
35  * bottom, either in the left column, in the right column or straddling both columns. In addition to
36  * this general behavior, the following additional layout capabilities are supported:<br>
37  * <ul>
38  * <li>Resizable margins are provided on the left and right side.
39  * <li>A special region is provided at the top that is only affected by the side margins but not by
40  * any other layout behavior.
41  * <li>It is possible to specify the vertical positioning of the cluster of laid out components for
42  * the case that they do not occupy the entire available real estate.
43  * <li>Individual components can be indented.
44  * </ul>
45  *
46  * <b>The Layout Behavior</b> <br>
47  * <br>
48  * The left and right margin are absolute. This means that they can not be penetrated by components.
49  * All layout happens between the limits established by these margins. The purpose of these margins
50  * is to ensure that components are not laid out all the way to the edge of their container, without
51  * the need to set matching borders for each component. <br>
52  * <br>
53  * The title margin at the top factors only into the layout behavior if there is a component set to
54  * be laid out in that region, otherwise it is ignored. <br>
55  * <br>
56  * The vertical space available to each row of components depends on the space requirements of the
57  * tallest component in that row. Both components are placed vertically centered in their row. <br>
58  * <br>
59  * All horizontal layout is based on the position of three vertical rules, the left rule, the right
60  * rule and the center rule. <br>
61  * <br>
62  * <img SRC="doc-files/TwoColumnLayout.gif"/> <br>
63  * <br>
64  * The actual position of each rule depends on the alignment strategy, margin settings and component
65  * sizes. Regardless of these factors, components placed in the left column are <i>always</i>
66  * positioned with their left edge aligned with the left rule. Components placed in the right column
67  * are <i>always</i> positioned with their left edge aligned with the center rule. If a component
68  * straddles both columns, it is <i>always</i> positioned with the left edge aligned with the left
69  * rule, but is allowed to extend all the way to the right rule. The only exception is a component
70  * that is specified with an indent. In this case the component is moved to the right of the
71  * respective rule by the indent amount. <br>
72  * <br>
73  * The location of the rules is determined based on the alignment strategy as follows:<br>
74  * <ul>
75  * <li>The right rule is always located at the edge of the right margin.
76  * <li><b>Left Alignment:</b> The left rule is located the edge of the left margin. The center
77  * rule is located far enough to the right to clear the widest component in the left column.
78  * <li><b>Center Alignment:</b> The center rule is located at the center of the panel. The left
79  * rule is located far enough to the left to make the widest component in the left column fit.
80  * <li><b>Right Alignment</b> The center rule is located far enough to the left of the right rule
81  * to make the widest component in the right column fit. The left rule is located far enough to the
82  * left to make the widest component in the left column fit.
83  * </ul>
84  * All components clump together vertically and are positioned right beneath the title margin. This
85  * is of course not a very appealing presentation. By setting how the remaining vertical space is
86  * distributed above and below the cluster of components the cluster can be positioned more
87  * favorably (see the shaded area in the illustration).
88  *
89  * @see com.izforge.izpack.gui.TwoColumnConstraints
90  *
91  * @version 0.0.1 / 11/14/02
92  * @author Elmar Grom
93  */

94 public class TwoColumnLayout implements LayoutManager2
95 {
96
97     public static final int LEFT = 0;
98
99     public static final int RIGHT = 1;
100
101     public static final int CENTER = 2;
102
103     /** holds all the components and layout constraints. */
104     private Vector JavaDoc[] components = { new Vector JavaDoc(), new Vector JavaDoc()};
105
106     /**
107      * holds the component to be placed in the title region, including layout constraints.
108      */

109     private TwoColumnConstraints title = null;
110
111     /** the margin setting in % of the container's width */
112     private int margin = 0;
113
114     /**
115      * the setting for the buffer area on top of hte comonent cluster in % of the left over height.
116      */

117     private int topBuffer = 0;
118
119     /** the indent setting in % of the conteiner's width */
120     private int indent = 0;
121
122     /** the gap between the two columns */
123     private int gap = 5;
124
125     private int alignment = LEFT;
126
127     private int leftRule;
128
129     private int rightRule;
130
131     private int centerRule;
132
133     private int titleHeight;
134
135     /**
136      * Constructs a <code>TwoColumnLayout</code> layout manager. To add components use the
137      * container's <code>add(comp, constraints)</code> method with a TwoColumnConstraints object.
138      *
139      * @param margin the margin width to use on the left and right side in % of the total container
140      * width. Values less than 0% and greater than 50% are not accepted.
141      * @param gap the gap between the two columns.
142      * @param indent the indent to use for components that have that constraint set. This is a value
143      * in pixels.
144      * @param topBuffer the percentage of left over vertical space to place on top of the component
145      * cluster. Values between 0% and 100% are accepted.
146      * @param alignment how to align the overall layout. Legal values are LEFT, CENTER, RIGHT.
147      */

148     public TwoColumnLayout(int margin, int gap, int indent, int topBuffer, int alignment)
149     {
150         this.indent = indent;
151         this.gap = gap;
152
153         if ((margin >= 0) && (margin <= 50))
154         {
155             this.margin = margin;
156         }
157         if ((topBuffer >= 0) && (topBuffer <= 100))
158         {
159             this.topBuffer = topBuffer;
160         }
161         if ((alignment == LEFT) || (alignment == CENTER) || (alignment == RIGHT))
162         {
163             this.alignment = alignment;
164         }
165     }
166
167     /**
168      * Sets the constraints for the specified component in this layout. <code>null</code> is a
169      * legal value for a component, but not for a constraints object.
170      *
171      * @param comp the component to be modified.
172      * @param constraints the constraints to be applied.
173      */

174     public void addLayoutComponent(Component comp, Object JavaDoc constraints)
175     {
176         if (constraints == null) { return; }
177
178         TwoColumnConstraints component = null;
179         try
180         {
181             component = (TwoColumnConstraints) constraints;
182             component = (TwoColumnConstraints) component.clone();
183         }
184         catch (Throwable JavaDoc exception)
185         {
186             return;
187         }
188
189         component.component = comp;
190
191         // ----------------------------------------------------
192
// the title component is recorded in a separate
193
// variable, displacing any component that might have
194
// been previously recorded for that location.
195
// ----------------------------------------------------
196
if (component.position == TwoColumnConstraints.NORTH)
197         {
198             title = component;
199             if (title.stretch)
200             {
201                 title.align = LEFT;
202             }
203         }
204
205         // ----------------------------------------------------
206
// components that straddle both columns are a bit
207
// tricky because these components are recorded in the
208
// left column and the same row cannot contain a
209
// component in the right column.
210
//
211
// If there are fewer components in the left column
212
// than in the right one, a null is inserted at this
213
// place in the right column. This allows the component
214
// to use both columns. The component that previously
215
// occupied this position and any that were placed
216
// below will be pushed down by one row due to this
217
// action.
218
//
219
// If there are the same number of components in both
220
// columns or if there are fewer in the right column
221
// then the component is added to the left column and
222
// then the right column filled with null until both
223
// contain the same number of components. this means
224
// that any components that will now be placed in the
225
// right column are positioned beneath this component.
226
// Unoccupied spots higher in the right column become
227
// inaccessible.
228
// ----------------------------------------------------
229
else if (component.position == TwoColumnConstraints.BOTH)
230         {
231             // first make sure that both columns have the same number of entries
232
while (components[RIGHT].size() > components[LEFT].size())
233             {
234                 components[LEFT].add(null);
235             }
236
237             while (components[LEFT].size() > components[RIGHT].size())
238             {
239                 components[RIGHT].add(null);
240             }
241
242             components[LEFT].add(component);
243             components[RIGHT].add(null);
244         }
245
246         // ----------------------------------------------------
247
// WEST components are added to the left column
248
// ----------------------------------------------------
249
else if (component.position == TwoColumnConstraints.WEST)
250         {
251             components[LEFT].add(component);
252         }
253
254         // ----------------------------------------------------
255
// WESTONLY components are added to the left column
256
// the right column has to be kept free
257
// ----------------------------------------------------
258
else if (component.position == TwoColumnConstraints.WESTONLY)
259         {
260             components[LEFT].add(component);
261
262             // fill right column to make sure nothing is placed there
263
while (components[RIGHT].size() < components[LEFT].size())
264             {
265                 components[RIGHT].add(null);
266             }
267
268         }
269
270         // ----------------------------------------------------
271
// EAST components are added to the right column
272
// ----------------------------------------------------
273
else if (component.position == TwoColumnConstraints.EAST)
274         {
275             components[RIGHT].add(component);
276         }
277
278         // ----------------------------------------------------
279
// EASTONLY components are added to the left column
280
// the right column has to be kept free
281
// ----------------------------------------------------
282
else if (component.position == TwoColumnConstraints.EASTONLY)
283         {
284             components[RIGHT].add(component);
285
286             // fill left column to make sure nothing is placed there
287
while (components[LEFT].size() < components[RIGHT].size())
288             {
289                 components[LEFT].add(null);
290             }
291
292         }
293
294         // ----------------------------------------------------
295
// If the position did not match any of the above
296
// criteria then the component is not added and
297
// consequently will not be laid out.
298
// ----------------------------------------------------
299
}
300
301     /**
302      * Lays out the container in the specified panel.
303      *
304      * @param parent the component which needs to be laid out.
305      */

306     public void layoutContainer(Container parent)
307     {
308         positionRules(parent);
309         positionTitle(parent);
310         positionComponents(parent);
311     }
312
313     /**
314      * Positions the three rules in preparation for layout. Sets the variables:<br>
315      * <ul>
316      * <li><code>leftRule</code>
317      * <li><code>rightRule</code>
318      * <li><code>centerRule</code>
319      * </ul>
320      *
321      * @param parent the component which needs to be laid out.
322      */

323     private void positionRules(Container parent)
324     {
325         int margin = margin(parent);
326
327         if (alignment == LEFT)
328         {
329             leftRule = margin;
330             centerRule = leftRule + minimumColumnWidth(LEFT, parent) + gap;
331             rightRule = parent.getWidth() - margin;
332         }
333
334         else if (alignment == CENTER)
335         {
336             centerRule = (int) (parent.getMinimumSize().getWidth() / 2);
337             leftRule = centerRule - minimumColumnWidth(LEFT, parent) - gap;
338             rightRule = parent.getWidth() - margin;
339         }
340
341         else if (alignment == RIGHT)
342         {
343             rightRule = parent.getWidth() - margin;
344             centerRule = rightRule - minimumColumnWidth(RIGHT, parent);
345             leftRule = centerRule - minimumColumnWidth(LEFT, parent) - gap;
346         }
347     }
348
349     /**
350      * Positions the title component and sets the variable <code>titleHeight</code>. <b>Note:</b>
351      * this method depends on the fact that the rules are set to their correct layout position.
352      *
353      * @param parent the component which needs to be laid out.
354      */

355     private void positionTitle(Container parent)
356     {
357         if (title != null)
358         {
359             Component component = title.component;
360             int width = (int) component.getMinimumSize().getWidth();
361             titleHeight = (int) component.getMinimumSize().getHeight();
362
363             if (component != null)
364             {
365                 if (title.stretch)
366                 {
367                     width = rightRule - leftRule;
368                     component.setBounds(leftRule, 0, width, titleHeight);
369                 }
370
371                 else if (title.align == TwoColumnConstraints.LEFT)
372                 {
373                     component.setBounds(leftRule, 0, width, titleHeight);
374                 }
375
376                 else if (title.align == TwoColumnConstraints.CENTER)
377                 {
378                     int left = centerRule - (width / 2);
379                     component.setBounds(left, 0, width, titleHeight);
380                 }
381
382                 else if (title.align == TwoColumnConstraints.RIGHT)
383                 {
384                     int left = rightRule - width;
385                     component.setBounds(left, 0, width, titleHeight);
386                 }
387             }
388         }
389     }
390
391     /**
392      * Positions all components in the container.
393      *
394      * @param parent the component which needs to be laid out.
395      */

396     private void positionComponents(Container parent)
397     {
398         int usedHeight = titleHeight + minimumClusterHeight();
399         int topBuffer = topBuffer(usedHeight, parent);
400         int leftHeight = 0;
401         int rightHeight = 0;
402
403         if (topBuffer < 0)
404         {
405             topBuffer = 0;
406         }
407
408         int y = titleHeight + topBuffer;
409
410         for (int i = 0; i < rows(); i++)
411         {
412             leftHeight = height(i, LEFT);
413             rightHeight = height(i, RIGHT);
414
415             if (leftHeight > rightHeight)
416             {
417                 int offset = (leftHeight - rightHeight) / 2;
418
419                 positionComponent(y, i, LEFT, parent);
420                 positionComponent((y + offset), i, RIGHT, parent);
421
422                 y += leftHeight;
423             }
424             else if (leftHeight < rightHeight)
425             {
426                 int offset = (rightHeight - leftHeight) / 2;
427
428                 positionComponent((y + offset), i, LEFT, parent);
429                 positionComponent(y, i, RIGHT, parent);
430
431                 y += rightHeight;
432             }
433             else
434             {
435                 positionComponent(y, i, LEFT, parent);
436                 positionComponent(y, i, RIGHT, parent);
437
438                 y += leftHeight;
439             }
440         }
441     }
442
443     /**
444      * Positiones one component as instructed. Constraints for each component, such as
445      * <code>stretch</code>, <code>BOTH</code> and <code>indent</code> are taken into
446      * account. In addition, empty comonents are handled properly.
447      *
448      * @param y the y location within the continer, where the component should be positioned.
449      * @param row the row of the component
450      * @param column the column of the component
451      * @param parent the container which needs to be laid out.
452      */

453     private void positionComponent(int y, int row, int column, Container parent)
454     {
455         TwoColumnConstraints constraints = null;
456
457         try
458         {
459             constraints = (TwoColumnConstraints) (components[column].elementAt(row));
460         }
461         catch (Throwable JavaDoc exception)
462         {
463             return;
464         }
465
466         int x = 0;
467
468         if (constraints != null)
469         {
470             Component component = constraints.component;
471             int width = (int) component.getPreferredSize().getWidth();
472             int height = (int) component.getPreferredSize().getHeight();
473
474             // --------------------------------------------------
475
// set x to the appropriate rule. The only need to
476
// modify this is for indent
477
// --------------------------------------------------
478
if (column == LEFT)
479             {
480                 x = leftRule;
481             }
482             else
483             {
484                 x = centerRule;
485             }
486
487             if (component != null)
488             {
489                 // --------------------------------------------------
490
// set the width for stretch based on BOTH, LEFT and
491
// RIGHT positionsing
492
// --------------------------------------------------
493
if ((constraints.stretch) && (constraints.position == TwoColumnConstraints.BOTH))
494                 {
495                     width = rightRule - leftRule;
496                     x = leftRule;
497                 }
498                 else if ((constraints.stretch) && (column == LEFT))
499                 {
500                     width = centerRule - leftRule;
501                 }
502                 else if ((constraints.stretch) && (column == RIGHT))
503                 {
504                     width = rightRule - centerRule;
505                 }
506
507                 // --------------------------------------------------
508
// if we straddle both columns but are not stretching
509
// use the preferred width as long as it is less then
510
// the width of both columns combined. Also set the x
511
// position to left, just to be sure.
512
// --------------------------------------------------
513
else if (constraints.position == TwoColumnConstraints.BOTH)
514                 {
515                     if (width > (rightRule - leftRule))
516                     {
517                         width = rightRule - leftRule;
518                     }
519                     x = leftRule;
520                 }
521
522                 // --------------------------------------------------
523
// correct for indent if this option is set
524
// --------------------------------------------------
525
if (constraints.indent)
526                 {
527                     width -= indent;
528                     x += indent;
529                 }
530
531                 component.setBounds(x, y, width, height);
532             }
533         }
534     }
535
536     /**
537      * Returns the minimum width of the column requested.
538      *
539      * @param column the columns to measure (LEFT / RIGHT)
540      * @param parent the component which needs to be laid out.
541      *
542      * @return the minimum width required to fis the components in this column
543      */

544     private int minimumColumnWidth(int column, Container parent)
545     {
546         Component component = null;
547         TwoColumnConstraints constraints = null;
548         int width = 0;
549         int temp = 0;
550
551         for (int i = 0; i < components[column].size(); i++)
552         {
553             constraints = (TwoColumnConstraints) components[column].elementAt(i);
554
555             if ((constraints != null) && (constraints.position != TwoColumnConstraints.BOTH))
556             {
557                 component = constraints.component;
558                 temp = (int) component.getMinimumSize().getWidth();
559
560                 if (constraints.indent)
561                 {
562                     temp += indent;
563                 }
564
565                 if (temp > width)
566                 {
567                     width = temp;
568                 }
569             }
570         }
571
572         return (width);
573     }
574
575     /**
576      * Retrunds the minimum width both columns together should have based on the minimum widths of
577      * all the components that straddle both columns and the minimum width of the title component.
578      *
579      * @param parent the component which needs to be laid out.
580      *
581      * @return the minimum width required to fis the components in this column
582      */

583     private int minimumBothColumnsWidth(Container parent)
584     {
585         Component component = null;
586         TwoColumnConstraints constraints = null;
587         int width = 0;
588         int temp = 0;
589
590         if (title != null)
591         {
592             component = title.component;
593             width = (int) component.getMinimumSize().getWidth();
594         }
595
596         for (int i = 0; i < components[LEFT].size(); i++)
597         {
598             constraints = (TwoColumnConstraints) components[LEFT].elementAt(i);
599
600             if ((constraints != null) && (constraints.position == TwoColumnConstraints.BOTH))
601             {
602                 component = constraints.component;
603                 temp = (int) component.getMinimumSize().getWidth();
604
605                 if (constraints.indent)
606                 {
607                     temp += indent;
608                 }
609
610                 if (temp > width)
611                 {
612                     width = temp;
613                 }
614             }
615         }
616
617         return (width);
618     }
619
620     private int minimumClusterHeight()
621     {
622         int height = 0;
623
624         for (int i = 0; i < rows(); i++)
625         {
626             height += rowHeight(i);
627         }
628
629         return (height);
630     }
631
632     /**
633      * Returns the number of rows that need to be laid out.
634      */

635     private int rows()
636     {
637         int rows = 0;
638         int leftRows = components[LEFT].size();
639         int rightRows = components[RIGHT].size();
640
641         if (leftRows > rightRows)
642         {
643             rows = leftRows;
644         }
645         else
646         {
647             rows = rightRows;
648         }
649
650         return (rows);
651     }
652
653     /**
654      * Measures and returns the minimum height required to render the components in the indicated
655      * row.
656      *
657      * @param row the index of the row to measure
658      */

659     private int rowHeight(int row)
660     {
661         int height = 0;
662         int height1 = height(row, LEFT);
663         int height2 = height(row, RIGHT);
664
665         // ----------------------------------------------------
666
// take the higher one
667
// ----------------------------------------------------
668
if (height1 > height2)
669         {
670             height = height1;
671         }
672         else
673         {
674             height = height2;
675         }
676
677         return (height);
678     }
679
680     /**
681      * Measures and returns the minimum height required to render the component in the indicated row
682      * and column.
683      *
684      * @param row the index of the row to measure
685      * @param column the column of the component to measure (<code>LEFT</code> or
686      * <code>RIGHT</code>)
687      */

688     private int height(int row, int column)
689     {
690         int height = 0;
691         int width = 0;
692         Component component;
693         TwoColumnConstraints constraints;
694
695         try
696         {
697             constraints = (TwoColumnConstraints) components[column].elementAt(row);
698             if (constraints != null)
699             {
700                 component = constraints.component;
701                 width = (int) component.getMinimumSize().getWidth();
702                 height = (int) component.getMinimumSize().getHeight();
703
704                 if (constraints.position == TwoColumnConstraints.WEST)
705                 {
706                     if (width > (centerRule - leftRule))
707                     {
708                         component.setBounds(0, 0, (centerRule - leftRule), height);
709                     }
710                 }
711                 else if (constraints.position == TwoColumnConstraints.EAST)
712                 {
713                     if (width > (rightRule - centerRule))
714                     {
715                         component.setBounds(0, 0, (rightRule - centerRule), height);
716                     }
717                 }
718                 else if (constraints.position == TwoColumnConstraints.BOTH)
719                 {
720                     if (width > (rightRule - leftRule))
721                     {
722                         component.setBounds(0, 0, (rightRule - leftRule), height);
723                     }
724                 }
725
726                 height = (int) component.getMinimumSize().getHeight();
727             }
728         }
729         // ----------------------------------------------------
730
// we might get an exception if one of the vectors is
731
// shorter, because we index out of bounds. If there
732
// is nothing there then the height is 0, nothing
733
// further to worry about!
734
// ----------------------------------------------------
735
catch (Throwable JavaDoc exception)
736         {}
737
738         return (height);
739     }
740
741     /**
742      * Computes the margin value based on the container width and the margin setting.
743      *
744      * @param parent the component which needs to be laid out.
745      */

746     private int margin(Container parent)
747     {
748         int amount = (int) (((parent.getSize().getWidth()) * margin) / 100);
749
750         return (amount);
751     }
752
753     /**
754      * Computes the top buffer value based on the container width and the setting for the top buffer
755      *
756      * @param usedHeight the amount of the parent component's height that is already in use (height
757      * of the title and the combined height of all rows).
758      * @param parent the component which needs to be laid out.
759      */

760     private int topBuffer(int usedHeight, Container parent)
761     {
762         int amount = ((int) parent.getSize().getHeight()) - usedHeight;
763         amount = (int) (amount * topBuffer) / 100;
764
765         return (amount);
766     }
767
768     /*--------------------------------------------------------------------------*/
769     /**
770      * Computes the indent value based on the container width and the indent setting.
771      *
772      * @param parent the component which needs to be laid out.
773      */

774     /*--------------------------------------------------------------------------*/
775     /*
776      * private int indent (Container parent) { int amount = (int)(((parent.getMinimumSize
777      * ().getWidth ()) * indent) / 100);
778      *
779      * return (amount); }
780      */

781     /**
782      * Calculates the preferred size dimensions for the specified panel given the components in the
783      * specified parent container.
784      *
785      * @param parent the component to be laid out
786      */

787     public Dimension preferredLayoutSize(Container parent)
788     {
789         return (minimumLayoutSize(parent));
790     }
791
792     /**
793      * Calculates the minimum size dimensions for the specified panel given the components in the
794      * specified parent container.
795      *
796      * @param parent the component to be laid out
797      */

798     public Dimension minimumLayoutSize(Container parent)
799     {
800         positionTitle(parent);
801
802         int width = minimumBothColumnsWidth(parent);
803         int height = minimumClusterHeight() + titleHeight;
804
805         return (new Dimension(width, height));
806     }
807
808     /**
809      * Calculates the maximum size dimensions for the specified panel given the components in the
810      * specified parent container.
811      *
812      * @param parent the component to be laid out
813      */

814     public Dimension maximumLayoutSize(Container parent)
815     {
816         return (minimumLayoutSize(parent));
817     }
818
819     /**
820      * Returns the alignment along the x axis. This specifies how the component would like to be
821      * aligned relative to other components. The value should be a number between 0 and 1 where 0
822      * represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is
823      * centered, etc.
824      *
825      * @param parent the component to be laid out
826      */

827     public float getLayoutAlignmentX(Container parent)
828     {
829         return (0);
830     }
831
832     /**
833      * Returns the alignment along the y axis. This specifies how the component would like to be
834      * aligned relative to other components. The value should be a number between 0 and 1 where 0
835      * represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is
836      * centered, etc.
837      *
838      * @param parent the component to be laid out
839      */

840     public float getLayoutAlignmentY(Container parent)
841     {
842         return (0);
843     }
844
845     /**
846      * Invalidates the layout, indicating that if the layout manager has cached information it
847      * should be discarded.
848      *
849      * @param parent the component to be laid out
850      */

851     public void invalidateLayout(Container parent)
852     {
853         leftRule = 0;
854         rightRule = 0;
855         centerRule = 0;
856         titleHeight = 0;
857     }
858
859     /**
860      * Adds the specified component with the specified name to the layout. This version is not
861      * supported, use <code>addLayoutComponent</code> with layout contsraints.
862      *
863      * @param name the component name
864      * @param comp the component to be added
865      */

866     public void addLayoutComponent(String JavaDoc name, Component comp)
867     {
868     }
869
870     /**
871      * This functionality removes the TwoColumnConstraints from Vectors
872      * so that alignment of components on UserInputPanel doesn't get
873      * dirty
874      *
875      * @param comp the component to be removed
876      */

877     public void removeLayoutComponent(Component comp)
878     {
879         Vector JavaDoc left = components[LEFT];
880         Vector JavaDoc right = components[RIGHT];
881
882         for (int i = 0; i < left.size(); i++)
883         {
884             TwoColumnConstraints constraints = (TwoColumnConstraints) left.get(i);
885             if (constraints == null)
886             {
887                 continue;
888             }
889             Component ctemp = constraints.component;
890             if (ctemp != null && ctemp.equals(comp))
891             {
892                 if (constraints.position == TwoColumnConstraints.BOTH || constraints.position == TwoColumnConstraints.WESTONLY)
893                 {
894                     right.remove(i);
895                 }
896                 break;
897             }
898         }
899
900         for (int j = 0; j < right.size(); j++)
901         {
902             TwoColumnConstraints constraints = (TwoColumnConstraints) right.get(j);
903             if (constraints == null)
904             {
905                 continue;
906             }
907             Component ctemp = constraints.component;
908             if (ctemp != null && ctemp.equals(comp))
909             {
910                 if (constraints.position == TwoColumnConstraints.BOTH || constraints.position == TwoColumnConstraints.EASTONLY)
911                 {
912                     left.remove(j);
913                 }
914                 break;
915             }
916         }
917     }
918
919     /**
920      * This method is provided for conveninence of debugging layout problems. It renders the three
921      * rules and the limit of the title marign visible after these positions have been computed. In
922      * addition, the indent locations are shown as dashed lines. To use this functionality do the
923      * following:<br>
924      * <br>
925      * <ul>
926      * <li>in the container using this layout manager override the <code>paint()</code> method.
927      * <li>in that method, first call <code>super.paint()</code>
928      * <li>then call this method
929      * </ul>
930      * <br>
931      * <b>Note:</b> cast the graphics object received in the <code>paint()</code> method to
932      * <code>Graphics2D</code> when making the call.<br>
933      * <br>
934      *
935      * @param graphics the graphics context used for drawing.
936      * @param color the color to use for rendering the layout grid
937      */

938     public void showRules(Graphics2D graphics, Color color)
939     {
940         int height = graphics.getClipBounds().height;
941
942         Stroke currentStroke = graphics.getStroke();
943         Color currentColor = graphics.getColor();
944
945         Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1.5f,
946                 new float[] { 10, 5}, 5);
947         graphics.setColor(color);
948
949         graphics.drawLine(leftRule, 0, leftRule, height);
950         graphics.drawLine(centerRule, titleHeight, centerRule, height);
951         graphics.drawLine(rightRule, 0, rightRule, height);
952         graphics.drawLine(leftRule, titleHeight, rightRule, titleHeight);
953
954         graphics.setStroke(stroke);
955         graphics.drawLine((leftRule + indent), titleHeight, (leftRule + indent), height);
956         graphics.drawLine((centerRule + indent), titleHeight, (centerRule + indent), height);
957
958         graphics.setStroke(currentStroke);
959         graphics.setColor(currentColor);
960     }
961 }
962 /*---------------------------------------------------------------------------*/
963
Popular Tags