Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 227534b4979cede7407cac1706b9c19d26e52711 (plain) (blame)
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * Copyright (c) 2011 Matthias Sohn <matthias.sohn@sap.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *	 IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.egit.ui.internal;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PreferenceLinkArea;
import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;

/**
 * A collection of factory methods for creating common SWT controls
 */
public class SWTUtils {

	/** */
	public static final int MARGINS_DEFAULT = -1;

	/** */
	public static final int MARGINS_NONE = 0;

	/** */
	public static final int MARGINS_DIALOG = 1;

	/**
	 * Creates a preference link which will open in the specified container
	 *
	 * @param container
	 * @param parent
	 * @param pageId
	 * @param text
	 *
	 * @return the created link
	 */
	public static PreferenceLinkArea createPreferenceLink(
			IWorkbenchPreferenceContainer container, Composite parent,
			String pageId, String text) {
		final PreferenceLinkArea area = new PreferenceLinkArea(parent,
				SWT.NONE, pageId, text, container, null);
		return area;
	}

	/**
	 * Creates a grid data with the specified metrics
	 *
	 * @param width
	 * @param height
	 * @param hFill
	 * @param vFill
	 *
	 * @return the created grid data
	 */
	public static GridData createGridData(int width, int height, boolean hFill,
			boolean vFill) {
		return createGridData(width, height, hFill ? SWT.FILL : SWT.BEGINNING,
				vFill ? SWT.FILL : SWT.CENTER, hFill, vFill);
	}

	/**
	 * Creates a grid data with the specified metrics
	 *
	 * @param width
	 * @param height
	 * @param hAlign
	 * @param vAlign
	 * @param hGrab
	 * @param vGrab
	 *
	 * @return the created grid data
	 */
	public static GridData createGridData(int width, int height, int hAlign,
			int vAlign, boolean hGrab, boolean vGrab) {
		final GridData gd = new GridData(hAlign, vAlign, hGrab, vGrab);
		gd.widthHint = width;
		gd.heightHint = height;
		return gd;
	}

	/**
	 * Creates a horizontal grid data with the default metrics
	 *
	 * @return the created grid data
	 */
	public static GridData createHFillGridData() {
		return createHFillGridData(1);
	}

	/**
	 * Creates a horizontal grid data with the specified span
	 *
	 * @param span
	 *
	 * @return the created grid data
	 */
	public static GridData createHFillGridData(int span) {
		final GridData gd = createGridData(0, SWT.DEFAULT, SWT.FILL,
				SWT.CENTER, true, false);
		gd.horizontalSpan = span;
		return gd;
	}

	/**
	 * Creates a horizontal fill composite with the specified margins
	 *
	 * @param parent
	 * @param margins
	 *
	 * @return the created composite
	 */
	public static Composite createHFillComposite(Composite parent, int margins) {
		return createHFillComposite(parent, margins, 1);
	}

	/**
	 * Creates a horizontal fill composite with the specified margins and
	 * columns
	 *
	 * @param parent
	 * @param margins
	 * @param columns
	 *
	 * @return the created composite
	 */
	public static Composite createHFillComposite(Composite parent, int margins,
			int columns) {
		final Composite composite = new Composite(parent, SWT.NONE);
		composite.setFont(parent.getFont());
		composite.setLayoutData(createHFillGridData());
		composite.setLayout(createGridLayout(columns,
				new PixelConverter(parent), margins));
		return composite;
	}

	/**
	 * Creates a horizontal/vertical fill composite with the specified margins
	 *
	 * @param parent
	 * @param margins
	 *
	 * @return the created composite
	 */
	public static Composite createHVFillComposite(Composite parent, int margins) {
		return createHVFillComposite(parent, margins, 1);
	}

	/**
	 * Creates a horizontal/vertical fill composite with the specified margins
	 * and columns
	 *
	 * @param parent
	 * @param margins
	 * @param columns
	 *
	 * @return the created composite
	 */
	public static Composite createHVFillComposite(Composite parent,
			int margins, int columns) {
		final Composite composite = new Composite(parent, SWT.NONE);
		composite.setFont(parent.getFont());
		composite.setLayoutData(createHVFillGridData());
		composite.setLayout(createGridLayout(columns,
				new PixelConverter(parent), margins));
		return composite;
	}

	/**
	 * Creates a horizontal fill group with the specified text and margins
	 *
	 * @param parent
	 * @param text
	 * @param margins
	 * @return the created group
	 */
	public static Group createHFillGroup(Composite parent, String text,
			int margins) {
		return createHFillGroup(parent, text, margins, 1);
	}

	/**
	 * Creates a horizontal fill group with the specified text, margins and rows
	 *
	 * @param parent
	 * @param text
	 * @param margins
	 * @param rows
	 *
	 * @return the created group
	 */
	public static Group createHFillGroup(Composite parent, String text,
			int margins, int rows) {
		final Group group = new Group(parent, SWT.NONE);
		group.setFont(parent.getFont());
		group.setLayoutData(createHFillGridData());
		if (text != null)
			group.setText(text);
		group.setLayout(createGridLayout(rows, new PixelConverter(parent),
				margins));
		return group;
	}

