KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > ListLayout


1 package com.sshtools.ui.swing;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.Container JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.Insets JavaDoc;
7 import java.awt.LayoutManager2 JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.Vector JavaDoc;
10
11 /**
12  * A list layout puts elements in a vertical list, based on their
13  * vertical preferred size. The width is expanded automatically.
14  *
15  * @version 1.0
16  * @author Claude Duguay
17  */

18 public class ListLayout
19     extends AbstractLayout
20     implements LayoutManager2 JavaDoc,
21     Serializable JavaDoc {
22     public static final int CENTER = 0;
23     public static final int LEFT = 1;
24     public static final int RIGHT = 2;
25     public static final int BOTH = 3;
26     protected Vector JavaDoc tabs = new Vector JavaDoc();
27     protected Vector JavaDoc panels = new Vector JavaDoc();
28     protected Component JavaDoc center;
29     protected int index = 1;
30     protected int alignment = BOTH;
31
32     /**
33      * Constructs a ContextLayout with no gaps between components.
34      **/

35     public ListLayout() {
36         super();
37     }
38
39     /**
40      * Constructs a ContextLayout with no gaps between components
41      * and the specified alignment.
42      * @param alignment The LEFT, RIGHT, CENTER or BOTH alignment
43      **/

44     public ListLayout(int alignment) {
45         super();
46         this.alignment = alignment;
47     }
48
49     /**
50      * Constructs a ContextLayout with the specified gaps.
51      * @param hgap The horizontal gap
52      * @param vgap The vertical gap
53      **/

54     public ListLayout(int hgap, int vgap) {
55         super(hgap, vgap);
56     }
57
58     /**
59      * Constructs a ContextLayout with the specified gaps.
60      * @param hgap The horizontal gap
61      * @param vgap The vertical gap
62      * @param alignment The LEFT, RIGHT, CENTER or BOTH alignment
63      **/

64     public ListLayout(int hgap, int vgap, int alignment) {
65         super(hgap, vgap);
66         this.alignment = alignment;
67     }
68
69     /**
70      * Returns the minimum dimensions needed to layout the
71      * components contained in the specified target container.
72      * @param target The Container on which to do the layout
73      **/

74     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc target) {
75       Insets JavaDoc insets = target.getInsets();
76       int w = 0;
77       int h = insets.top;
78       Dimension JavaDoc size;
79       int ncomponents = target.getComponentCount();
80       for (int i = 0; i < ncomponents; i++) {
81           size = target.getComponent(i).getMinimumSize();
82           if (size.width > w) {
83               w = size.width;
84           }
85           h += size.height + vgap;
86       }
87       h += insets.bottom;
88       return new Dimension JavaDoc(w + (hgap * 2), h);
89     }
90
91     /**
92      * Returns the preferred dimensions for this layout given the
93      * components in the specified target container.
94      * @param target The component which needs to be laid out
95      **/

96     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc target) {
97         Insets JavaDoc insets = target.getInsets();
98         int w = 0;
99         int h = insets.top;
100         Dimension JavaDoc size;
101         int ncomponents = target.getComponentCount();
102         for (int i = 0; i < ncomponents; i++) {
103             size = target.getComponent(i).getPreferredSize();
104             if (size.width > w) {
105                 w = size.width;
106             }
107             h += size.height + vgap;
108         }
109         h += insets.bottom;
110         return new Dimension JavaDoc(w + (hgap * 2), h);
111     }
112
113     /**
114      * Lays out the specified container. This method will actually
115      * reshape the components in the specified target container in
116      * order to satisfy the constraints of the layout object.
117      * @param target The component being laid out
118      **/

119     public void layoutContainer(Container JavaDoc parent) {
120         Insets JavaDoc insets = parent.getInsets();
121         int w = parent.getSize().width;
122         Component JavaDoc comp;
123         Dimension JavaDoc size;
124         int position = insets.top;
125         int ncomponents = parent.getComponentCount();
126
127         for (int i = 0; i < ncomponents; i++) {
128             comp = parent.getComponent(i);
129             size = comp.getPreferredSize();
130
131             int h = size.height - insets.top - insets.bottom;
132
133             switch (alignment) {
134                 case CENTER: {
135                     int l = (w - size.width) / 2;
136                     comp.setBounds(insets.left + hgap + l, position,
137                                    size.width - insets.left - insets.right -
138                                    (hgap * 2), h);
139
140                     break;
141                 }
142                 case LEFT: {
143                     comp.setBounds(insets.left + hgap, position,
144                                    size.width - insets.left - insets.right -
145                                    (hgap * 2), h);
146
147                     break;
148                 }
149                 case RIGHT: {
150                     int l = w - size.width;
151                     comp.setBounds(insets.left + hgap + l, position,
152                                    size.width - insets.left - insets.right -
153                                    (hgap * 2), h);
154
155                     break;
156                 }
157                 default: {
158                     comp.setBounds(insets.left + hgap, position,
159                                    w - insets.left - insets.right - (hgap * 2),
160                                    h);
161
162                     break;
163                 }
164             }
165
166             position += (h + vgap);
167         }
168     }
169 }
170
Popular Tags