KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > layout > FillLayout


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.layout;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.*;
16
17 /**
18  * <code>FillLayout</code> is the simplest layout class. It lays out
19  * controls in a single row or column, forcing them to be the same size.
20  * <p>
21  * Initially, the controls will all be as tall as the tallest control,
22  * and as wide as the widest. <code>FillLayout</code> does not wrap,
23  * but you can specify margins and spacing. You might use it to
24  * lay out buttons in a task bar or tool bar, or to stack checkboxes
25  * in a <code>Group</code>. <code>FillLayout</code> can also be used
26  * when a <code>Composite</code> only has one child. For example,
27  * if a <code>Shell</code> has a single <code>Group</code> child,
28  * <code>FillLayout</code> will cause the <code>Group</code> to
29  * completely fill the <code>Shell</code> (if margins are 0).
30  * </p>
31  * <p>
32  * Example code: first a <code>FillLayout</code> is created and
33  * its type field is set, and then the layout is set into the
34  * <code>Composite</code>. Note that in a <code>FillLayout</code>,
35  * children are always the same size, and they fill all available space.
36  * <pre>
37  * FillLayout fillLayout = new FillLayout();
38  * fillLayout.type = SWT.VERTICAL;
39  * shell.setLayout(fillLayout);
40  * </pre>
41  * </p>
42  */