	/**
	 * Creates a horizontal/vertical fill group with the specified text and
	 * margins
	 *
	 * @param parent
	 * @param text
	 * @param margins
	 *
	 * @return the created group
	 */
	public static Group createHVFillGroup(Composite parent, String text,
			int margins) {
		return createHVFillGroup(parent, text, margins, 1);
	}

	/**
	 * Creates a horizontal/vertical fill group with the specified text, margins
	 * and rows
	 *
	 * @param parent
	 * @param text
	 * @param margins
	 * @param rows
	 *
	 * @return the created group
	 */
	public static Group createHVFillGroup(Composite parent, String text,
			int margins, int rows) {
		final Group group = new Group(parent, SWT.NONE);
		group.setFont(parent.getFont());
		group.setLayoutData(createHVFillGridData());
		if (text != null)
			group.setText(text);
		group.setLayout(createGridLayout(rows, new PixelConverter(parent),
				margins));
		return group;
	}

	/**
	 * Creates a horizontal/vertical fill grid data with the default metrics
	 *
	 * @return the created grid data
	 */
	public static GridData createHVFillGridData() {
		return createHVFillGridData(1);
	}

	/**
	 * Creates a horizontal/vertical fill grid data with the specified span
	 *
	 * @param span
	 *
	 * @return the created grid data
	 */
	public static GridData createHVFillGridData(int span) {
		final GridData gd = createGridData(0, 0, true, true);
		gd.horizontalSpan = span;
		return gd;
	}

	/**
	 * Creates a grid layout with the specified number of columns and the
	 * standard spacings.
	 *
	 * @param numColumns
	 *            the number of columns
	 * @param converter
	 *            the pixel converter
	 * @param margins
	 *            one of <code>MARGINS_DEFAULT</code>, <code>MARGINS_NONE</code>
	 *            or <code>MARGINS_DIALOG</code>.
	 *
	 * @return the created grid layout
	 */
	public static GridLayout createGridLayout(int numColumns,
			PixelConverter converter, int margins) {
		Assert.isTrue(margins == MARGINS_DEFAULT || margins == MARGINS_NONE
				|| margins == MARGINS_DIALOG);

		final GridLayout layout = new GridLayout(numColumns, false);
		layout.horizontalSpacing = converter
				.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		layout.verticalSpacing = converter
				.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);

