KickJava   Java API By Example, From Geeks To Geeks.

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


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>GridData</code> is the layout data object associated with
19  * <code>GridLayout</code>. To set a <code>GridData</code> object into a
20  * control, you use the <code>Control.setLayoutData(Object)</code> method.
21  * <p>
22  * There are two ways to create a <code>GridData</code> object with certain
23  * fields set. The first is to set the fields directly, like this:
24  * <pre>
25  * GridData gridData = new GridData();
26  * gridData.horizontalAlignment = GridData.FILL;
27  * gridData.grabExcessHorizontalSpace = true;
28  * button1.setLayoutData(gridData);
29  * </pre>
30  * The second is to take advantage of convenience style bits defined
31  * by <code>GridData</code>:
32  * <pre>
33  * button1.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
34  * </pre>
35  * </p>
36  * <p>
37  * NOTE: Do not reuse <code>GridData</code> objects. Every control in a
38  * <code>Composite</code> that is managed by a <code>GridLayout</code>
39  * must have a unique <code>GridData</code> object. If the layout data
40  * for a control in a <code>GridLayout</code> is null at layout time,
41  * a unique <code>GridData</code> object is created for it.
42  * </p>
43  *
44  * @see GridLayout
45  * @see Control#setLayoutData
46  */

47 public final class GridData {
48     /**
49      * verticalAlignment specifies how controls will be positioned
50      * vertically within a cell.
51      *
52      * The default value is CENTER.
53      *
54      * Possible values are: <ul>
55      * <li>SWT.BEGINNING (or SWT.TOP): Position the control at the top of the cell</li>
56      * <li>SWT.CENTER: Position the control in the vertical center of the cell</li>
57      * <li>SWT.END (or SWT.BOTTOM): Position the control at the bottom of the cell</li>
58      * <li>SWT.FILL: Resize the control to fill the cell vertically</li>
59      * </ul>
60      */

61     public int verticalAlignment = CENTER;
62     
63     /**
64      * horizontalAlignment specifies how controls will be positioned
65      * horizontally within a cell.
66      *
67      * The default value is BEGINNING.
68      *
69      * Possible values are: <ul>
70      * <li>SWT.BEGINNING (or SWT.LEFT): Position the control at the left of the cell</li>
71      * <li>SWT.CENTER: Position the control in the horizontal center of the cell</li>
72      * <li>SWT.END (or SWT.RIGHT): Position the control at the right of the cell</li>
73      * <li>SWT.FILL: Resize the control to fill the cell horizontally</li>
74      * </ul>
75      */

76     public int horizontalAlignment = BEGINNING;
77     
78     /**
79      * widthHint specifies the preferred width in pixels. This value
80      * is the wHint passed into Control.computeSize(int, int, boolean)
81      * to determine the preferred size of the control.
82      *
83      * The default value is SWT.DEFAULT.
84      *
85      * @see Control#computeSize(int, int, boolean)
86      */

87     public int widthHint = SWT.DEFAULT;
88     
89     /**
90      * heightHint specifies the preferred height in pixels. This value
91      * is the hHint passed into Control.computeSize(int, int, boolean)
92      * to determine the preferred size of the control.
93      *
94      * The default value is SWT.DEFAULT.
95      *
96      * @see Control#computeSize(int, int, boolean)
97      */

98     public int heightHint = SWT.DEFAULT;
99     
100     /**
101      * horizontalIndent specifies the number of pixels of indentation
102      * that will be placed along the left side of the cell.
103      *
104      * The default value is 0.
105      */

106     public int horizontalIndent = 0;
107     
108     /**
109      * verticalIndent specifies the number of pixels of indentation
110      * that will be placed along the top side of the cell.
111      *
112      * The default value is 0.
113      *
114      * @since 3.1
115      */

116     public int verticalIndent = 0;
117     
118     /**
119      * horizontalSpan specifies the number of column cells that the control
120      * will take up.
121      *
122      * The default value is 1.
123      */

124     public int horizontalSpan = 1;
125     
126     /**
127      * verticalSpan specifies the number of row cells that the control
128      * will take up.
129      *
130      * The default value is 1.
131      */

132     public int verticalSpan = 1;
133     
134     /**
135      * <p>grabExcessHorizontalSpace specifies whether the width of the cell
136      * changes depending on the size of the parent Composite. If
137      * grabExcessHorizontalSpace is <code>true</code>, the following rules
138      * apply to the width of the cell:</p>
139      * <ul>
140      * <li>If extra horizontal space is available in the parent, the cell will
141      * grow to be wider than its preferred width. The new width
142      * will be "preferred width + delta" where delta is the extra
143      * horizontal space divided by the number of grabbing columns.</li>
144      * <li>If there is not enough horizontal space available in the parent, the
145      * cell will shrink until it reaches its minimum width as specified by
146      * GridData.minimumWidth. The new width will be the maximum of
147      * "minimumWidth" and "preferred width - delta", where delta is
148      * the amount of space missing divided by the number of grabbing columns.</li>
149      * <li>If the parent is packed, the cell will be its preferred width
150      * as specified by GridData.widthHint.</li>
151      * <li>If the control spans multiple columns and there are no other grabbing
152      * controls in any of the spanned columns, the last column in the span will
153      * grab the extra space. If there is at least one other grabbing control
154      * in the span, the grabbing will be spread over the columns already
155      * marked as grabExcessHorizontalSpace.</li>
156      * </ul>
157      *
158      * <p>The default value is false.</p>
159      *
160      * @see GridData#minimumWidth
161      * @see GridData#widthHint
162      */

163     public boolean grabExcessHorizontalSpace = false;
164     
165     /**
166      * <p>grabExcessVerticalSpace specifies whether the height of the cell
167      * changes depending on the size of the parent Composite. If
168      * grabExcessVerticalSpace is <code>true</code>, the following rules
169      * apply to the height of the cell:</p>
170      * <ul>
171      * <li>If extra vertical space is available in the parent, the cell will
172      * grow to be taller than its preferred height. The new height
173      * will be "preferred height + delta" where delta is the extra
174      * vertical space divided by the number of grabbing rows.</li>
175      * <li>If there is not enough vertical space available in the parent, the
176      * cell will shrink until it reaches its minimum height as specified by
177      * GridData.minimumHeight. The new height will be the maximum of
178      * "minimumHeight" and "preferred height - delta", where delta is
179      * the amount of space missing divided by the number of grabbing rows.</li>
180      * <li>If the parent is packed, the cell will be its preferred height
181      * as specified by GridData.heightHint.</li>
182      * <li>If the control spans multiple rows and there are no other grabbing
183      * controls in any of the spanned rows, the last row in the span will
184      * grab the extra space. If there is at least one other grabbing control
185      * in the span, the grabbing will be spread over the rows already
186      * marked as grabExcessVerticalSpace.</li>
187      * </ul>
188      *
189      * <p>The default value is false.</p>
190      *
191      * @see GridData#minimumHeight
192      * @see GridData#heightHint
193      */

194     public boolean grabExcessVerticalSpace = false;
195
196     /**
197      * minimumWidth specifies the minimum width in pixels. This value
198      * applies only if grabExcessHorizontalSpace is true. A value of
199      * SWT.DEFAULT means that the minimum width will be the result
200      * of Control.computeSize(int, int, boolean) where wHint is
201      * determined by GridData.widthHint.
202      *
203      * The default value is 0.
204      *
205      * @since 3.1
206      * @see Control#computeSize(int, int, boolean)
207      * @see GridData#widthHint
208      */

209     public int minimumWidth = 0;
210     
211     /**
212      * minimumHeight specifies the minimum height in pixels. This value
213      * applies only if grabExcessVerticalSpace is true. A value of
214      * SWT.DEFAULT means that the minimum height will be the result
215      * of Control.computeSize(int, int, boolean) where hHint is
216      * determined by GridData.heightHint.
217      *
218      * The default value is 0.
219      *
220      * @since 3.1
221      * @see Control#computeSize(int, int, boolean)
222      * @see GridData#heightHint
223      */

224     public int minimumHeight = 0;
225     
226     /**
227      * exclude informs the layout to ignore this control when sizing
228      * and positioning controls. If this value is <code>true</code>,
229      * the size and position of the control will not be managed by the
230      * layout. If this value is <code>false</code>, the size and
231      * position of the control will be computed and assigned.
232      *
233      * The default value is <code>false</code>.
234      *
235      * @since 3.1
236      */

237     public boolean exclude = false;
238     
239     /**
240      * Value for horizontalAlignment or verticalAlignment.
241      * Position the control at the top or left of the cell.
242      * Not recommended. Use SWT.BEGINNING, SWT.TOP or SWT.LEFT instead.
243      */

244     public static final int BEGINNING = SWT.BEGINNING;
245     
246     /**
247      * Value for horizontalAlignment or verticalAlignment.
248      * Position the control in the vertical or horizontal center of the cell
249      * Not recommended. Use SWT.CENTER instead.
250      */

251     public static final int CENTER = 2;
252     
253     /**
254      * Value for horizontalAlignment or verticalAlignment.
255      * Position the control at the bottom or right of the cell
256      * Not recommended. Use SWT.END, SWT.BOTTOM or SWT.RIGHT instead.
257      */

258     public static final int END = 3;
259     
260     /**
261      * Value for horizontalAlignment or verticalAlignment.
262      * Resize the control to fill the cell horizontally or vertically.
263      * Not recommended. Use SWT.FILL instead.
264      */

265     public static final int FILL = SWT.FILL;
266
267     /**
268      * Style bit for <code>new GridData(int)</code>.
269      * Position the control at the top of the cell.
270      * Not recommended. Use
271      * <code>new GridData(int, SWT.BEGINNING, boolean, boolean)</code>
272      * instead.
273      */

274     public static final int VERTICAL_ALIGN_BEGINNING = 1 << 1;
275     
276     /**
277      * Style bit for <code>new GridData(int)</code> to position the
278      * control in the vertical center of the cell.
279      * Not recommended. Use
280      * <code>new GridData(int, SWT.CENTER, boolean, boolean)</code>
281      * instead.
282      */

283     public static final int VERTICAL_ALIGN_CENTER = 1 << 2;
284     
285     /**
286      * Style bit for <code>new GridData(int)</code> to position the
287      * control at the bottom of the cell.
288      * Not recommended. Use
289      * <code>new GridData(int, SWT.END, boolean, boolean)</code>
290      * instead.
291      */

292     public static final int VERTICAL_ALIGN_END = 1 << 3;
293     
294     /**
295      * Style bit for <code>new GridData(int)</code> to resize the
296      * control to fill the cell vertically.
297      * Not recommended. Use
298      * <code>new GridData(int, SWT.FILL, boolean, boolean)</code>
299      * instead
300      */

301     public static final int VERTICAL_ALIGN_FILL = 1 << 4;
302     
303     /**
304      * Style bit for <code>new GridData(int)</code> to position the
305      * control at the left of the cell.
306      * Not recommended. Use
307      * <code>new GridData(SWT.BEGINNING, int, boolean, boolean)</code>
308      * instead.
309      */

310     public static final int HORIZONTAL_ALIGN_BEGINNING = 1 << 5;
311     
312     /**
313      * Style bit for <code>new GridData(int)</code> to position the
314      * control in the horizontal center of the cell.
315      * Not recommended. Use
316      * <code>new GridData(SWT.CENTER, int, boolean, boolean)</code>
317      * instead.
318      */

319     public static final int HORIZONTAL_ALIGN_CENTER = 1 << 6;
320     
321     /**
322      * Style bit for <code>new GridData(int)</code> to position the
323      * control at the right of the cell.
324      * Not recommended. Use
325      * <code>new GridData(SWT.END, int, boolean, boolean)</code>
326      * instead.
327      */

328     public static final int HORIZONTAL_ALIGN_END = 1 << 7;
329     
330     /**
331      * Style bit for <code>new GridData(int)</code> to resize the
332      * control to fill the cell horizontally.
333      * Not recommended. Use
334      * <code>new GridData(SWT.FILL, int, boolean, boolean)</code>
335      * instead.
336      */

337     public static final int HORIZONTAL_ALIGN_FILL = 1 << 8;
338     
339     /**
340      * Style bit for <code>new GridData(int)</code> to resize the
341      * control to fit the remaining horizontal space.
342      * Not recommended. Use
343      * <code>new GridData(int, int, true, boolean)</code>
344      * instead.
345      */

346     public static final int GRAB_HORIZONTAL = 1 << 9;
347     
348     /**
349      * Style bit for <code>new GridData(int)</code> to resize the
350      * control to fit the remaining vertical space.
351      * Not recommended. Use
352      * <code>new GridData(int, int, boolean, true)</code>
353      * instead.
354      */

355     public static final int GRAB_VERTICAL = 1 << 10;
356     
357     /**
358      * Style bit for <code>new GridData(int)</code> to resize the
359      * control to fill the cell vertically and to fit the remaining
360      * vertical space.
361      * FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL
362      * Not recommended. Use
363      * <code>new GridData(int, SWT.FILL, boolean, true)</code>
364      * instead.
365      */

366     public static final int FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL;
367     
368     /**
369      * Style bit for <code>new GridData(int)</code> to resize the
370      * control to fill the cell horizontally and to fit the remaining
371      * horizontal space.
372      * FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL
373      * Not recommended. Use
374      * <code>new GridData(SWT.FILL, int, true, boolean)</code>
375      * instead.
376      */

377     public static final int FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL;
378     
379     /**
380      * Style bit for <code>new GridData(int)</code> to resize the
381      * control to fill the cell horizontally and vertically and
382      * to fit the remaining horizontal and vertical space.
383      * FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL
384      * Not recommended. Use
385      * <code>new GridData(SWT.FILL, SWT.FILL, true, true)</code>
386      * instead.
387      */

388     public static final int FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL;
389
390     int cacheWidth = -1, cacheHeight = -1;
391     int defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1;
392     int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1;
393
394 /**
395  * Constructs a new instance of GridData using
396  * default values.
397  */

398 public GridData () {
399     super ();
400 }
401
402 /**
403  * Constructs a new instance based on the GridData style.
404  * This constructor is not recommended.
405  *
406  * @param style the GridData style
407  */

408 public GridData (int style) {
409     super ();
410     if ((style & VERTICAL_ALIGN_BEGINNING) != 0) verticalAlignment = BEGINNING;
411     if ((style & VERTICAL_ALIGN_CENTER) != 0) verticalAlignment = CENTER;
412     if ((style & VERTICAL_ALIGN_FILL) != 0) verticalAlignment = FILL;
413     if ((style & VERTICAL_ALIGN_END) != 0) verticalAlignment = END;
414     if ((style & HORIZONTAL_ALIGN_BEGINNING) != 0) horizontalAlignment = BEGINNING;
415     if ((style & HORIZONTAL_ALIGN_CENTER) != 0) horizontalAlignment = CENTER;
416     if ((style & HORIZONTAL_ALIGN_FILL) != 0) horizontalAlignment = FILL;
417     if ((style & HORIZONTAL_ALIGN_END) != 0) horizontalAlignment = END;
418     grabExcessHorizontalSpace = (style & GRAB_HORIZONTAL) != 0;
419     grabExcessVerticalSpace = (style & GRAB_VERTICAL) != 0;
420 }
421
422 /**
423  * Constructs a new instance of GridData according to the parameters.
424  *
425  * @param horizontalAlignment how control will be positioned horizontally within a cell
426  * @param verticalAlignment how control will be positioned vertically within a cell
427  * @param grabExcessHorizontalSpace whether cell will be made wide enough to fit the remaining horizontal space
428  * @param grabExcessVerticalSpace whether cell will be made high enough to fit the remaining vertical space
429  *
430  * @since 3.0
431  */

432 public GridData (int horizontalAlignment, int verticalAlignment, boolean grabExcessHorizontalSpace, boolean grabExcessVerticalSpace) {
433     this (horizontalAlignment, verticalAlignment, grabExcessHorizontalSpace, grabExcessVerticalSpace, 1, 1);
434 }
435
436 /**
437  * Constructs a new instance of GridData according to the parameters.
438  *
439  * @param horizontalAlignment how control will be positioned horizontally within a cell
440  * @param verticalAlignment how control will be positioned vertically within a cell
441  * @param grabExcessHorizontalSpace whether cell will be made wide enough to fit the remaining horizontal space
442  * @param grabExcessVerticalSpace whether cell will be made high enough to fit the remaining vertical space
443  * @param horizontalSpan the number of column cells that the control will take up
444  * @param verticalSpan the number of row cells that the control will take up
445  *
446  * @since 3.0
447  */

448 public GridData (int horizontalAlignment, int verticalAlignment, boolean grabExcessHorizontalSpace, boolean grabExcessVerticalSpace, int horizontalSpan, int verticalSpan) {
449     super ();
450     this.horizontalAlignment = horizontalAlignment;
451     this.verticalAlignment = verticalAlignment;
452     this.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
453     this.grabExcessVerticalSpace = grabExcessVerticalSpace;
454     this.horizontalSpan = horizontalSpan;
455     this.verticalSpan = verticalSpan;
456 }
457
458 /**
459  * Constructs a new instance of GridData according to the parameters.
460  * A value of SWT.DEFAULT indicates that no minimum width or
461  * no minimum height is specified.
462  *
463  * @param width a minimum width for the column
464  * @param height a minimum height for the row
465  *
466  * @since 3.0
467  */

468 public GridData (int width, int height) {
469     super ();
470     this.widthHint = width;
471     this.heightHint = height;
472 }
473
474 void computeSize (Control control, int wHint, int hHint, boolean flushCache) {
475     if (cacheWidth != -1 && cacheHeight != -1) return;
476     if (wHint == this.widthHint && hHint == this.heightHint) {
477         if (defaultWidth == -1 || defaultHeight == -1 || wHint != defaultWhint || hHint != defaultHhint) {
478             Point size = control.computeSize (wHint, hHint, flushCache);
479             defaultWhint = wHint;
480             defaultHhint = hHint;
481             defaultWidth = size.x;
482             defaultHeight = size.y;
483         }
484         cacheWidth = defaultWidth;
485         cacheHeight = defaultHeight;
486         return;
487     }
488     if (currentWidth == -1 || currentHeight == -1 || wHint != currentWhint || hHint != currentHhint) {
489         Point size = control.computeSize (wHint, hHint, flushCache);
490         currentWhint = wHint;
491         currentHhint = hHint;
492         currentWidth = size.x;
493         currentHeight = size.y;
494     }
495     cacheWidth = currentWidth;
496     cacheHeight = currentHeight;
497 }
498
499 void flushCache () {
500     cacheWidth = cacheHeight = -1;
501     defaultWidth = defaultHeight = -1;
502     currentWidth = currentHeight = -1;
503 }
504
505 String JavaDoc getName () {
506     String JavaDoc string = getClass ().getName ();
507     int index = string.lastIndexOf ('.');
508     if (index == -1) return string;
509     return string.substring (index + 1, string.length ());
510 }
511
512 /**
513  * Returns a string containing a concise, human-readable
514  * description of the receiver.
515  *
516  * @return a string representation of the GridData object
517  */

518 public String JavaDoc toString () {
519     String JavaDoc hAlign = "";
520     switch (horizontalAlignment) {
521         case SWT.FILL: hAlign = "SWT.FILL"; break;
522         case SWT.BEGINNING: hAlign = "SWT.BEGINNING"; break;
523         case SWT.LEFT: hAlign = "SWT.LEFT"; break;
524         case SWT.END: hAlign = "SWT.END"; break;
525         case END: hAlign = "GridData.END"; break;
526         case SWT.RIGHT: hAlign = "SWT.RIGHT"; break;
527         case SWT.CENTER: hAlign = "SWT.CENTER"; break;
528         case CENTER: hAlign = "GridData.CENTER"; break;
529         default: hAlign = "Undefined "+horizontalAlignment; break;
530     }
531     String JavaDoc vAlign = "";
532     switch (verticalAlignment) {
533         case SWT.FILL: vAlign = "SWT.FILL"; break;
534         case SWT.BEGINNING: vAlign = "SWT.BEGINNING"; break;
535         case SWT.TOP: vAlign = "SWT.TOP"; break;
536         case SWT.END: vAlign = "SWT.END"; break;
537         case END: vAlign = "GridData.END"; break;
538         case SWT.BOTTOM: vAlign = "SWT.BOTTOM"; break;
539         case SWT.CENTER: vAlign = "SWT.CENTER"; break;
540         case CENTER: vAlign = "GridData.CENTER"; break;
541         default: vAlign = "Undefined "+verticalAlignment; break;
542     }
543     String JavaDoc string = getName()+" {";
544     string += "horizontalAlignment="+hAlign+" ";
545     if (horizontalIndent != 0) string += "horizontalIndent="+horizontalIndent+" ";
546     if (horizontalSpan != 1) string += "horizontalSpan="+horizontalSpan+" ";
547     if (grabExcessHorizontalSpace) string += "grabExcessHorizontalSpace="+grabExcessHorizontalSpace+" ";
548     if (widthHint != SWT.DEFAULT) string += "widthHint="+widthHint+" ";
549     if (minimumWidth != 0) string += "minimumWidth="+minimumWidth+" ";
550     string += "verticalAlignment="+vAlign+" ";
551     if (verticalIndent != 0) string += "verticalIndent="+verticalIndent+" ";
552     if (verticalSpan != 1) string += "verticalSpan="+verticalSpan+" ";
553     if (grabExcessVerticalSpace) string += "grabExcessVerticalSpace="+grabExcessVerticalSpace+" ";
554     if (heightHint != SWT.DEFAULT) string += "heightHint="+heightHint+" ";
555     if (minimumHeight != 0) string += "minimumHeight="+minimumHeight+" ";
556     if (exclude) string += "exclude="+exclude+" ";
557     string = string.trim();
558     string += "}";
559     return string;
560 }
561 }
562
Popular Tags