KickJava   Java API By Example, From Geeks To Geeks.

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


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  
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.widgets.*;
17
18 /**
19  * Instances of this class are used to define the attachments
20  * of a control in a <code>FormLayout</code>.
21  * <p>
22  * To set a <code>FormData</code> object into a control, you use the
23  * <code>setLayoutData ()</code> method. To define attachments for the
24  * <code>FormData</code>, set the fields directly, like this:
25  * <pre>
26  * FormData data = new FormData();
27  * data.left = new FormAttachment(0,5);
28  * data.right = new FormAttachment(100,-5);
29  * button.setLayoutData(formData);
30  * </pre>
31  * </p>
32  * <p>
33  * <code>FormData</code> contains the <code>FormAttachments</code> for
34  * each edge of the control that the <code>FormLayout</code> uses to
35  * determine the size and position of the control. <code>FormData</code>
36  * objects also allow you to set the width and height of controls within
37  * a <code>FormLayout</code>.
38  * </p>
39  *
40  * @see FormLayout
41  * @see FormAttachment
42  *
43  * @since 2.0
44  */

45 public final class FormData {
46     /**
47      * width specifies the preferred width in pixels. This value
48      * is the wHint passed into Control.computeSize(int, int, boolean)
49      * to determine the preferred size of the control.
50      *
51      * The default value is SWT.DEFAULT.
52      *
53      * @see Control#computeSize(int, int, boolean)
54      */

55     public int width = SWT.DEFAULT;
56     /**
57      * height specifies the preferred height in pixels. This value
58      * is the hHint passed into Control.computeSize(int, int, boolean)
59      * to determine the preferred size of the control.
60      *
61      * The default value is SWT.DEFAULT.
62      *
63      * @see Control#computeSize(int, int, boolean)
64      */

65     public int height = SWT.DEFAULT;
66     /**
67      * left specifies the attachment of the left side of
68      * the control.
69      */

70     public FormAttachment left;
71     /**
72      * right specifies the attachment of the right side of
73      * the control.
74      */

75     public FormAttachment right;
76     /**
77      * top specifies the attachment of the top of the control.
78      */

79     public FormAttachment top;
80     /**
81      * bottom specifies the attachment of the bottom of the
82      * control.
83      */

84     public FormAttachment bottom;
85     
86     int cacheWidth = -1, cacheHeight = -1;
87     int defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1;
88     int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1;
89     FormAttachment cacheLeft, cacheRight, cacheTop, cacheBottom;
90     boolean isVisited, needed;
91     
92 /**
93  * Constructs a new instance of FormData using
94  * default values.
95  */

96 public FormData () {
97 }
98     
99 /**
100  * Constructs a new instance of FormData according to the parameters.
101  * A value of SWT.DEFAULT indicates that no minimum width or
102  * no minimum height is specified.
103  *
104  * @param width a minimum width for the control
105  * @param height a minimum height for the control
106  */

107 public FormData (int width, int height) {
108     this.width = width;
109     this.height = height;
110 }
111
112 void computeSize (Control control, int wHint, int hHint, boolean flushCache) {
113     if (cacheWidth != -1 && cacheHeight != -1) return;
114     if (wHint == this.width && hHint == this.height) {
115         if (defaultWidth == -1 || defaultHeight == -1 || wHint != defaultWhint || hHint != defaultHhint) {
116             Point size = control.computeSize (wHint, hHint, flushCache);
117             defaultWhint = wHint;
118             defaultHhint = hHint;
119             defaultWidth = size.x;
120             defaultHeight = size.y;
121         }
122         cacheWidth = defaultWidth;
123         cacheHeight = defaultHeight;
124         return;
125     }
126     if (currentWidth == -1 || currentHeight == -1 || wHint != currentWhint || hHint != currentHhint) {
127         Point size = control.computeSize (wHint, hHint, flushCache);
128         currentWhint = wHint;
129         currentHhint = hHint;
130         currentWidth = size.x;
131         currentHeight = size.y;
132     }
133     cacheWidth = currentWidth;
134     cacheHeight = currentHeight;
135 }
136
137 void flushCache () {
138     cacheWidth = cacheHeight = -1;
139     defaultHeight = defaultWidth = -1;
140     currentHeight = currentWidth = -1;
141 }
142
143 int getWidth (Control control, boolean flushCache) {
144     needed = true;
145     computeSize (control, width, height, flushCache);
146     return cacheWidth;
147 }
148
149 int getHeight (Control control, boolean flushCache) {
150     computeSize (control, width, height, flushCache);
151     return cacheHeight;
152 }
153
154 FormAttachment getBottomAttachment (Control control, int spacing, boolean flushCache) {
155     if (cacheBottom != null) return cacheBottom;
156     if (isVisited) return cacheBottom = new FormAttachment (0, getHeight (control, flushCache));
157     if (bottom == null) {
158         if (top == null) return cacheBottom = new FormAttachment (0, getHeight (control, flushCache));
159         return cacheBottom = getTopAttachment (control, spacing, flushCache).plus (getHeight (control, flushCache));
160     }
161     Control bottomControl = bottom.control;
162     if (bottomControl != null) {
163         if (bottomControl.isDisposed ()) {
164             bottom.control = bottomControl = null;
165         } else {
166             if (bottomControl.getParent () != control.getParent ()) {
167                 bottomControl = null;
168             }
169         }
170     }
171     if (bottomControl == null) return cacheBottom = bottom;
172     isVisited = true;
173     FormData bottomData = (FormData) bottomControl.getLayoutData ();
174     FormAttachment bottomAttachment = bottomData.getBottomAttachment (bottomControl, spacing, flushCache);
175     switch (bottom.alignment) {
176         case SWT.BOTTOM:
177             cacheBottom = bottomAttachment.plus (bottom.offset);
178             break;
179         case SWT.CENTER: {
180             FormAttachment topAttachment = bottomData.getTopAttachment (bottomControl, spacing, flushCache);
181             FormAttachment bottomHeight = bottomAttachment.minus (topAttachment);
182             cacheBottom = bottomAttachment.minus (bottomHeight.minus (getHeight (control, flushCache)).divide (2));
183             break;
184         }
185         default: {
186             FormAttachment topAttachment = bottomData.getTopAttachment (bottomControl, spacing, flushCache);
187             cacheBottom = topAttachment.plus (bottom.offset - spacing);
188             break;
189         }
190     }
191     isVisited = false;
192     return cacheBottom;
193 }
194
195 FormAttachment getLeftAttachment (Control control, int spacing, boolean flushCache) {
196     if (cacheLeft != null) return cacheLeft;
197     if (isVisited) return cacheLeft = new FormAttachment (0, 0);
198     if (left == null) {
199         if (right == null) return cacheLeft = new FormAttachment (0, 0);
200         return cacheLeft = getRightAttachment (control, spacing, flushCache).minus (getWidth (control, flushCache));
201     }
202     Control leftControl = left.control;
203     if (leftControl != null) {
204         if (leftControl.isDisposed ()) {
205             left.control = leftControl = null;
206         } else {
207             if (leftControl.getParent () != control.getParent ()) {
208                 leftControl = null;
209             }
210         }
211     }
212     if (leftControl == null) return cacheLeft = left;
213     isVisited = true;
214     FormData leftData = (FormData) leftControl.getLayoutData ();
215     FormAttachment leftAttachment = leftData.getLeftAttachment (leftControl, spacing, flushCache);
216     switch (left.alignment) {
217         case SWT.LEFT:
218             cacheLeft = leftAttachment.plus (left.offset);
219             break;
220         case SWT.CENTER: {
221             FormAttachment rightAttachment = leftData.getRightAttachment (leftControl, spacing, flushCache);
222             FormAttachment leftWidth = rightAttachment.minus (leftAttachment);
223             cacheLeft = leftAttachment.plus (leftWidth.minus (getWidth (control, flushCache)).divide (2));
224             break;
225         }
226         default: {
227             FormAttachment rightAttachment = leftData.getRightAttachment (leftControl, spacing, flushCache);
228             cacheLeft = rightAttachment.plus (left.offset + spacing);
229         }
230     }
231     isVisited = false;
232     return cacheLeft;
233 }
234     
235 String JavaDoc getName () {
236     String JavaDoc string = getClass ().getName ();
237     int index = string.lastIndexOf ('.');
238     if (index == -1) return string;
239     return string.substring (index + 1, string.length ());
240 }
241
242 FormAttachment getRightAttachment (Control control, int spacing, boolean flushCache) {
243     if (cacheRight != null) return cacheRight;
244     if (isVisited) return cacheRight = new FormAttachment (0, getWidth (control, flushCache));
245     if (right == null) {
246         if (left == null) return cacheRight = new FormAttachment (0, getWidth (control, flushCache));
247         return cacheRight = getLeftAttachment (control, spacing, flushCache).plus (getWidth (control, flushCache));
248     }
249     Control rightControl = right.control;
250     if (rightControl != null) {
251         if (rightControl.isDisposed ()) {
252             right.control = rightControl = null;
253         } else {
254             if (rightControl.getParent () != control.getParent ()) {
255                 rightControl = null;
256             }
257         }
258     }
259     if (rightControl == null) return cacheRight = right;
260     isVisited = true;
261     FormData rightData = (FormData) rightControl.getLayoutData ();
262     FormAttachment rightAttachment = rightData.getRightAttachment (rightControl, spacing, flushCache);
263     switch (right.alignment) {
264         case SWT.RIGHT:
265             cacheRight = rightAttachment.plus (right.offset);
266             break;
267         case SWT.CENTER: {
268             FormAttachment leftAttachment = rightData.getLeftAttachment (rightControl, spacing, flushCache);
269             FormAttachment rightWidth = rightAttachment.minus (leftAttachment);
270             cacheRight = rightAttachment.minus (rightWidth.minus (getWidth (control, flushCache)).divide (2));
271             break;
272         }
273         default: {
274             FormAttachment leftAttachment = rightData.getLeftAttachment (rightControl, spacing, flushCache);
275             cacheRight = leftAttachment.plus (right.offset - spacing);
276             break;
277         }
278     }
279     isVisited = false;
280     return cacheRight;
281 }
282
283 FormAttachment getTopAttachment (Control control, int spacing, boolean flushCache) {
284     if (cacheTop != null) return cacheTop;
285     if (isVisited) return cacheTop = new FormAttachment (0, 0);
286     if (top == null) {
287         if (bottom == null) return cacheTop = new FormAttachment (0, 0);
288         return cacheTop = getBottomAttachment (control, spacing, flushCache).minus (getHeight (control, flushCache));
289     }
290     Control topControl = top.control;
291     if (topControl != null) {
292         if (topControl.isDisposed ()) {
293             top.control = topControl = null;
294         } else {
295             if (topControl.getParent () != control.getParent ()) {
296                 topControl = null;
297             }
298         }
299     }
300     if (topControl == null) return cacheTop = top;
301     isVisited = true;
302     FormData topData = (FormData) topControl.getLayoutData ();
303     FormAttachment topAttachment = topData.getTopAttachment (topControl, spacing, flushCache);
304     switch (top.alignment) {
305         case SWT.TOP:
306             cacheTop = topAttachment.plus (top.offset);
307             break;
308         case SWT.CENTER: {
309             FormAttachment bottomAttachment = topData.getBottomAttachment (topControl, spacing, flushCache);
310             FormAttachment topHeight = bottomAttachment.minus (topAttachment);
311             cacheTop = topAttachment.plus (topHeight.minus (getHeight (control, flushCache)).divide (2));
312             break;
313         }
314         default: {
315             FormAttachment bottomAttachment = topData.getBottomAttachment (topControl, spacing, flushCache);
316             cacheTop = bottomAttachment.plus (top.offset + spacing);
317             break;
318         }
319     }
320     isVisited = false;
321     return cacheTop;
322 }
323
324 /**
325  * Returns a string containing a concise, human-readable
326  * description of the receiver.
327  *
328  * @return a string representation of the FormData object
329  */

330 public String JavaDoc toString () {
331     String JavaDoc string = getName()+" {";
332     if (width != SWT.DEFAULT) string += "width="+width+" ";
333     if (height != SWT.DEFAULT) string += "height="+height+" ";
334     if (left != null) string += "left="+left+" ";
335     if (right != null) string += "right="+right+" ";
336     if (top != null) string += "top="+top+" ";
337     if (bottom != null) string += "bottom="+bottom+" ";
338     string = string.trim();
339     string += "}";
340     return string;
341 }
342
343 }
344
Popular Tags