1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * Copyright (c) 2011 Matthias Sohn <matthias.sohn@sap.com>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
13 * IBM Corporation - initial API and implementation
14 *******************************************************************************/
16 package org
.eclipse
.egit
.ui
.internal
;
18 import org
.eclipse
.core
.runtime
.Assert
;
19 import org
.eclipse
.jface
.dialogs
.Dialog
;
20 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.graphics
.FontMetrics
;
23 import org
.eclipse
.swt
.graphics
.GC
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.layout
.GridLayout
;
26 import org
.eclipse
.swt
.widgets
.Button
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Control
;
29 import org
.eclipse
.swt
.widgets
.Group
;
30 import org
.eclipse
.swt
.widgets
.Label
;
31 import org
.eclipse
.swt
.widgets
.Text
;
32 import org
.eclipse
.ui
.dialogs
.PreferenceLinkArea
;
33 import org
.eclipse
.ui
.preferences
.IWorkbenchPreferenceContainer
;
36 * A collection of factory methods for creating common SWT controls
38 public class SWTUtils
{
41 public static final int MARGINS_DEFAULT
= -1;
44 public static final int MARGINS_NONE
= 0;
47 public static final int MARGINS_DIALOG
= 1;
50 * Creates a preference link which will open in the specified container
57 * @return the created link
59 public static PreferenceLinkArea
createPreferenceLink(
60 IWorkbenchPreferenceContainer container
, Composite parent
,
61 String pageId
, String text
) {
62 final PreferenceLinkArea area
= new PreferenceLinkArea(parent
,
63 SWT
.NONE
, pageId
, text
, container
, null);
68 * Creates a grid data with the specified metrics
75 * @return the created grid data
77 public static GridData
createGridData(int width
, int height
, boolean hFill
,
79 return createGridData(width
, height
, hFill ? SWT
.FILL
: SWT
.BEGINNING
,
80 vFill ? SWT
.FILL
: SWT
.CENTER
, hFill
, vFill
);
84 * Creates a grid data with the specified metrics
93 * @return the created grid data
95 public static GridData
createGridData(int width
, int height
, int hAlign
,
96 int vAlign
, boolean hGrab
, boolean vGrab
) {
97 final GridData gd
= new GridData(hAlign
, vAlign
, hGrab
, vGrab
);
99 gd
.heightHint
= height
;
104 * Creates a horizontal grid data with the default metrics
106 * @return the created grid data
108 public static GridData
createHFillGridData() {
109 return createHFillGridData(1);
113 * Creates a horizontal grid data with the specified span
117 * @return the created grid data
119 public static GridData
createHFillGridData(int span
) {
120 final GridData gd
= createGridData(0, SWT
.DEFAULT
, SWT
.FILL
,
121 SWT
.CENTER
, true, false);
122 gd
.horizontalSpan
= span
;
127 * Creates a horizontal fill composite with the specified margins
132 * @return the created composite
134 public static Composite
createHFillComposite(Composite parent
, int margins
) {
135 return createHFillComposite(parent
, margins
, 1);
139 * Creates a horizontal fill composite with the specified margins and
146 * @return the created composite
148 public static Composite
createHFillComposite(Composite parent
, int margins
,
150 final Composite composite
= new Composite(parent
, SWT
.NONE
);
151 composite
.setFont(parent
.getFont());
152 composite
.setLayoutData(createHFillGridData());
153 composite
.setLayout(createGridLayout(columns
,
154 new PixelConverter(parent
), margins
));
159 * Creates a horizontal/vertical fill composite with the specified margins
164 * @return the created composite
166 public static Composite
createHVFillComposite(Composite parent
, int margins
) {
167 return createHVFillComposite(parent
, margins
, 1);
171 * Creates a horizontal/vertical fill composite with the specified margins
178 * @return the created composite
180 public static Composite
createHVFillComposite(Composite parent
,
181 int margins
, int columns
) {
182 final Composite composite
= new Composite(parent
, SWT
.NONE
);
183 composite
.setFont(parent
.getFont());
184 composite
.setLayoutData(createHVFillGridData());
185 composite
.setLayout(createGridLayout(columns
,
186 new PixelConverter(parent
), margins
));
191 * Creates a horizontal fill group with the specified text and margins
196 * @return the created group
198 public static Group
createHFillGroup(Composite parent
, String text
,
200 return createHFillGroup(parent
, text
, margins
, 1);
204 * Creates a horizontal fill group with the specified text, margins and rows
211 * @return the created group
213 public static Group
createHFillGroup(Composite parent
, String text
,
214 int margins
, int rows
) {
215 final Group group
= new Group(parent
, SWT
.NONE
);
216 group
.setFont(parent
.getFont());
217 group
.setLayoutData(createHFillGridData());
220 group
.setLayout(createGridLayout(rows
, new PixelConverter(parent
),
226 * Creates a horizontal/vertical fill group with the specified text and
233 * @return the created group
235 public static Group
createHVFillGroup(Composite parent
, String text
,
237 return createHVFillGroup(parent
, text
, margins
, 1);
241 * Creates a horizontal/vertical fill group with the specified text, margins
249 * @return the created group
251 public static Group
createHVFillGroup(Composite parent
, String text
,
252 int margins
, int rows
) {
253 final Group group
= new Group(parent
, SWT
.NONE
);
254 group
.setFont(parent
.getFont());
255 group
.setLayoutData(createHVFillGridData());
258 group
.setLayout(createGridLayout(rows
, new PixelConverter(parent
),
264 * Creates a horizontal/vertical fill grid data with the default metrics
266 * @return the created grid data
268 public static GridData
createHVFillGridData() {
269 return createHVFillGridData(1);
273 * Creates a horizontal/vertical fill grid data with the specified span
277 * @return the created grid data
279 public static GridData
createHVFillGridData(int span
) {
280 final GridData gd
= createGridData(0, 0, true, true);
281 gd
.horizontalSpan
= span
;
286 * Creates a grid layout with the specified number of columns and the
290 * the number of columns
292 * the pixel converter
294 * one of <code>MARGINS_DEFAULT</code>, <code>MARGINS_NONE</code>
295 * or <code>MARGINS_DIALOG</code>.
297 * @return the created grid layout
299 public static GridLayout
createGridLayout(int numColumns
,
300 PixelConverter converter
, int margins
) {
301 Assert
.isTrue(margins
== MARGINS_DEFAULT
|| margins
== MARGINS_NONE
302 || margins
== MARGINS_DIALOG
);
304 final GridLayout layout
= new GridLayout(numColumns
, false);
305 layout
.horizontalSpacing
= converter
306 .convertHorizontalDLUsToPixels(IDialogConstants
.HORIZONTAL_SPACING
);
307 layout
.verticalSpacing
= converter
308 .convertVerticalDLUsToPixels(IDialogConstants
.VERTICAL_SPACING
);
312 layout
.marginLeft
= layout
.marginRight
= 0;
313 layout
.marginTop
= layout
.marginBottom
= 0;
316 layout
.marginLeft
= layout
.marginRight
= converter
317 .convertHorizontalDLUsToPixels(IDialogConstants
.HORIZONTAL_MARGIN
);
318 layout
.marginTop
= layout
.marginBottom
= converter
319 .convertVerticalDLUsToPixels(IDialogConstants
.VERTICAL_MARGIN
);
321 case MARGINS_DEFAULT
:
322 layout
.marginLeft
= layout
.marginRight
= layout
.marginWidth
;
323 layout
.marginTop
= layout
.marginBottom
= layout
.marginHeight
;
328 layout
.marginWidth
= layout
.marginHeight
= 0;
333 * Creates a label with the specified message
338 * @return the created label
340 public static Label
createLabel(Composite parent
, String message
) {
341 return createLabel(parent
, message
, 1);
345 * Creates a label with the specified message and span
351 * @return the created label
353 public static Label
createLabel(Composite parent
, String message
, int span
) {
354 final Label label
= new Label(parent
, SWT
.WRAP
);
356 label
.setText(message
);
357 label
.setLayoutData(createHFillGridData(span
));
362 * Creates a check box with the specified message
367 * @return the created check box
369 public static Button
createCheckBox(Composite parent
, String message
) {
370 return createCheckBox(parent
, message
, 1);
374 * Creates a check box with the specified message and span
380 * @return the created check box
382 public static Button
createCheckBox(Composite parent
, String message
,
384 final Button button
= new Button(parent
, SWT
.CHECK
);
385 button
.setText(message
);
386 button
.setLayoutData(createHFillGridData(span
));
391 * Creates a radio button with the specified message
396 * @return the created radio button
398 public static Button
createRadioButton(Composite parent
, String message
) {
399 return createRadioButton(parent
, message
, 1);
403 * Creates a radio button with the specified message and span
409 * @return the created radio button
411 public static Button
createRadioButton(Composite parent
, String message
,
413 final Button button
= new Button(parent
, SWT
.RADIO
);
414 button
.setText(message
);
415 button
.setLayoutData(createHFillGridData(span
));
420 * Creates a text control
424 * @return the created text control
426 public static Text
createText(Composite parent
) {
427 return createText(parent
, 1);
431 * Creates a text control with the specified span
436 * @return the created text control
438 public static Text
createText(Composite parent
, int span
) {
439 final Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.BORDER
);
440 text
.setLayoutData(createHFillGridData(span
));
445 * Creates a place holder with the specified height and span
448 * @param heightInChars
451 * @return the created place holder
453 public static Control
createPlaceholder(Composite parent
,
454 int heightInChars
, int span
) {
455 Assert
.isTrue(heightInChars
> 0);
456 final Control placeHolder
= new Composite(parent
, SWT
.NONE
);
457 final GridData gd
= new GridData(SWT
.BEGINNING
, SWT
.TOP
, false, false);
458 gd
.heightHint
= new PixelConverter(parent
)
459 .convertHeightInCharsToPixels(heightInChars
);
460 gd
.horizontalSpan
= span
;
461 placeHolder
.setLayoutData(gd
);
466 * Creates a place holder with the specified height
469 * @param heightInChars
470 * @return the created place holder
472 public static Control
createPlaceholder(Composite parent
, int heightInChars
) {
473 return createPlaceholder(parent
, heightInChars
, 1);
477 * Creates a pixel converter
481 * @return the created pixel converter
483 public static PixelConverter
createDialogPixelConverter(Control control
) {
484 Dialog
.applyDialogFont(control
);
485 return new PixelConverter(control
);
489 * Calculates the size of the specified controls, using the specified
495 * @return the size of the control(s)
497 public static int calculateControlSize(PixelConverter converter
,
498 Control
[] controls
) {
499 return calculateControlSize(converter
, controls
, 0, controls
.length
- 1);
503 * Calculates the size of the specified subset of controls, using the
504 * specified converter
511 * @return the created control
513 public static int calculateControlSize(PixelConverter converter
,
514 Control
[] controls
, int start
, int end
) {
515 int minimum
= converter
516 .convertHorizontalDLUsToPixels(IDialogConstants
.BUTTON_WIDTH
);
517 for (int i
= start
; i
<= end
; i
++) {
518 final int length
= controls
[i
]
519 .computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
;
520 if (minimum
< length
)
527 * Equalizes the specified controls using the specified converter
532 public static void equalizeControls(PixelConverter converter
,
533 Control
[] controls
) {
534 equalizeControls(converter
, controls
, 0, controls
.length
- 1);
538 * Equalizes the specified subset of controls using the specified converter
545 public static void equalizeControls(PixelConverter converter
,
546 Control
[] controls
, int start
, int end
) {
547 final int size
= calculateControlSize(converter
, controls
, start
, end
);
548 for (int i
= start
; i
<= end
; i
++) {
549 final Control button
= controls
[i
];
550 if (button
.getLayoutData() instanceof GridData
) {
551 ((GridData
) button
.getLayoutData()).widthHint
= size
;
557 * Gets the width of the longest string in <code>strings</code>, using the
558 * specified pixel converter
563 * @return the width of the longest string
565 public static int getWidthInCharsForLongest(PixelConverter converter
,
568 for (int i
= 0; i
< strings
.length
; i
++) {
569 final int length
= converter
.convertWidthInCharsToPixels(strings
[i
]
571 if (minimum
< length
)
577 private static class PixelConverter
{
579 private final FontMetrics fFontMetrics
;
581 public PixelConverter(Control control
) {
582 GC gc
= new GC(control
);
584 gc
.setFont(control
.getFont());
585 fFontMetrics
= gc
.getFontMetrics();
591 public int convertHeightInCharsToPixels(int chars
) {
592 return Dialog
.convertHeightInCharsToPixels(fFontMetrics
, chars
);
595 public int convertHorizontalDLUsToPixels(int dlus
) {
596 return Dialog
.convertHorizontalDLUsToPixels(fFontMetrics
, dlus
);
599 public int convertVerticalDLUsToPixels(int dlus
) {
600 return Dialog
.convertVerticalDLUsToPixels(fFontMetrics
, dlus
);
603 public int convertWidthInCharsToPixels(int chars
) {
604 return Dialog
.convertWidthInCharsToPixels(fFontMetrics
, chars
);