1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.egit
.ui
.internal
;
14 import org
.eclipse
.core
.runtime
.Assert
;
15 import org
.eclipse
.jface
.dialogs
.Dialog
;
16 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.graphics
.FontMetrics
;
19 import org
.eclipse
.swt
.graphics
.GC
;
20 import org
.eclipse
.swt
.layout
.GridData
;
21 import org
.eclipse
.swt
.layout
.GridLayout
;
22 import org
.eclipse
.swt
.widgets
.Button
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Group
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.eclipse
.swt
.widgets
.Text
;
28 import org
.eclipse
.ui
.dialogs
.PreferenceLinkArea
;
29 import org
.eclipse
.ui
.preferences
.IWorkbenchPreferenceContainer
;
32 * A collection of factory methods for creating common SWT controls
34 public class SWTUtils
{
37 public static final int MARGINS_DEFAULT
= -1;
40 public static final int MARGINS_NONE
= 0;
43 public static final int MARGINS_DIALOG
= 1;
46 * Creates a preference link which will open in the specified container
53 * @return the created link
55 public static PreferenceLinkArea
createPreferenceLink(
56 IWorkbenchPreferenceContainer container
, Composite parent
,
57 String pageId
, String text
) {
58 final PreferenceLinkArea area
= new PreferenceLinkArea(parent
,
59 SWT
.NONE
, pageId
, text
, container
, null);
64 * Creates a grid data with the specified metrics
71 * @return the created grid data
73 public static GridData
createGridData(int width
, int height
, boolean hFill
,
75 return createGridData(width
, height
, hFill ? SWT
.FILL
: SWT
.BEGINNING
,
76 vFill ? SWT
.FILL
: SWT
.CENTER
, hFill
, vFill
);
80 * Creates a grid data with the specified metrics
89 * @return the created grid data
91 public static GridData
createGridData(int width
, int height
, int hAlign
,
92 int vAlign
, boolean hGrab
, boolean vGrab
) {
93 final GridData gd
= new GridData(hAlign
, vAlign
, hGrab
, vGrab
);
95 gd
.heightHint
= height
;
100 * Creates a horizontal grid data with the default metrics
102 * @return the created grid data
104 public static GridData
createHFillGridData() {
105 return createHFillGridData(1);
109 * Creates a horizontal grid data with the specified span
113 * @return the created grid data
115 public static GridData
createHFillGridData(int span
) {
116 final GridData gd
= createGridData(0, SWT
.DEFAULT
, SWT
.FILL
,
117 SWT
.CENTER
, true, false);
118 gd
.horizontalSpan
= span
;
123 * Creates a horizontal fill composite with the specified margins
128 * @return the created composite
130 public static Composite
createHFillComposite(Composite parent
, int margins
) {
131 return createHFillComposite(parent
, margins
, 1);
135 * Creates a horizontal fill composite with the specified margins and
142 * @return the created composite
144 public static Composite
createHFillComposite(Composite parent
, int margins
,
146 final Composite composite
= new Composite(parent
, SWT
.NONE
);
147 composite
.setFont(parent
.getFont());
148 composite
.setLayoutData(createHFillGridData());
149 composite
.setLayout(createGridLayout(columns
,
150 new PixelConverter(parent
), margins
));
155 * Creates a horizontal/vertical fill composite with the specified margins
160 * @return the created composite
162 public static Composite
createHVFillComposite(Composite parent
, int margins
) {
163 return createHVFillComposite(parent
, margins
, 1);
167 * Creates a horizontal/vertical fill composite with the specified margins
174 * @return the created composite
176 public static Composite
createHVFillComposite(Composite parent
,
177 int margins
, int columns
) {
178 final Composite composite
= new Composite(parent
, SWT
.NONE
);
179 composite
.setFont(parent
.getFont());
180 composite
.setLayoutData(createHVFillGridData());
181 composite
.setLayout(createGridLayout(columns
,
182 new PixelConverter(parent
), margins
));
187 * Creates a horizontal fill group with the specified text and margins
192 * @return the created group
194 public static Group
createHFillGroup(Composite parent
, String text
,
196 return createHFillGroup(parent
, text
, margins
, 1);
200 * Creates a horizontal fill group with the specified text, margins and rows
207 * @return the created group
209 public static Group
createHFillGroup(Composite parent
, String text
,
210 int margins
, int rows
) {
211 final Group group
= new Group(parent
, SWT
.NONE
);
212 group
.setFont(parent
.getFont());
213 group
.setLayoutData(createHFillGridData());
216 group
.setLayout(createGridLayout(rows
, new PixelConverter(parent
),
222 * Creates a horizontal/vertical fill group with the specified text and
229 * @return the created group
231 public static Group
createHVFillGroup(Composite parent
, String text
,
233 return createHVFillGroup(parent
, text
, margins
, 1);
237 * Creates a horizontal/vertical fill group with the specified text, margins
245 * @return the created group
247 public static Group
createHVFillGroup(Composite parent
, String text
,
248 int margins
, int rows
) {
249 final Group group
= new Group(parent
, SWT
.NONE
);
250 group
.setFont(parent
.getFont());
251 group
.setLayoutData(createHVFillGridData());
254 group
.setLayout(createGridLayout(rows
, new PixelConverter(parent
),
260 * Creates a horizontal/vertical fill grid data with the default metrics
262 * @return the created grid data
264 public static GridData
createHVFillGridData() {
265 return createHVFillGridData(1);
269 * Creates a horizontal/vertical fill grid data with the specified span
273 * @return the created grid data
275 public static GridData
createHVFillGridData(int span
) {
276 final GridData gd
= createGridData(0, 0, true, true);
277 gd
.horizontalSpan
= span
;
282 * Creates a grid layout with the specified number of columns and the
286 * the number of columns
288 * the pixel converter
290 * one of <code>MARGINS_DEFAULT</code>, <code>MARGINS_NONE</code>
291 * or <code>MARGINS_DIALOG</code>.
293 * @return the created grid layout
295 public static GridLayout
createGridLayout(int numColumns
,
296 PixelConverter converter
, int margins
) {
297 Assert
.isTrue(margins
== MARGINS_DEFAULT
|| margins
== MARGINS_NONE
298 || margins
== MARGINS_DIALOG
);
300 final GridLayout layout
= new GridLayout(numColumns
, false);
301 layout
.horizontalSpacing
= converter
302 .convertHorizontalDLUsToPixels(IDialogConstants
.HORIZONTAL_SPACING
);
303 layout
.verticalSpacing
= converter
304 .convertVerticalDLUsToPixels(IDialogConstants
.VERTICAL_SPACING
);
308 layout
.marginLeft
= layout
.marginRight
= 0;
309 layout
.marginTop
= layout
.marginBottom
= 0;
312 layout
.marginLeft
= layout
.marginRight
= converter
313 .convertHorizontalDLUsToPixels(IDialogConstants
.HORIZONTAL_MARGIN
);
314 layout
.marginTop
= layout
.marginBottom
= converter
315 .convertVerticalDLUsToPixels(IDialogConstants
.VERTICAL_MARGIN
);
317 case MARGINS_DEFAULT
:
318 layout
.marginLeft
= layout
.marginRight
= layout
.marginWidth
;
319 layout
.marginTop
= layout
.marginBottom
= layout
.marginHeight
;
321 layout
.marginWidth
= layout
.marginHeight
= 0;
326 * Creates a label with the specified message
331 * @return the created label
333 public static Label
createLabel(Composite parent
, String message
) {
334 return createLabel(parent
, message
, 1);
338 * Creates a label with the specified message and span
344 * @return the created label
346 public static Label
createLabel(Composite parent
, String message
, int span
) {
347 final Label label
= new Label(parent
, SWT
.WRAP
);
349 label
.setText(message
);
350 label
.setLayoutData(createHFillGridData(span
));
355 * Creates a check box with the specified message
360 * @return the created check box
362 public static Button
createCheckBox(Composite parent
, String message
) {
363 return createCheckBox(parent
, message
, 1);
367 * Creates a check box with the specified message and span
373 * @return the created check box
375 public static Button
createCheckBox(Composite parent
, String message
,
377 final Button button
= new Button(parent
, SWT
.CHECK
);
378 button
.setText(message
);
379 button
.setLayoutData(createHFillGridData(span
));
384 * Creates a radio button with the specified message
389 * @return the created radio button
391 public static Button
createRadioButton(Composite parent
, String message
) {
392 return createRadioButton(parent
, message
, 1);
396 * Creates a radio button with the specified message and span
402 * @return the created radio button
404 public static Button
createRadioButton(Composite parent
, String message
,
406 final Button button
= new Button(parent
, SWT
.RADIO
);
407 button
.setText(message
);
408 button
.setLayoutData(createHFillGridData(span
));
413 * Creates a text control
417 * @return the created text control
419 public static Text
createText(Composite parent
) {
420 return createText(parent
, 1);
424 * Creates a text control with the specified span
429 * @return the created text control
431 public static Text
createText(Composite parent
, int span
) {
432 final Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.BORDER
);
433 text
.setLayoutData(createHFillGridData(span
));
438 * Creates a place holder with the specified height and span
441 * @param heightInChars
444 * @return the created place holder
446 public static Control
createPlaceholder(Composite parent
,
447 int heightInChars
, int span
) {
448 Assert
.isTrue(heightInChars
> 0);
449 final Control placeHolder
= new Composite(parent
, SWT
.NONE
);
450 final GridData gd
= new GridData(SWT
.BEGINNING
, SWT
.TOP
, false, false);
451 gd
.heightHint
= new PixelConverter(parent
)
452 .convertHeightInCharsToPixels(heightInChars
);
453 gd
.horizontalSpan
= span
;
454 placeHolder
.setLayoutData(gd
);
459 * Creates a place holder with the specified height
462 * @param heightInChars
463 * @return the created place holder
465 public static Control
createPlaceholder(Composite parent
, int heightInChars
) {
466 return createPlaceholder(parent
, heightInChars
, 1);
470 * Creates a pixel converter
474 * @return the created pixel converter
476 public static PixelConverter
createDialogPixelConverter(Control control
) {
477 Dialog
.applyDialogFont(control
);
478 return new PixelConverter(control
);
482 * Calculates the size of the specified controls, using the specified
488 * @return the size of the control(s)
490 public static int calculateControlSize(PixelConverter converter
,
491 Control
[] controls
) {
492 return calculateControlSize(converter
, controls
, 0, controls
.length
- 1);
496 * Calculates the size of the specified subset of controls, using the
497 * specified converter
504 * @return the created control
506 public static int calculateControlSize(PixelConverter converter
,
507 Control
[] controls
, int start
, int end
) {
508 int minimum
= converter
509 .convertHorizontalDLUsToPixels(IDialogConstants
.BUTTON_WIDTH
);
510 for (int i
= start
; i
<= end
; i
++) {
511 final int length
= controls
[i
]
512 .computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
;
513 if (minimum
< length
)
520 * Equalizes the specified controls using the specified converter
525 public static void equalizeControls(PixelConverter converter
,
526 Control
[] controls
) {
527 equalizeControls(converter
, controls
, 0, controls
.length
- 1);
531 * Equalizes the specified subset of controls using the specified converter
538 public static void equalizeControls(PixelConverter converter
,
539 Control
[] controls
, int start
, int end
) {
540 final int size
= calculateControlSize(converter
, controls
, start
, end
);
541 for (int i
= start
; i
<= end
; i
++) {
542 final Control button
= controls
[i
];
543 if (button
.getLayoutData() instanceof GridData
) {
544 ((GridData
) button
.getLayoutData()).widthHint
= size
;
550 * Gets the width of the longest string in <code>strings</code>, using the
551 * specified pixel converter
556 * @return the width of the longest string
558 public static int getWidthInCharsForLongest(PixelConverter converter
,
561 for (int i
= 0; i
< strings
.length
; i
++) {
562 final int length
= converter
.convertWidthInCharsToPixels(strings
[i
]
564 if (minimum
< length
)
570 private static class PixelConverter
{
572 private final FontMetrics fFontMetrics
;
574 public PixelConverter(Control control
) {
575 GC gc
= new GC(control
);
577 gc
.setFont(control
.getFont());
578 fFontMetrics
= gc
.getFontMetrics();
584 public int convertHeightInCharsToPixels(int chars
) {
585 return Dialog
.convertHeightInCharsToPixels(fFontMetrics
, chars
);
588 public int convertHorizontalDLUsToPixels(int dlus
) {
589 return Dialog
.convertHorizontalDLUsToPixels(fFontMetrics
, dlus
);
592 public int convertVerticalDLUsToPixels(int dlus
) {
593 return Dialog
.convertVerticalDLUsToPixels(fFontMetrics
, dlus
);
596 public int convertWidthInCharsToPixels(int chars
) {
597 return Dialog
.convertWidthInCharsToPixels(fFontMetrics
, chars
);