summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/utils/ui/GridDataFactory.java
blob: 4ccda404c47f0bbc2e05ec87a4d253c17b12375c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*******************************************************************************
 * Copyright (c) 2005, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.utils.ui;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Control;

/**
 * This class provides a convienient shorthand for creating and initializing GridData. This offers
 * several benefits over creating GridData normal way:
 * 
 * <ul>
 * <li>The same factory can be used many times to create several GridData instances</li>
 * <li>The setters on GridDataFactory all return "this", allowing them to be chained</li>
 * <li>GridDataFactory uses vector setters (it accepts Points), making it easy to set X and Y values
 * together</li>
 * </ul>
 * 
 * <p>
 * GridDataFactory instances are created using one of the static methods on this class.
 * </p>
 * 
 * <p>
 * Example usage:
 * </p>
 * <code>
 * 
 * ////////////////////////////////////////////////////////////
 * // Example 1: Typical grid data for a non-wrapping label
 * 
 *     // GridDataFactory version
 *     GridDataFactory.fillDefaults().applyTo(myLabel);
 * 
 *     // Equivalent SWT version
 *     GridData labelData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
 *     myLabel.setLayoutData(labelData);
 * 
 * ///////////////////////////////////////////////////////////
 * // Example 2: Typical grid data for a wrapping label
 * 
 *     // GridDataFactory version
 *     GridDataFactory.fillDefaults()
 *          .align(SWT.FILL, SWT.CENTER)
 *    	    .hint(150, SWT.DEFAULT)
 *    	    .grab(true, false)
 *          .applyTo(wrappingLabel);
 *      
 *     // Equivalent SWT version
 *     GridData wrappingLabelData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
 *     wrappingLabelData.minimumWidth = 1;
 *     wrappingLabelData.widthHint = 150;
 *     wrappingLabel.setLayoutData(wrappingLabelData);
 * 
 * //////////////////////////////////////////////////////////////
 * // Example 3: Typical grid data for a scrollable control (a list box, tree, table, etc.)
 * 
 *     // GridDataFactory version
 *     GridDataFactory.fillDefaults().grab(true, true).hint(150, 150).applyTo(listBox);
 * 
 *     // Equivalent SWT version
 *     GridData listBoxData = new GridData(GridData.FILL_BOTH);
 *     listBoxData.widthHint = 150;
 *     listBoxData.heightHint = 150;
 *     listBoxData.minimumWidth = 1;
 *     listBoxData.minimumHeight = 1;
 *     listBox.setLayoutData(listBoxData);
 * 
 * /////////////////////////////////////////////////////////////
 * // Example 4: Typical grid data for a button
 * 
 *     // GridDataFactory version
 *     Point preferredSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
 *     Point hint = Geometry.max(LayoutConstants.getMinButtonSize(), preferredSize);
 *     GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).hint(hint).applyTo(button);
 * 
 *     // Equivalent SWT version
 *     Point preferredSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
 *     Point hint = Geometry.max(LayoutConstants.getMinButtonSize(), preferredSize);
 *     GridData buttonData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
 *     buttonData.widthHint = hint.x;
 *     buttonData.heightHint = hint.y;
 *     button.setLayoutData(buttonData); 
 * </code>
 * 
 * <p>
 * IMPORTANT: WHEN ASSIGNING LAYOUT DATA TO A CONTROL, BE SURE TO USE
 * gridDataFactory.applyTo(control) AND NEVER control.setLayoutData(gridDataFactory).
 * </p>
 * 
 * @since 3.2
 */
public final class GridDataFactory {
  private final Control m_control;
  private final PixelConverter m_pixelConverter;
  private final GridData m_data;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  private GridDataFactory(Control control, GridData gridData) {
    m_control = control;
    m_pixelConverter = new PixelConverter(m_control);
    //
    m_data = gridData;
    if (m_control.getLayoutData() != m_data) {
      m_control.setLayoutData(m_data);
    }
  }

  /**
   * Creates new {@link GridDataFactory} with new {@link GridData}.
   */
  public static GridDataFactory create(Control control) {
    return new GridDataFactory(control, new GridData());
  }

