KickJava   Java API By Example, From Geeks To Geeks.

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


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.widgets.*;
15
16 /**
17  * Instances of this class are used to define the edges of a control
18  * within a <code>FormLayout</code>.
19  * <p>
20  * <code>FormAttachments</code> are set into the top, bottom, left,
21  * and right fields of the <code>FormData</code> for a control.
22  * For example:
23  * <pre>
24  * FormData data = new FormData();
25  * data.top = new FormAttachment(0,5);
26  * data.bottom = new FormAttachment(100,-5);
27  * data.left = new FormAttachment(0,5);
28  * data.right = new FormAttachment(100,-5);
29  * button.setLayoutData(data);
30  * </pre>
31  * </p>
32  * <p>
33  * A <code>FormAttachment</code> defines where to attach the side of
34  * a control by using the equation, y = ax + b. The "a" term represents
35  * a fraction of the parent composite's width (from the left) or height
36  * (from the top). It can be defined using a numerator and denominator,
37  * or just a percentage value. If a percentage is used, the denominator
38  * is set to 100. The "b" term in the equation represents an offset, in
39  * pixels, from the attachment position. For example:
40  * <pre>
41  * FormAttachment attach = new FormAttachment (20, -5);
42  * </pre>
43  * specifies that the side to which the <code>FormAttachment</code>
44  * object belongs will lie at 20% of the parent composite, minus 5 pixels.
45  * </p>
46  * <p>
47  * Control sides can also be attached to another control.
48  * For example:
49  * <pre>
50  * FormAttachment attach = new FormAttachment (button, 10);
51  * </pre>
52  * specifies that the side to which the <code>FormAttachment</code>
53  * object belongs will lie in the same position as the adjacent side of
54  * the <code>button</code> control, plus 10 pixels. The control side can
55  * also be attached to the opposite side of the specified control.
56  * For example:
57  * <pre>
58  * FormData data = new FormData ();
59  * data.left = new FormAttachment (button, 0, SWT.LEFT);
60  * </pre>
61  * specifies that the left side of the control will lie in the same position
62  * as the left side of the <code>button</code> control. The control can also
63  * be attached in a position that will center the control on the specified
64  * control. For example:
65  * <pre>
66  * data.left = new FormAttachment (button, 0, SWT.CENTER);
67  * </pre>
68  * specifies that the left side of the control will be positioned so that it is
69  * centered between the left and right sides of the <code>button</code> control.
70  * If the alignment is not specified, the default is to attach to the adjacent side.
71  * </p>
72  *
73  * @see FormLayout
74  * @see FormData
75  *
76  * @since 2.0
77  */

