KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > swtcustomlayout > SWTFlowLayout


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    $Log: SWTFlowLayout.java,v $
11    Revision 1.3 2003/12/17 16:30:35 bobintetley
12    Flowlayout fix, vertical toolbar support and cleaned up text alignment
13    hierarchy.
14
15    Revision 1.2 2003/12/16 09:18:20 bobintetley
16    Integration with preferred sizes
17
18    Revision 1.1 2003/12/16 08:46:21 bobintetley
19    Changed name to match other files
20
21    Revision 1.1 2003/12/16 08:44:47 bobintetley
22    Better mapped FlowLayout
23
24
25 */

26
27 package swingwt.awt.swtcustomlayout;
28
29 import org.eclipse.swt.widgets.*;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.graphics.Rectangle;
32
33 public class SWTFlowLayout extends Layout {
34     
35     public static final int LEFT = 0;
36     public static final int CENTER = 1;
37     public static final int RIGHT = 2;
38     public static final int LEADING = 3;
39     public static final int TRAILING = 4;
40     
41     private int _alignment;
42     private int _hgap;
43     private int _vgap;
44
45     public SWTFlowLayout()
46     {
47         this(1, 5, 5);
48     }
49
50     public SWTFlowLayout(int align)
51     {
52         this(align, 5, 5);
53     }
54
55     public SWTFlowLayout(int align, int hgap, int vgap)
56     {
57         setHgap(hgap);
58         setVgap(vgap);
59         setAlignment(align);
60     }
61
62     protected void layout(Composite target, boolean flushCache)
63     {
64         Rectangle r = target.getClientArea();
65         Control children[] = target.getChildren();
66         int maxwidth = r.width - getHgap() * 2;
67         int nmembers = children.length;
68         int x = 0;
69         int y = getVgap();
70         int rowh = 0;
71         int start = 0;
72         boolean ltr = true;
73         for (int i = 0; i < nmembers; i++)
74         {
75             Control m = children[i];
76             if (m.isVisible())
77             {
78                 // Retreive the size info for this peer if it has one set so
79
// we can apply it in the layout
80
Point d;
81                 if (m.getData("size") == null)
82                     d = m.computeSize(-1, -1);
83                 else {
84                     swingwt.awt.Dimension dim = (swingwt.awt.Dimension) m.getData("size");
85                     d = m.computeSize(dim.width, dim.height);
86                 }
87                 
88                 m.setBounds(new Rectangle(0, 0, d.x, d.y));
89                 //m.setSize(d.x, d.y);
90

91                 if (x == 0 || x + d.x <= maxwidth)
92                 {
93                     if (x > 0)
94                         x += getHgap();
95                     x += d.x;
96                     rowh = Math.max(rowh, d.y);
97                 }
98                 else
99                 {
100                     moveComponents(target, r, getHgap(), y, maxwidth - x, rowh, start, i, ltr);
101                     x = d.x;
102                     y += getVgap() + rowh;
103                     rowh = d.y;
104                     start = i;
105                 }
106             }
107         }
108         moveComponents(target, r, getHgap(), y, maxwidth - x, rowh, start, nmembers, ltr);
109     }
110
111     protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache)
112     {
113         Point size = new Point(0, 0);
114         Control children[] = composite.getChildren();
115         boolean firstVisibleComponent = true;
116         for (int i = 0; i < children.length; i++)
117         {
118             Control m = children[i];
119             if (m.isVisible())
120             {
121                 Point d = m.computeSize(-1, -1);
122                 size.y = Math.max(size.y, d.y);
123                 if (firstVisibleComponent)
124                     firstVisibleComponent = false;
125                 else
126                     size.x += getHgap();
127                 size.x += d.x;
128             }
129         }
130         size.x += getHgap() * 2;
131         size.y += getVgap() * 2;
132         return size;
133     }
134
135     public int getAlignment()
136     {
137         return _alignment;
138     }
139
140     public void setAlignment(int align)
141     {
142         _alignment = align;
143     }
144
145     public int getHgap()
146     {
147         return _hgap;
148     }
149
150     public void setHgap(int hgap)
151     {
152         _hgap = hgap;
153     }
154
155     public int getVgap()
156     {
157         return _vgap;
158     }
159
160     public void setVgap(int vgap)
161     {
162         _vgap = vgap;
163     }
164
165     private void moveComponents(Composite target, Rectangle r, int x, int y, int width, int height, int rowStart, int rowEnd, boolean ltr)
166     {
167         switch (getAlignment())
168         {
169         case 0:
170             x += ltr ? 0 : width;
171             break;
172
173         case 1:
174             x += width / 2;
175             break;
176
177         case 2:
178             x += ltr ? width : 0;
179             break;
180
181         case 4:
182             x += width;
183             break;
184         }
185         Control children[] = target.getChildren();
186         for (int i = rowStart; i < rowEnd; i++)
187         {
188             Control m = children[i];
189             Point size = m.getSize();
190             
191             if (m.isVisible())
192             {
193                 if (ltr)
194                     m.setLocation(x, y + (height - size.y) / 2);
195                 else
196                     m.setLocation(r.width - x - size.x, y + (height - size.y) / 2);
197                 x += size.x + getHgap();
198             }
199         }
200     }
201
202     public String JavaDoc toString()
203     {
204         return "[" + paramString() + "]";
205     }
206
207     protected String JavaDoc paramString()
208     {
209         String JavaDoc str;
210         str = "";
211         switch (getAlignment())
212         {
213         case 0:
214             str = ",align=left";
215             break;
216
217         case 1:
218             str = ",align=center";
219             break;
220
221         case 2:
222             str = ",align=right";
223             break;
224
225         case 3:
226             str = ",align=leading";
227             break;
228
229         case 4:
230             str = ",align=trailing";
231             break;
232         }
233         return getHgap() + ",vgap=" + getVgap() + str;
234     }
235 }
236
Popular Tags