  /**
   * Creates new {@link GridDataFactory} for modifying {@link GridData} already installed in
   * control.
   */
  public static GridDataFactory modify(Control control) {
    GridData gridData;
    {
      Object existingLayoutData = control.getLayoutData();
      if (existingLayoutData instanceof GridData) {
        gridData = (GridData) existingLayoutData;
      } else {
        gridData = new GridData();
      }
    }
    return new GridDataFactory(control, gridData);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Span
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Sets the GridData span. The span controls how many cells are filled by the control.
   * 
   * @param hSpan
   *          number of columns spanned by the control
   * @param vSpan
   *          number of rows spanned by the control
   * @return this
   */
  public GridDataFactory span(int hSpan, int vSpan) {
    m_data.horizontalSpan = hSpan;
    m_data.verticalSpan = vSpan;
    return this;
  }

  /**
   * Sets the GridData span. The span controls how many cells are filled by the control.
   * 
   * @param hSpan
   *          number of columns spanned by the control
   * @return this
   */
  public GridDataFactory spanH(int hSpan) {
    m_data.horizontalSpan = hSpan;
    return this;
  }

  /**
   * Sets the GridData span. The span controls how many cells are filled by the control.
   * 
   * @param vSpan
   *          number of rows spanned by the control
   * @return this
   */
  public GridDataFactory spanV(int vSpan) {
    m_data.verticalSpan = vSpan;
    return this;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Hint
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Sets the width and height hints. The width and height hints override the control's preferred
   * size. If either hint is set to SWT.DEFAULT, the control's preferred size is used.
   * 
   * @param xHint
   *          horizontal hint (pixels), or SWT.DEFAULT to use the control's preferred size
   * @param yHint
   *          vertical hint (pixels), or SWT.DEFAULT to use the control's preferred size
   * @return this
   */
  public GridDataFactory hint(int xHint, int yHint) {
    m_data.widthHint = xHint;
    m_data.heightHint = yHint;
    return this;
  }

  /**
   * Sets hint in chars.
   */
  public GridDataFactory hintC(int xHintInChars, int yHintInChars) {
    hintHC(xHintInChars);
    hintVC(yHintInChars);
    return this;
  }

  /**
   * Sets the width hint.
   * 
   * @return this
   */
  public GridDataFactory hintH(int xHint) {
    m_data.widthHint = xHint;
    return this;
  }

  /**
   * Sets the width hint to the minimum of current hint and given <code>otherHint</code>.
   * 
   * @return this
   */
  public GridDataFactory hintHMin(int otherHint) {
    m_data.widthHint = Math.min(m_data.widthHint, otherHint);
    return this;
  }

  /**
   * Sets the width hint in chars.
   * 
   * @return this
   */
  public GridDataFactory hintHC(int hintInChars) {
    return hintH(m_pixelConverter.convertWidthInCharsToPixels(hintInChars));
  }

  /**
   * Sets the width hint.
   * 
   * @return this
   */
  public GridDataFactory hintHU(int hintInDLU) {
    return hintH(m_pixelConverter.convertHorizontalDLUsToPixels(hintInDLU));
  }

  /**
   * Sets the height hint.
   * 
   * @return this
   */
  public GridDataFactory hintV(int yHint) {
    m_data.heightHint = yHint;
    return this;
  }

  /**
   * Sets the height hint in chars.
   * 
   * @return this
   */
  public GridDataFactory hintVC(int hintInChars) {
    return hintV(m_pixelConverter.convertHeightInCharsToPixels(hintInChars));
  }

  /**
   * Increments horizontal hint on given value.
   * 
   * @return this
   */
  public GridDataFactory hintHAdd(int increment) {
    return hintV(m_data.widthHint + increment);
  }

  /**
   * Increments vertical hint on given value.
   * 
   * @return this
   */
  public GridDataFactory hintVAdd(int increment) {
    return hintV(m_data.heightHint + increment);
  }

  /**
   * Sets the width and height hints. The width and height hints override the control's preferred
   * size. If either hint is set to SWT.DEFAULT, the control's preferred size is used.
   * 
   * @param hint
   *          size (pixels) to be used instead of the control's preferred size. If the x or y values
   *          are set to SWT.DEFAULT, the control's computeSize() method will be used to obtain that
   *          dimension of the preferred size.
   * @return this
   */
  public GridDataFactory hint(Point hint) {
    m_data.widthHint = hint.x;
    m_data.heightHint = hint.y;
    return this;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Minimum size
  //
  ////////////////////////////////////////////////////////////////////////////
  public GridDataFactory minH(int minimumWidth) {
    m_data.minimumWidth = minimumWidth;
    return this;
  }

  public GridDataFactory minHC(int widthInChars) {
    return minH(m_pixelConverter.convertWidthInCharsToPixels(widthInChars));
  }

  public GridDataFactory minV(int minimumHeight) {
    m_data.minimumHeight = minimumHeight;
    return this;
  }

  public GridDataFactory minVC(int heightInChars) {
    return minV(m_pixelConverter.convertHeightInCharsToPixels(heightInChars));
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Alignment
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Sets the alignment of the control within its cell.
   * 
   * @param hAlign
   *          horizontal alignment. One of SWT.BEGINNING, SWT.CENTER, SWT.END, or SWT.FILL.
   * @param vAlign
   *          vertical alignment. One of SWT.BEGINNING, SWT.CENTER, SWT.END, or SWT.FILL.
   * @return this
   */
  public GridDataFactory align(int hAlign, int vAlign) {
    m_data.horizontalAlignment = hAlign;
    m_data.verticalAlignment = vAlign;
    return this;
  }

  /**
   * Sets the horizontal and vertical alignment to GridData.FILL.
   */
  public GridDataFactory fill() {
    return align(GridData.FILL, GridData.FILL);
  }

  /**
   * Sets the horizontal alignment of the control within its cell.
   * 
   * @param hAlign
   *          horizontal alignment. One of SWT.BEGINNING, SWT.CENTER, SWT.END, or SWT.FILL.
   * @return this
   */
  public GridDataFactory alignH(int hAlign) {
    m_data.horizontalAlignment = hAlign;
    return this;
  }

  /**
   * Sets the horizontal alignment of the control to GridData.BEGINNING
   * 
   * @return this
   */
  public GridDataFactory alignHL() {
    return alignH(GridData.BEGINNING);
  }

  /**
   * Sets the horizontal alignment of the control to GridData.CENTER
   * 
   * @return this
   */
  public GridDataFactory alignHC() {
    return alignH(GridData.CENTER);
  }

  /**
   * Sets the horizontal alignment of the control to GridData.FILL
   * 
   * @return this
   */
  public GridDataFactory alignHF() {
    return alignH(GridData.FILL);
  }

  /**
   * Sets the horizontal alignment of the control to GridData.FILL
   * 
   * @return this
   */
  public GridDataFactory fillH() {
    return alignHF();
  }

  /**
   * Sets the horizontal alignment of the control to GridData.END
   * 
   * @return this
   */
  public GridDataFactory alignHR() {
    return alignH(GridData.END);
  }

  /**
   * Sets the vertical alignment of the control within its cell.
   * 
   * @param vAlign
   *          vertical alignment. One of SWT.BEGINNING, SWT.CENTER, SWT.END, or SWT.FILL.
   * @return this
   */
  public GridDataFactory alignV(int vAlign) {
    m_data.verticalAlignment = vAlign;
    return this;
  }

  /**
   * Sets the vertical alignment of the control to GridData.BEGINNING
   * 
   * @return this
   */
  public GridDataFactory alignVT() {
    return alignV(GridData.BEGINNING);
  }

  /**
   * Sets the vertical alignment of the control to GridData.CENTER
   * 
   * @return this
   */
  public GridDataFactory alignVM() {
    return alignV(GridData.CENTER);
  }

  /**
   * Sets the vertical alignment of the control to GridData.FILL
   * 
   * @return this
   */
  public GridDataFactory alignVF() {
    return alignV(GridData.FILL);
  }

  /**
   * Sets the vertical alignment of the control to GridData.FILL
   * 
   * @return this
   */
  public GridDataFactory fillV() {
    return alignVF();
  }

  /**
   * Sets the vertical alignment of the control to GridData.END
   * 
   * @return this
   */
  public GridDataFactory alignVB() {
    return alignV(GridData.END);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Indent
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Sets the indent of the control within the cell in pixels.
   */
  public GridDataFactory indentH(int hIndent) {
    m_data.horizontalIndent = hIndent;
    return this;
  }

  /**
   * Sets the indent of the control within the cell in characters.
   */
  public GridDataFactory indentHC(int hIndent) {
    m_data.horizontalIndent = m_pixelConverter.convertWidthInCharsToPixels(hIndent);
    return this;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Grab
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Determines whether extra horizontal or vertical space should be allocated to this control's
   * column when the layout resizes. If any control in the column is set to grab horizontal then the
   * whole column will grab horizontal space. If any control in the row is set to grab vertical then
   * the whole row will grab vertical space.
   * 
   * @param horizontal
   *          true if the control's column should grow horizontally
   * @param vertical
   *          true if the control's row should grow vertically
   * @return this
   */
  public GridDataFactory grab(boolean horizontal, boolean vertical) {
    m_data.grabExcessHorizontalSpace = horizontal;
    m_data.grabExcessVerticalSpace = vertical;
    return this;
  }

  public GridDataFactory grabH() {
    m_data.grabExcessHorizontalSpace = true;
    return this;
  }

  public GridDataFactory grabV() {
    m_data.grabExcessVerticalSpace = true;
    return this;
  }

  public GridDataFactory grab() {
    return grab(true, true);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Exclude
  //
  ////////////////////////////////////////////////////////////////////////////
  public GridDataFactory exclude(boolean value) {
    m_data.exclude = value;
    return this;
  }
}