		switch (margins) {
		case MARGINS_NONE:
			layout.marginLeft = layout.marginRight = 0;
			layout.marginTop = layout.marginBottom = 0;
			break;
		case MARGINS_DIALOG:
			layout.marginLeft = layout.marginRight = converter
					.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
			layout.marginTop = layout.marginBottom = converter
					.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
			break;
		case MARGINS_DEFAULT:
			layout.marginLeft = layout.marginRight = layout.marginWidth;
			layout.marginTop = layout.marginBottom = layout.marginHeight;
			break;
		default:
			break;
		}
		layout.marginWidth = layout.marginHeight = 0;
		return layout;
	}

	/**
	 * Creates a label with the specified message
	 *
	 * @param parent
	 * @param message
	 *
	 * @return the created label
	 */
	public static Label createLabel(Composite parent, String message) {
		return createLabel(parent, message, 1);
	}

	/**
	 * Creates a label with the specified message and span
	 *
	 * @param parent
	 * @param message
	 * @param span
	 *
	 * @return the created label
	 */
	public static Label createLabel(Composite parent, String message, int span) {
		final Label label = new Label(parent, SWT.WRAP);
		if (message != null)
			label.setText(message);
		label.setLayoutData(createHFillGridData(span));
		return label;
	}

	/**
	 * Creates a check box with the specified message
	 *
	 * @param parent
	 * @param message
	 *
	 * @return the created check box
	 */
	public static Button createCheckBox(Composite parent, String message) {
		return createCheckBox(parent, message, 1);
	}

	/**
	 * Creates a check box with the specified message and span
	 *
	 * @param parent
	 * @param message
	 * @param span
	 *
	 * @return the created check box
	 */
	public static Button createCheckBox(Composite parent, String message,
			int span) {
		final Button button = new Button(parent, SWT.CHECK);
		button.setText(message);
		button.setLayoutData(createHFillGridData(span));
		return button;
	}

	/**
	 * Creates a radio button with the specified message
	 *
	 * @param parent
	 * @param message
	 *
	 * @return the created radio button
	 */
	public static Button createRadioButton(Composite parent, String message) {
		return createRadioButton(parent, message, 1);
	}

	/**
	 * Creates a radio button with the specified message and span
	 *
	 * @param parent
	 * @param message
	 * @param span
	 *
	 * @return the created radio button
	 */
	public static Button createRadioButton(Composite parent, String message,
			int span) {
		final Button button = new Button(parent, SWT.RADIO);
		button.setText(message);
		button.setLayoutData(createHFillGridData(span));
		return button;
	}

	/**
	 * Creates a text control
	 *
	 * @param parent
	 *
	 * @return the created text control
	 */
	public static Text createText(Composite parent) {
		return createText(parent, 1);
	}

	/**
	 * Creates a text control with the specified span
	 *
	 * @param parent
	 * @param span
	 *
	 * @return the created text control
	 */
	public static Text createText(Composite parent, int span) {
		final Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
		text.setLayoutData(createHFillGridData(span));
		return text;
	}

	/**
	 * Creates a place holder with the specified height and span
	 *
	 * @param parent
	 * @param heightInChars
	 * @param span
	 *
	 * @return the created place holder
	 */
	public static Control createPlaceholder(Composite parent,
			int heightInChars, int span) {
		Assert.isTrue(heightInChars > 0);
		final Control placeHolder = new Composite(parent, SWT.NONE);
		final GridData gd = new GridData(SWT.BEGINNING, SWT.TOP, false, false);
		gd.heightHint = new PixelConverter(parent)
				.convertHeightInCharsToPixels(heightInChars);
		gd.horizontalSpan = span;
		placeHolder.setLayoutData(gd);
		return placeHolder;
	}

	/**
	 * Creates a place holder with the specified height
	 *
	 * @param parent
	 * @param heightInChars
	 * @return the created place holder
	 */
	public static Control createPlaceholder(Composite parent, int heightInChars) {
		return createPlaceholder(parent, heightInChars, 1);
	}

	/**
	 * Creates a pixel converter
	 *
	 * @param control
	 *
	 * @return the created pixel converter
	 */
	public static PixelConverter createDialogPixelConverter(Control control) {
		Dialog.applyDialogFont(control);
		return new PixelConverter(control);
	}

	/**
	 * Calculates the size of the specified controls, using the specified
	 * converter
	 *
	 * @param converter
	 * @param controls
	 *
	 * @return the size of the control(s)
	 */
	public static int calculateControlSize(PixelConverter converter,
			Control[] controls) {
		return calculateControlSize(converter, controls, 0, controls.length - 1);
	}

	/**
	 * Calculates the size of the specified subset of controls, using the
	 * specified converter
	 *
	 * @param converter
	 * @param controls
	 * @param start
	 * @param end
	 *
	 * @return the created control
	 */
	public static int calculateControlSize(PixelConverter converter,
			Control[] controls, int start, int end) {
		int minimum = converter
				.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
		for (int i = start; i <= end; i++) {
			final int length = controls[i]
					.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
			if (minimum < length)
				minimum = length;
		}
		return minimum;
	}

	/**
	 * Equalizes the specified controls using the specified converter
	 *
	 * @param converter
	 * @param controls
	 */
	public static void equalizeControls(PixelConverter converter,
			Control[] controls) {
		equalizeControls(converter, controls, 0, controls.length - 1);
	}

	/**
	 * Equalizes the specified subset of controls using the specified converter
	 *
	 * @param converter
	 * @param controls
	 * @param start
	 * @param end
	 */
	public static void equalizeControls(PixelConverter converter,
			Control[] controls, int start, int end) {
		final int size = calculateControlSize(converter, controls, start, end);
		for (int i = start; i <= end; i++) {
			final Control button = controls[i];
			if (button.getLayoutData() instanceof GridData) {
				((GridData) button.getLayoutData()).widthHint = size;
			}
		}
	}

	/**
	 * Gets the width of the longest string in <code>strings</code>, using the
	 * specified pixel converter
	 *
	 * @param converter
	 * @param strings
	 *
	 * @return the width of the longest string
	 */
	public static int getWidthInCharsForLongest(PixelConverter converter,
			String[] strings) {
		int minimum = 0;
		for (int i = 0; i < strings.length; i++) {
			final int length = converter.convertWidthInCharsToPixels(strings[i]
					.length());
			if (minimum < length)
				minimum = length;
		}
		return minimum;
	}

	private static class PixelConverter {

		private final FontMetrics fFontMetrics;

		public PixelConverter(Control control) {
			GC gc = new GC(control);
			try {
				gc.setFont(control.getFont());
				fFontMetrics = gc.getFontMetrics();
			} finally {
				gc.dispose();
			}
		}

		public int convertHeightInCharsToPixels(int chars) {
			return Dialog.convertHeightInCharsToPixels(fFontMetrics, chars);
		}

		public int convertHorizontalDLUsToPixels(int dlus) {
			return Dialog.convertHorizontalDLUsToPixels(fFontMetrics, dlus);
		}

		public int convertVerticalDLUsToPixels(int dlus) {
			return Dialog.convertVerticalDLUsToPixels(fFontMetrics, dlus);
		}

		public int convertWidthInCharsToPixels(int chars) {
			return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars);
		}
	}

}

Back to the top