78 public final class FormAttachment {
79     /**
80      * numerator specifies the numerator of the "a" term in the
81      * equation, y = ax + b, which defines the attachment.
82      */

83     public int numerator;
84     
85     /**
86      * denominator specifies the denominator of the "a" term in the
87      * equation, y = ax + b, which defines the attachment.
88      *
89      * The default value is 100.
90      */

91     public int denominator = 100;
92     
93     /**
94      * offset specifies the offset, in pixels, of the control side
95      * from the attachment position.
96      * If the offset is positive, then the control side is offset
97      * to the right of or below the attachment position. If it is
98      * negative, then the control side is offset to the left of or
99      * above the attachment position.
100      *
101      * This is equivalent to the "b" term in the equation y = ax + b.
102      * The default value is 0.
103      */

104     public int offset;
105     
106     /**
107      * control specifies the control to which the control side is
108      * attached.
109      */

110     public Control control;
111     
112     /**
113      * alignment specifies the alignment of the control side that is
114      * attached to a control.
115      * <p>
116      * For top and bottom attachments, TOP, BOTTOM and CENTER are used. For left
117      * and right attachments, LEFT, RIGHT and CENTER are used. If any other case
118      * occurs, the default will be used instead.
119      * </p>
120      *
121      * <br>Possible values are: <ul>
122      * <li>TOP: Attach the side to the top side of the specified control.</li>
123      * <li>BOTTOM : Attach the side to the bottom side of the specified control.</li>
124      * <li>LEFT: Attach the side to the left side of the specified control.</li>
125      * <li>RIGHT: Attach the side to the right side of the specified control.</li>
126      * <li>CENTER: Attach the side at a position which will center the control on the specified control.</li>
127      * <li>DEFAULT: Attach the side to the adjacent side of the specified control.</li>
128      * </ul>
129      */

130     public int alignment;
131
132 /**
133  * Constructs a new instance of this class.
134  * Since no numerator, denominator or offset is specified,
135  * the attachment is treated as a percentage of the form.
136  * The numerator is zero, the denominator is 100 and the
137  * offset is zero.
138  *
139  * @since 3.2
140  */

141 public FormAttachment () {
142 }
143
144 /**
145  * Constructs a new instance of this class given a numerator
146  * Since no denominator or offset is specified, the default
147  * is to treat the numerator as a percentage of the form, with a
148  * denominator of 100. The offset is zero.
149  *
150  * @param numerator the percentage of the position
151  *
152  * @since 3.0
153  */

154 public FormAttachment (int numerator) {
155     this (numerator, 100, 0);
156 }
157
158 /**
159  * Constructs a new instance of this class given a numerator
160  * and an offset. Since no denominator is specified, the default
161  * is to treat the numerator as a percentage of the form, with a
162  * denominator of 100.
163  *
164  * @param numerator the percentage of the position
165  * @param offset the offset of the side from the position
166  */

167 public FormAttachment (int numerator, int offset) {
168     this (numerator, 100, offset);
169 }
170
171 /**
172  * Constructs a new instance of this class given a numerator
173  * and denominator and an offset. The position of the side is
174  * given by the fraction of the form defined by the numerator
175  * and denominator.
176  *
177  * @param numerator the numerator of the position
178  * @param denominator the denominator of the position
179  * @param offset the offset of the side from the position
180  */

181 public FormAttachment (int numerator, int denominator, int offset) {
182     if (denominator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);
183     this.numerator = numerator;
184     this.denominator = denominator;
185     this.offset = offset;
186 }
187
188 /**
189  * Constructs a new instance of this class given a control.
190  * Since no alignment is specified, the default alignment is
191  * to attach the side to the adjacent side of the specified
192  * control. Since no offset is specified, an offset of 0 is
193  * used.
194  *
195  * @param control the control the side is attached to
196  */

197 public FormAttachment (Control control) {
198     this (control, 0, SWT.DEFAULT);
199 }
200
201 /**
202  * Constructs a new instance of this class given a control
203  * and an offset. Since no alignment is specified, the default
204  * alignment is to attach the side to the adjacent side of the
205  * specified control.
206  *
207  * @param control the control the side is attached to
208  * @param offset the offset of the side from the control
209  */

210 public FormAttachment (Control control, int offset) {
211     this (control, offset, SWT.DEFAULT);
212 }
213
214 /**
215  * Constructs a new instance of this class given a control,
216  * an offset and an alignment.
217  *
218  * @param control the control the side is attached to
219  * @param offset the offset of the side from the control
220  * @param alignment the alignment of the side to the control it is attached to
221  */

222 public FormAttachment (Control control, int offset, int alignment) {
223     this.control = control;
224     this.offset = offset;
225     this.alignment = alignment;
226 }
227
228 FormAttachment divide (int value) {
229     return new FormAttachment (numerator, denominator * value, offset / value);
230 }
231
232 int gcd (int m, int n) {
233     int temp;
234     m = Math.abs (m);
235     n = Math.abs (n);
236     if (m < n) {
237         temp = m;
238         m = n;
239         n = temp;
240     }
241     while (n != 0){
242         temp = m;
243         m = n;
244         n = temp % n;
245     }
246     return m;
247 }
248
249 FormAttachment minus (FormAttachment attachment) {
250     FormAttachment solution = new FormAttachment ();
251     solution.numerator = numerator * attachment.denominator - denominator * attachment.numerator;
252     solution.denominator = denominator * attachment.denominator;
253     int gcd = gcd (solution.denominator, solution.numerator);
254     solution.numerator = solution.numerator / gcd;
255     solution.denominator = solution.denominator / gcd;
256     solution.offset = offset - attachment.offset;
257     return solution;
258 }
259
260 FormAttachment minus (int value) {
261     return new FormAttachment (numerator, denominator, offset - value);
262 }
263
264 FormAttachment plus (FormAttachment attachment) {
265     FormAttachment solution = new FormAttachment ();
266     solution.numerator = numerator * attachment.denominator + denominator * attachment.numerator;
267     solution.denominator = denominator * attachment.denominator;
268     int gcd = gcd (solution.denominator, solution.numerator);
269     solution.numerator = solution.numerator / gcd;
270     solution.denominator = solution.denominator / gcd;
271     solution.offset = offset + attachment.offset;
272     return solution;
273 }
274
275 FormAttachment plus (int value) {
276     return new FormAttachment (numerator, denominator, offset + value);
277 }
278
279 int solveX (int value) {
280     if (denominator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);
281     return ((numerator * value) / denominator) + offset;
282 }
283
284 int solveY (int value) {
285     if (numerator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);
286     return (value - offset) * denominator / numerator;
287 }
288     
289 /**
290  * Returns a string containing a concise, human-readable
291  * description of the receiver.
292  *
293  * @return a string representation of the FormAttachment
294  */

295 public String JavaDoc toString () {
296     String JavaDoc string = control != null ? control.toString () : numerator + "/" + denominator;
297     return "{y = (" + string + (offset >= 0 ? ")x + " + offset: ")x - " + (-offset))+"}";
298 }
299
300 }
301
Popular Tags