KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > FlowLayout


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4  
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7  
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9  */

10
11 package swingwt.awt;
12
13
14 public class FlowLayout implements LayoutManager {
15     
16     public static final int LEFT = 0;
17     public static final int CENTER = 1;
18     public static final int RIGHT = 2;
19     public static final int LEADING = 3;
20     public static final int TRAILING = 4;
21     
22     int align;
23     int newAlign;
24     int hgap;
25     int vgap;
26     
27     public FlowLayout() {
28         this(CENTER, 5, 5);
29     }
30     
31     public FlowLayout(int align) {
32         this(align, 5, 5);
33     }
34     
35     public FlowLayout(int align, int hgap, int vgap) {
36         this.hgap = hgap;
37         this.vgap = vgap;
38         setAlignment(align);
39     }
40     
41     public int getAlignment() {
42         return newAlign;
43     }
44     
45     public void setAlignment(int align) {
46         this.newAlign = align;
47         
48         switch (align) {
49             case LEADING :
50                 this.align = LEFT;
51                 break;
52             case TRAILING :
53                 this.align = RIGHT;
54                 break;
55             default :
56                 this.align = align;
57                 break;
58         }
59     }
60     
61     public int getHgap() {
62         return hgap;
63     }
64     
65     public void setHgap(int hgap) {
66         this.hgap = hgap;
67     }
68     
69     public int getVgap() {
70         return vgap;
71     }
72     
73     public void setVgap(int vgap) {
74         this.vgap = vgap;
75     }
76     
77     public void addLayoutComponent(String JavaDoc name, Component comp) {
78     }
79     
80     public void removeLayoutComponent(Component comp) {
81     }
82     
83     public Dimension preferredLayoutSize(Container target) {
84         Dimension dim = new Dimension(0, 0);
85         int nmembers = target.getComponentCount();
86         boolean firstVisibleComponent = true;
87
88         for (int i = 0; i < nmembers; i++) {
89             Component m = target.getComponent(i);
90             if (m.isVisible()) {
91                 Dimension d = m.getPreferredSize();
92                 dim.height = Math.max(dim.height, d.height);
93                 if (firstVisibleComponent) {
94                     firstVisibleComponent = false;
95                 } else {
96                     dim.width += hgap;
97                 }
98                 dim.width += d.width;
99             }
100         }
101         Insets insets = target.getInsets();
102         dim.width += insets.left + insets.right + hgap * 2;
103         dim.height += insets.top + insets.bottom + vgap * 2;
104         return dim;
105     }
106     
107     public Dimension minimumLayoutSize(Container target) {
108         Dimension dim = new Dimension(0, 0);
109         int nmembers = target.getComponentCount();
110
111         for (int i = 0; i < nmembers; i++) {
112             Component m = target.getComponent(i);
113             if (m.isVisible()) {
114                 Dimension d = m.getMinimumSize();
115                 dim.height = Math.max(dim.height, d.height);
116                 if (i > 0) {
117                     dim.width += hgap;
118                 }
119                 dim.width += d.width;
120             }
121         }
122         Insets insets = target.getInsets();
123         dim.width += insets.left + insets.right + hgap * 2;
124         dim.height += insets.top + insets.bottom + vgap * 2;
125         return dim;
126     }
127     
128     
129     public void layoutContainer(Container target) {
130         Insets insets = target.getInsets();
131         int maxwidth =
132         target.getWidth() - (insets.left + insets.right + hgap * 2);
133         int nmembers = target.getComponentCount();
134         int x = 0, y = insets.top + vgap;
135         int rowh = 0, start = 0;
136
137         boolean ltr = target.getComponentOrientation().isLeftToRight();
138
139         for (int i = 0; i < nmembers; i++) {
140             Component m = target.getComponent(i);
141             if (m.isVisible()) {
142                 Dimension d = m.getPreferredSize();
143                 m.setSize(d.width, d.height);
144
145                 if ((x == 0) || ((x + d.width) <= maxwidth)) {
146                     if (x > 0) {
147                         x += hgap;
148                     }
149                     x += d.width;
150                     rowh = Math.max(rowh, d.height);
151                 } else {
152                     setComponentPositions(target, insets.left + hgap, y, maxwidth - x, rowh, start, i, ltr);
153                     x = d.width;
154                     y += vgap + rowh;
155                     rowh = d.height;
156                     start = i;
157                 }
158             }
159         }
160         setComponentPositions(target, insets.left + hgap, y, maxwidth - x, rowh, start, nmembers, ltr);
161     }
162     
163     protected void setComponentPositions(
164                                     Container target,
165                                     int x,
166                                     int y,
167                                     int width,
168                                     int height,
169                                     int rowStart,
170                                     int rowEnd,
171                                     boolean ltr) {
172         switch (newAlign) {
173             case LEFT :
174                 x += ltr ? 0 : width;
175                 break;
176             case CENTER :
177                 x += width / 2;
178                 break;
179             case RIGHT :
180                 x += ltr ? width : 0;
181                 break;
182             case LEADING :
183                 break;
184             case TRAILING :
185                 x += width;
186                 break;
187         }
188         for (int i = rowStart; i < rowEnd; i++) {
189             Component m = target.getComponent(i);
190             if (m.isVisible()) {
191                 if (ltr) {
192                     m.setBounds(x, y + (height - m.getPreferredSize().height) / 2, m.getPreferredSize().width, m.getPreferredSize().height);
193                 } else {
194                     m.setBounds(target.getWidth() - x - m.getPreferredSize().width, y + (height - m.getPreferredSize().height) / 2, m.getPreferredSize().width, m.getPreferredSize().height);
195                 }
196                 x += m.getPreferredSize().width + hgap;
197             }
198         }
199     }
200
201     
202 }
203
Popular Tags