1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 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
|
* Asim Ullah - Ported for use in draw2d (c.f Bugzilla 71684).[Sep 10, 2004]
|
11
|
*******************************************************************************/
|
12
|
package org.eclipse.draw2d;
|
13
|
|
14
|
|
15
|
import org.eclipse.draw2d.geometry.Dimension;
|
16
|
import org.eclipse.draw2d.rap.swt.SWT;
|
17
|
|
18
|
/**
|
19
|
* <code>GridData</code> is the layout data object associated with
|
20
|
* <code>GridLayout</code>. To set a <code>GridData</code> object into a
|
21
|
* <code>Figure</code>, you use the <code>setConstraint()</code> method of
|
22
|
* <code>GridLayout</code> to map the <code>Figure</code> to its layout
|
23
|
* <code>GridData</code>.
|
24
|
* <p>
|
25
|
* There are two ways to create a <code>GridData</code> object with certain
|
26
|
* fields set. The first is to set the fields directly, like this:
|
27
|
*
|
28
|
* <pre>
|
29
|
* GridData gridData = new GridData();
|
30
|
* gridData.horizontalAlignment = GridData.FILL;
|
31
|
* gridData.grabExcessHorizontalSpace = true;
|
32
|
*
|
33
|
* // associate the figure to the GridData object
|
34
|
* myGridlayout.setConstraint(myFigure, gridData);
|
35
|
* </pre>
|
36
|
*
|
37
|
* The second is to take advantage of convenience style bits defined by
|
38
|
* <code>GridData</code>:
|
39
|
*
|
40
|
* <pre>
|
41
|
* GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
|
42
|
* | GridData.GRAB_HORIZONTAL);
|
43
|
* </pre>
|
44
|
*
|
45
|
* </p>
|
46
|
* <p>
|
47
|
* NOTE: Do not reuse <code>GridData</code> objects. Every child in the parent
|
48
|
* <code>Figure</code> that is managed by the <code>GridLayout</code> must have
|
49
|
* a unique <code>GridData</code> object. If the layout data for a Grid member
|
50
|
* in a <code>GridLayout</code> is null at layout time, a unique
|
51
|
* <code>GridData</code> object is created for it.
|
52
|
* </p>
|
53
|
*
|
54
|
* @see GridLayout
|
55
|
*/
|
56
|
public final class GridData {
|
57
|
/**
|
58
|
* verticalAlignment specifies how figures will be positioned vertically
|
59
|
* within a cell.
|
60
|
*
|
61
|
* The default value is CENTER.
|
62
|
*
|
63
|
* Possible values are:
|
64
|
*
|
65
|
* SWT.BEGINNING (or SWT.TOP): Position the figure at the top of the cell
|
66
|
* SWT.CENTER: Position the figure in the vertical center of the cell
|
67
|
* SWT.END (or SWT.BOTTOM): Position the figure at the bottom of the cell
|
68
|
* SWT.FILL: Resize the figure to fill the cell vertically
|
69
|
*/
|
70
|
public int verticalAlignment = CENTER;
|
71
|
|
72
|
/**
|
73
|
* horizontalAlignment specifies how figures will be positioned horizontally
|
74
|
* within a cell.
|
75
|
*
|
76
|
* The default value is BEGINNING.
|
77
|
*
|
78
|
* Possible values are:
|
79
|
*
|
80
|
* SWT.BEGINNING (or SWT.LEFT): Position the figure at the left of the cell
|
81
|
* SWT.CENTER: Position the figure in the horizontal center of the cell
|
82
|
* SWT.END (or SWT.RIGHT): Position the figure at the right of the cell
|
83
|
* SWT.FILL: Resize the figure to fill the cell horizontally
|
84
|
*/
|
85
|
public int horizontalAlignment = BEGINNING;
|
86
|
|
87
|
/**
|
88
|
* widthHint specifies a minimum width for the column. A value of
|
89
|
* SWT.DEFAULT indicates that no minimum width is specified.
|
90
|
*
|
91
|
* The default value is SWT.DEFAULT.
|
92
|
*/
|
93
|
public int widthHint = SWT.DEFAULT;
|
94
|
|
95
|
/**
|
96
|
* heightHint specifies a minimum height for the row. A value of SWT.DEFAULT
|
97
|
* indicates that no minimum height is specified.
|
98
|
*
|
99
|
* The default value is SWT.DEFAULT.
|
100
|
*/
|
101
|
public int heightHint = SWT.DEFAULT;
|
102
|
|
103
|
/**
|
104
|
* horizontalIndent specifies the number of pixels of indentation that will
|
105
|
* be placed along the left side of the cell.
|
106
|
*
|
107
|
* The default value is 0.
|
108
|
*/
|
109
|
public int horizontalIndent = 0;
|
110
|
|
111
|
/**
|
112
|
* horizontalSpan specifies the number of column cells that the figure will
|
113
|
* take up.
|
114
|
*
|
115
|
* The default value is 1.
|
116
|
*/
|
117
|
public int horizontalSpan = 1;
|
118
|
|
119
|
/**
|
120
|
* verticalSpan specifies the number of row cells that the figure will take
|
121
|
* up.
|
122
|
*
|
123
|
* The default value is 1.
|
124
|
*/
|
125
|
public int verticalSpan = 1;
|
126
|
|
127
|
/**
|
128
|
* grabExcessHorizontalSpace specifies whether the cell will be made wide
|
129
|
* enough to fit the remaining horizontal space.
|
130
|
*
|
131
|
* The default value is false.
|
132
|
*/
|
133
|
public boolean grabExcessHorizontalSpace = false;
|
134
|
|
135
|
/**
|
136
|
* grabExcessVerticalSpace specifies whether the cell will be made tall
|
137
|
* enough to fit the remaining vertical space.
|
138
|
*
|
139
|
* The default value is false.
|
140
|
*/
|
141
|
public boolean grabExcessVerticalSpace = false;
|
142
|
|
143
|
/**
|
144
|
* Value for horizontalAlignment or verticalAlignment. Position the figure
|
145
|
* at the top or left of the cell. Not recommended. Use SWT.BEGINNING,
|
146
|
* SWT.TOP or SWT.LEFT instead.
|
147
|
*/
|
148
|
public static final int BEGINNING = SWT.BEGINNING;
|
149
|
|
150
|
/**
|
151
|
* Value for horizontalAlignment or verticalAlignment. Position the figure
|
152
|
* in the vertical or horizontal center of the cell Not recommended. Use
|
153
|
* SWT.CENTER instead.
|
154
|
*/
|
155
|
public static final int CENTER = 2;
|
156
|
|
157
|
/**
|
158
|
* Value for horizontalAlignment or verticalAlignment. Position the figure
|
159
|
* at the bottom or right of the cell Not recommended. Use SWT.END,
|
160
|
* SWT.BOTTOM or SWT.RIGHT instead.
|
161
|
*/
|
162
|
public static final int END = 3;
|
163
|
|
164
|
/**
|
165
|
* Value for horizontalAlignment or verticalAlignment. Resize the figure to
|
166
|
* fill the cell horizontally or vertically. Not recommended. Use SWT.FILL
|
167
|
* instead.
|
168
|
*/
|
169
|
public static final int FILL = SWT.FILL;
|
170
|
|
171
|
/**
|
172
|
* Style bit for <code>new GridData(int)</code>. Position the figure at the
|
173
|
* top of the cell. Not recommended. Use
|
174
|
* <code>new GridData(int, SWT.BEGINNING, boolean, boolean)</code> instead.
|
175
|
*/
|
176
|
public static final int VERTICAL_ALIGN_BEGINNING = 1 << 1;
|
177
|
|
178
|
/**
|
179
|
* Style bit for <code>new GridData(int)</code> to position the figure in
|
180
|
* the vertical center of the cell. Not recommended. Use
|
181
|
* <code>new GridData(int, SWT.CENTER, boolean, boolean)</code> instead.
|
182
|
*/
|
183
|
public static final int VERTICAL_ALIGN_CENTER = 1 << 2;
|
184
|
|
185
|
/**
|
186
|
* Style bit for <code>new GridData(int)</code> to position the figure at
|
187
|
* the bottom of the cell. Not recommended. Use
|
188
|
* <code>new GridData(int, SWT.END, boolean, boolean)</code> instead.
|
189
|
*/
|
190
|
public static final int VERTICAL_ALIGN_END = 1 << 3;
|
191
|
|
192
|
/**
|
193
|
* Style bit for <code>new GridData(int)</code> to resize the figure to fill
|
194
|
* the cell vertically. Not recommended. Use
|
195
|
* <code>new GridData(int, SWT.FILL, boolean, boolean)</code> instead
|
196
|
*/
|
197
|
public static final int VERTICAL_ALIGN_FILL = 1 << 4;
|
198
|
|
199
|
/**
|
200
|
* Style bit for <code>new GridData(int)</code> to position the figure at
|
201
|
* the left of the cell. Not recommended. Use
|
202
|
* <code>new GridData(SWT.BEGINNING, int, boolean, boolean)</code> instead.
|
203
|
*/
|
204
|
public static final int HORIZONTAL_ALIGN_BEGINNING = 1 << 5;
|
205
|
|
206
|
/**
|
207
|
* Style bit for <code>new GridData(int)</code> to position the figure in
|
208
|
* the horizontal center of the cell. Not recommended. Use
|
209
|
* <code>new GridData(SWT.CENTER, int, boolean, boolean)</code> instead.
|
210
|
*/
|
211
|
public static final int HORIZONTAL_ALIGN_CENTER = 1 << 6;
|
212
|
|
213
|
/**
|
214
|
* Style bit for <code>new GridData(int)</code> to position the figure at
|
215
|
* the right of the cell. Not recommended. Use
|
216
|
* <code>new GridData(SWT.END, int, boolean, boolean)</code> instead.
|
217
|
*/
|
218
|
public static final int HORIZONTAL_ALIGN_END = 1 << 7;
|
219
|
|
220
|
/**
|
221
|
* Style bit for <code>new GridData(int)</code> to resize the figure to fill
|
222
|
* the cell horizontally. Not recommended. Use
|
223
|
* <code>new GridData(SWT.FILL, int, boolean, boolean)</code> instead.
|
224
|
*/
|
225
|
public static final int HORIZONTAL_ALIGN_FILL = 1 << 8;
|
226
|
|
227
|
/**
|
228
|
* Style bit for <code>new GridData(int)</code> to resize the figure to fit
|
229
|
* the remaining horizontal space. Not recommended. Use
|
230
|
* <code>new GridData(int, int, true, boolean)</code> instead.
|
231
|
*/
|
232
|
public static final int GRAB_HORIZONTAL = 1 << 9;
|
233
|
|
234
|
/**
|
235
|
* Style bit for <code>new GridData(int)</code> to resize the figure to fit
|
236
|
* the remaining vertical space. Not recommended. Use
|
237
|
* <code>new GridData(int, int, boolean, true)</code> instead.
|
238
|
*/
|
239
|
public static final int GRAB_VERTICAL = 1 << 10;
|
240
|
|
241
|
/**
|
242
|
* Style bit for <code>new GridData(int)</code> to resize the figure to fill
|
243
|
* the cell vertically and to fit the remaining vertical space.
|
244
|
* FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL Not recommended. Use
|
245
|
* <code>new GridData(int, SWT.FILL, boolean, true)</code> instead.
|
246
|
*/
|
247
|
public static final int FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL;
|
248
|
|
249
|
/**
|
250
|
* Style bit for <code>new GridData(int)</code> to resize the figure to fill
|
251
|
* the cell horizontally and to fit the remaining horizontal space.
|
252
|
* FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL Not
|
253
|
* recommended. Use <code>new GridData(SWT.FILL, int, true, boolean)</code>
|
254
|
* instead.
|
255
|
*/
|
256
|
public static final int FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL
|
257
|
| GRAB_HORIZONTAL;
|
258
|
|
259
|
/**
|
260
|
* Style bit for <code>new GridData(int)</code> to resize the figure to fill
|
261
|
* the cell horizontally and vertically and to fit the remaining horizontal
|
262
|
* and vertical space. FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL Not
|
263
|
* recommended. Use
|
264
|
* <code>new GridData(SWT.FILL, SWT.FILL, true, true)</code> instead.
|
265
|
*/
|
266
|
public static final int FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL;
|
267
|
|
268
|
int cacheWidth = -1, cacheHeight = -1;
|
269
|
int[][] cache = new int[2][4];
|
270
|
int cacheIndex = -1;
|
271
|
|
272
|
/**
|
273
|
* Constructs a new instance of GridData using default values.
|
274
|
*/
|
275
|
public GridData() {
|
276
|
super();
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* Constructs a new instance based on the GridData style. This constructor
|
281
|
* is not recommended.
|
282
|
*
|
283
|
* @param style
|
284
|
* the GridData style
|
285
|
*/
|
286
|
public GridData(int style) {
|
287
|
super();
|
288
|
if ((style & VERTICAL_ALIGN_BEGINNING) != 0)
|
289
|
verticalAlignment = BEGINNING;
|
290
|
if ((style & VERTICAL_ALIGN_CENTER) != 0)
|
291
|
verticalAlignment = CENTER;
|
292
|
if ((style & VERTICAL_ALIGN_FILL) != 0)
|
293
|
verticalAlignment = FILL;
|
294
|
if ((style & VERTICAL_ALIGN_END) != 0)
|
295
|
verticalAlignment = END;
|
296
|
if ((style & HORIZONTAL_ALIGN_BEGINNING) != 0)
|
297
|
horizontalAlignment = BEGINNING;
|
298
|
if ((style & HORIZONTAL_ALIGN_CENTER) != 0)
|
299
|
horizontalAlignment = CENTER;
|
300
|
if ((style & HORIZONTAL_ALIGN_FILL) != 0)
|
301
|
horizontalAlignment = FILL;
|
302
|
if ((style & HORIZONTAL_ALIGN_END) != 0)
|
303
|
horizontalAlignment = END;
|
304
|
grabExcessHorizontalSpace = (style & GRAB_HORIZONTAL) != 0;
|
305
|
grabExcessVerticalSpace = (style & GRAB_VERTICAL) != 0;
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* Constructs a new instance of GridData according to the parameters.
|
310
|
*
|
311
|
* @param horizontalAlignment
|
312
|
* how figure will be positioned horizontally within a cell
|
313
|
* @param verticalAlignment
|
314
|
* how figure will be positioned vertically within a cell
|
315
|
* @param grabExcessHorizontalSpace
|
316
|
* whether cell will be made wide enough to fit the remaining
|
317
|
* horizontal space
|
318
|
* @param grabExcessVerticalSpace
|
319
|
* whether cell will be made high enough to fit the remaining
|
320
|
* vertical space
|
321
|
*
|
322
|
*/
|
323
|
public GridData(int horizontalAlignment, int verticalAlignment,
|
324
|
boolean grabExcessHorizontalSpace, boolean grabExcessVerticalSpace) {
|
325
|
this(horizontalAlignment, verticalAlignment, grabExcessHorizontalSpace,
|
326
|
grabExcessVerticalSpace, 1, 1);
|
327
|
}
|
328
|
|
329
|
/**
|
330
|
* Constructs a new instance of GridData according to the parameters.
|
331
|
*
|
332
|
* @param horizontalAlignment
|
333
|
* how figure will be positioned horizontally within a cell
|
334
|
* @param verticalAlignment
|
335
|
* how figure will be positioned vertically within a cell
|
336
|
* @param grabExcessHorizontalSpace
|
337
|
* whether cell will be made wide enough to fit the remaining
|
338
|
* horizontal space
|
339
|
* @param grabExcessVerticalSpace
|
340
|
* whether cell will be made high enough to fit the remaining
|
341
|
* vertical space
|
342
|
* @param horizontalSpan
|
343
|
* the number of column cells that the figure will take up
|
344
|
* @param verticalSpan
|
345
|
* the number of row cells that the figure will take up
|
346
|
*
|
347
|
*/
|
348
|
public GridData(int horizontalAlignment, int verticalAlignment,
|
349
|
boolean grabExcessHorizontalSpace, boolean grabExcessVerticalSpace,
|
350
|
int horizontalSpan, int verticalSpan) {
|
351
|
super();
|
352
|
this.horizontalAlignment = horizontalAlignment;
|
353
|
this.verticalAlignment = verticalAlignment;
|
354
|
this.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
|
355
|
this.grabExcessVerticalSpace = grabExcessVerticalSpace;
|
356
|
this.horizontalSpan = horizontalSpan;
|
357
|
this.verticalSpan = verticalSpan;
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* Constructs a new instance of GridData according to the parameters. A
|
362
|
* value of SWT.DEFAULT indicates that no minimum width or no minumum height
|
363
|
* is specified.
|
364
|
*
|
365
|
* @param width
|
366
|
* a minimum width for the column
|
367
|
* @param height
|
368
|
* a minimum height for the row
|
369
|
*
|
370
|
*/
|
371
|
public GridData(int width, int height) {
|
372
|
super();
|
373
|
this.widthHint = width;
|
374
|
this.heightHint = height;
|
375
|
}
|
376
|
|
377
|
Dimension computeSize(IFigure figure, boolean flushCache) {
|
378
|
if (cacheWidth != -1 && cacheHeight != -1) {
|
379
|
return new Dimension(cacheWidth, cacheHeight);
|
380
|
}
|
381
|
for (int i = 0; i < cacheIndex + 1; i++) {
|
382
|
if (cache[i][0] == widthHint && cache[i][1] == heightHint) {
|
383
|
cacheWidth = cache[i][2];
|
384
|
cacheHeight = cache[i][3];
|
385
|
return new Dimension(cacheWidth, cacheHeight);
|
386
|
}
|
387
|
}
|
388
|
|
389
|
Dimension size = figure.getPreferredSize(widthHint, heightHint)
|
390
|
.getCopy();
|
391
|
if (widthHint != -1)
|
392
|
size.width = widthHint;
|
393
|
if (heightHint != -1)
|
394
|
size.height = heightHint;
|
395
|
|
396
|
if (cacheIndex < cache.length - 1)
|
397
|
cacheIndex++;
|
398
|
cache[cacheIndex][0] = widthHint;
|
399
|
cache[cacheIndex][1] = heightHint;
|
400
|
cacheWidth = cache[cacheIndex][2] = size.width;
|
401
|
cacheHeight = cache[cacheIndex][3] = size.height;
|
402
|
return size;
|
403
|
}
|
404
|
|
405
|
void flushCache() {
|
406
|
cacheWidth = cacheHeight = -1;
|
407
|
cacheIndex = -1;
|
408
|
}
|
409
|
|
410
|
String getName() {
|
411
|
String string = getClass().getName();
|
412
|
int index = string.lastIndexOf('.');
|
413
|
if (index == -1)
|
414
|
return string;
|
415
|
return string.substring(index + 1, string.length());
|
416
|
}
|
417
|
|
418
|
public String toString() {
|
419
|
|
420
|
String hAlign = ""; //$NON-NLS-1$
|
421
|
switch (horizontalAlignment) {
|
422
|
case SWT.FILL:
|
423
|
hAlign = "SWT.FILL"; //$NON-NLS-1$
|
424
|
break;
|
425
|
case SWT.BEGINNING:
|
426
|
hAlign = "SWT.BEGINNING"; //$NON-NLS-1$
|
427
|
break;
|
428
|
case SWT.LEFT:
|
429
|
hAlign = "SWT.LEFT"; //$NON-NLS-1$
|
430
|
break;
|
431
|
case SWT.END:
|
432
|
hAlign = "SWT.END"; //$NON-NLS-1$
|
433
|
break;
|
434
|
case END:
|
435
|
hAlign = "GridData.END"; //$NON-NLS-1$
|
436
|
break;
|
437
|
case SWT.RIGHT:
|
438
|
hAlign = "SWT.RIGHT"; //$NON-NLS-1$
|
439
|
break;
|
440
|
case SWT.CENTER:
|
441
|
hAlign = "SWT.CENTER"; //$NON-NLS-1$
|
442
|
break;
|
443
|
case CENTER:
|
444
|
hAlign = "GridData.CENTER"; //$NON-NLS-1$
|
445
|
break;
|
446
|
default:
|
447
|
hAlign = "Undefined " + horizontalAlignment; //$NON-NLS-1$
|
448
|
break;
|
449
|
}
|
450
|
String vAlign = ""; //$NON-NLS-1$
|
451
|
switch (verticalAlignment) {
|
452
|
case SWT.FILL:
|
453
|
vAlign = "SWT.FILL"; //$NON-NLS-1$
|
454
|
break;
|
455
|
case SWT.BEGINNING:
|
456
|
vAlign = "SWT.BEGINNING"; //$NON-NLS-1$
|
457
|
break;
|
458
|
case SWT.TOP:
|
459
|
vAlign = "SWT.TOP"; //$NON-NLS-1$
|
460
|
break;
|
461
|
case SWT.END:
|
462
|
vAlign = "SWT.END"; //$NON-NLS-1$
|
463
|
break;
|
464
|
case END:
|
465
|
vAlign = "GridData.END"; //$NON-NLS-1$
|
466
|
break;
|
467
|
case SWT.BOTTOM:
|
468
|
vAlign = "SWT.BOTTOM"; //$NON-NLS-1$
|
469
|
break;
|
470
|
case SWT.CENTER:
|
471
|
vAlign = "SWT.CENTER"; //$NON-NLS-1$
|
472
|
break;
|
473
|
case CENTER:
|
474
|
vAlign = "GridData.CENTER"; //$NON-NLS-1$
|
475
|
break;
|
476
|
default:
|
477
|
vAlign = "Undefined " + verticalAlignment; //$NON-NLS-1$
|
478
|
break;
|
479
|
}
|
480
|
String string = getName() + " {"; //$NON-NLS-1$
|
481
|
string += "horizontalAlignment=" + hAlign + " "; //$NON-NLS-1$ //$NON-NLS-2$
|
482
|
if (horizontalIndent != 0)
|
483
|
string += "horizontalIndent=" + horizontalIndent + " "; //$NON-NLS-1$ //$NON-NLS-2$
|
484
|
if (horizontalSpan != 1)
|
485
|
string += "horizontalSpan=" + horizontalSpan + " "; //$NON-NLS-1$//$NON-NLS-2$
|
486
|
if (grabExcessHorizontalSpace)
|
487
|
string += "grabExcessHorizontalSpace=" + grabExcessHorizontalSpace //$NON-NLS-1$
|
488
|
+ " "; //$NON-NLS-1$
|
489
|
if (widthHint != SWT.DEFAULT)
|
490
|
string += "widthHint=" + widthHint + " "; //$NON-NLS-1$ //$NON-NLS-2$
|
491
|
string += "verticalAlignment=" + vAlign + " "; //$NON-NLS-1$ //$NON-NLS-2$
|
492
|
if (verticalSpan != 1)
|
493
|
string += "verticalSpan=" + verticalSpan + " "; //$NON-NLS-1$ //$NON-NLS-2$
|
494
|
if (grabExcessVerticalSpace)
|
495
|
string += "grabExcessVerticalSpace=" + grabExcessVerticalSpace //$NON-NLS-1$
|
496
|
+ " "; //$NON-NLS-1$
|
497
|
if (heightHint != SWT.DEFAULT)
|
498
|
string += "heightHint=" + heightHint + " "; //$NON-NLS-1$ //$NON-NLS-2$
|
499
|
string = string.trim();
|
500
|
string += "}"; //$NON-NLS-1$
|
501
|
return string;
|
502
|
|
503
|
}
|
504
|
|
505
|
}
|