43 public final class FillLayout extends Layout {
44     /**
45      * type specifies how controls will be positioned
46      * within the layout.
47      *
48      * The default value is HORIZONTAL.
49      *
50      * Possible values are: <ul>
51      * <li>HORIZONTAL: Position the controls horizontally from left to right</li>
52      * <li>VERTICAL: Position the controls vertically from top to bottom</li>
53      * </ul>
54      */

55     public int type = SWT.HORIZONTAL;
56     
57     /**
58      * marginWidth specifies the number of pixels of horizontal margin
59      * that will be placed along the left and right edges of the layout.
60      *
61      * The default value is 0.
62      *
63      * @since 3.0
64      */

65     public int marginWidth = 0;
66     
67     /**
68      * marginHeight specifies the number of pixels of vertical margin
69      * that will be placed along the top and bottom edges of the layout.
70      *
71      * The default value is 0.
72      *
73      * @since 3.0
74      */

75     public int marginHeight = 0;
76     
77     /**
78      * spacing specifies the number of pixels between the edge of one cell
79      * and the edge of its neighbouring cell.
80      *
81      * The default value is 0.
82      *
83      * @since 3.0
84      */

85     public int spacing = 0;
86     
87 /**
88  * Constructs a new instance of this class.
89  */

90 public FillLayout () {
91 }
92
93 /**
94  * Constructs a new instance of this class given the type.
95  *
96  * @param type the type of fill layout
97  *
98  * @since 2.0
99  */

100 public FillLayout (int type) {
101     this.type = type;
102 }
103
104 protected Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache) {
105     Control [] children = composite.getChildren ();
106     int count = children.length;
107     int maxWidth = 0, maxHeight = 0;
108     for (int i=0; i<count; i++) {
109         Control child = children [i];
110         int w = wHint, h = hHint;
111         if (count > 0) {
112             if (type == SWT.HORIZONTAL && wHint != SWT.DEFAULT) {
113                 w = Math.max (0, (wHint - (count - 1) * spacing) / count);
114             }
115             if (type == SWT.VERTICAL && hHint != SWT.DEFAULT) {
116                 h = Math.max (0, (hHint - (count - 1) * spacing) / count);
117             }
118         }
119         Point size = computeChildSize (child, w, h, flushCache);
120         maxWidth = Math.max (maxWidth, size.x);
121         maxHeight = Math.max (maxHeight, size.y);
122     }
123     int width = 0, height = 0;
124     if (type == SWT.HORIZONTAL) {
125         width = count * maxWidth;
126         if (count != 0) width += (count - 1) * spacing;
127         height = maxHeight;
128     } else {
129         width = maxWidth;
130         height = count * maxHeight;
131         if (count != 0) height += (count - 1) * spacing;
132     }
133     width += marginWidth * 2;
134     height += marginHeight * 2;
135     if (wHint != SWT.DEFAULT) width = wHint;
136     if (hHint != SWT.DEFAULT) height = hHint;
137     return new Point (width, height);
138 }
139
140 Point computeChildSize (Control control, int wHint, int hHint, boolean flushCache) {
141     FillData data = (FillData)control.getLayoutData ();
142     if (data == null) {
143         data = new FillData ();
144         control.setLayoutData (data);
145     }
146     Point size = null;
147     if (wHint == SWT.DEFAULT && hHint == SWT.DEFAULT) {
148         size = data.computeSize (control, wHint, hHint, flushCache);
149     } else {
150         // TEMPORARY CODE
151
int trimX, trimY;
152         if (control instanceof Scrollable) {
153             Rectangle rect = ((Scrollable) control).computeTrim (0, 0, 0, 0);
154             trimX = rect.width;
155             trimY = rect.height;
156         } else {
157             trimX = trimY = control.getBorderWidth () * 2;
158         }
159         int w = wHint == SWT.DEFAULT ? wHint : Math.max (0, wHint - trimX);
160         int h = hHint == SWT.DEFAULT ? hHint : Math.max (0, hHint - trimY);
161         size = data.computeSize (control, w, h, flushCache);
162     }
163     return size;
164 }
165
166 protected boolean flushCache (Control control) {
167     Object JavaDoc data = control.getLayoutData();
168     if (data != null) ((FillData)data).flushCache();
169     return true;
170 }
171
172 String JavaDoc getName () {
173     String JavaDoc string = getClass ().getName ();
174     int index = string.lastIndexOf ('.');
175     if (index == -1) return string;
176     return string.substring (index + 1, string.length ());
177 }
178
179 protected void layout (Composite composite, boolean flushCache) {
180     Rectangle rect = composite.getClientArea ();
181     Control [] children = composite.getChildren ();
182     int count = children.length;
183     if (count == 0) return;
184     int width = rect.width - marginWidth * 2;
185     int height = rect.height - marginHeight * 2;
186     if (type == SWT.HORIZONTAL) {
187         width -= (count - 1) * spacing;
188         int x = rect.x + marginWidth, extra = width % count;
189         int y = rect.y + marginHeight, cellWidth = width / count;
190         for (int i=0; i<count; i++) {
191             Control child = children [i];
192             int childWidth = cellWidth;
193             if (i == 0) {
194                 childWidth += extra / 2;
195             } else {
196                 if (i == count - 1) childWidth += (extra + 1) / 2;
197             }
198             child.setBounds (x, y, childWidth, height);
199             x += childWidth + spacing;
200         }
201     } else {
202         height -= (count - 1) * spacing;
203         int x = rect.x + marginWidth, cellHeight = height / count;
204         int y = rect.y + marginHeight, extra = height % count;
205         for (int i=0; i<count; i++) {
206             Control child = children [i];
207             int childHeight = cellHeight;
208             if (i == 0) {
209                 childHeight += extra / 2;
210             } else {
211                 if (i == count - 1) childHeight += (extra + 1) / 2;
212             }
213             child.setBounds (x, y, width, childHeight);
214             y += childHeight + spacing;
215         }
216     }
217 }
218
219 /**
220  * Returns a string containing a concise, human-readable
221  * description of the receiver.
222  *
223  * @return a string representation of the layout
224  */

225 public String JavaDoc toString () {
226     String JavaDoc string = getName ()+" {";
227     string += "type="+((type == SWT.VERTICAL) ? "SWT.VERTICAL" : "SWT.HORIZONTAL")+" ";
228     if (marginWidth != 0) string += "marginWidth="+marginWidth+" ";
229     if (marginHeight != 0) string += "marginHeight="+marginHeight+" ";
230     if (spacing != 0) string += "spacing="+spacing+" ";
231     string = string.trim();
232     string += "}";
233     return string;
234 }
235 }
236
Popular Tags