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
.spearce
.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
.*;
23 import org
.eclipse
.ui
.dialogs
.PreferenceLinkArea
;
24 import org
.eclipse
.ui
.preferences
.IWorkbenchPreferenceContainer
;
27 * A collection of factory methods for creating common SWT controls
29 public class SWTUtils
{
32 public static final int MARGINS_DEFAULT
= -1;
35 public static final int MARGINS_NONE
= 0;
38 public static final int MARGINS_DIALOG
= 1;
41 * Creates a preference link which will open in the specified container
48 * @return the created link
50 public static PreferenceLinkArea
createPreferenceLink(
51 IWorkbenchPreferenceContainer container
, Composite parent
,
52 String pageId
, String text
) {
53 final PreferenceLinkArea area
= new PreferenceLinkArea(parent
,
54 SWT
.NONE
, pageId
, text
, container
, null);
59 * Creates a grid data with the specified metrics
66 * @return the created grid data
68 public static GridData
createGridData(int width
, int height
, boolean hFill
,
70 return createGridData(width
, height
, hFill ? SWT
.FILL
: SWT
.BEGINNING
,
71 vFill ? SWT
.FILL
: SWT
.CENTER
, hFill
, vFill
);
75 * Creates a grid data with the specified metrics
84 * @return the created grid data
86 public static GridData
createGridData(int width
, int height
, int hAlign
,
87 int vAlign
, boolean hGrab
, boolean vGrab
) {
88 final GridData gd
= new GridData(hAlign
, vAlign
, hGrab
, vGrab
);
90 gd
.heightHint
= height
;
95 * Creates a horizontal grid data with the default metrics
97 * @return the created grid data
99 public static GridData
createHFillGridData() {
100 return createHFillGridData(1);
104 * Creates a horizontal grid data with the specified span
108 * @return the created grid data
110 public static GridData
createHFillGridData(int span
) {
111 final GridData gd
= createGridData(0, SWT
.DEFAULT
, SWT
.FILL
,
112 SWT
.CENTER
, true, false);
113 gd
.horizontalSpan
= span
;
118 * Creates a horizontal fill composite with the specified margins
123 * @return the created composite
125 public static Composite
createHFillComposite(Composite parent
, int margins
) {
126 return createHFillComposite(parent
, margins
, 1);
130 * Creates a horizontal fill composite with the specified margins and
137 * @return the created composite
139 public static Composite
createHFillComposite(Composite parent
, int margins
,
141 final Composite composite
= new Composite(parent
, SWT
.NONE
);
142 composite
.setFont(parent
.getFont());
143 composite
.setLayoutData(createHFillGridData());
144 composite
.setLayout(createGridLayout(columns
,
145 new PixelConverter(parent
), margins
));
150 * Creates a horizontal/vertical fill composite with the specified margins
155 * @return the created composite
157 public static Composite
createHVFillComposite(Composite parent
, int margins
) {
158 return createHVFillComposite(parent
, margins
, 1);
162 * Creates a horizontal/vertical fill composite with the specified margins
169 * @return the created composite
171 public static Composite
createHVFillComposite(Composite parent
,
172 int margins
, int columns
) {
173 final Composite composite
= new Composite(parent
, SWT
.NONE
);
174 composite
.setFont(parent
.getFont());
175 composite
.setLayoutData(createHVFillGridData());
176 composite
.setLayout(createGridLayout(columns
,
177 new PixelConverter(parent
), margins
));
182 * Creates a horizontal fill group with the specified text and margins
187 * @return the created group
189 public static Group
createHFillGroup(Composite parent
, String text
,
191 return createHFillGroup(parent
, text
, margins
, 1);
195 * Creates a horizontal fill group with the specified text, margins and rows
202 * @return the created group
204 public static Group
createHFillGroup(Composite parent
, String text
,
205 int margins
, int rows
) {
206 final Group group
= new Group(parent
, SWT
.NONE
);
207 group
.setFont(parent
.getFont());
208 group
.setLayoutData(createHFillGridData());
211 group
.setLayout(createGridLayout(rows
, new PixelConverter(parent
),
217 * Creates a horizontal/vertical fill group with the specified text and
224 * @return the created group
226 public static Group
createHVFillGroup(Composite parent
, String text
,
228 return createHVFillGroup(parent
, text
, margins
, 1);
232 * Creates a horizontal/vertical fill group with the specified text, margins
240 * @return the created group
242 public static Group
createHVFillGroup(Composite parent
, String text
,
243 int margins
, int rows
) {
244 final Group group
= new Group(parent
, SWT
.NONE
);
245 group
.setFont(parent
.getFont());
246 group
.setLayoutData(createHVFillGridData());
249 group
.setLayout(createGridLayout(rows
, new PixelConverter(parent
),
255 * Creates a horizontal/vertical fill grid data with the default metrics
257 * @return the created grid data
259 public static GridData
createHVFillGridData() {
260 return createHVFillGridData(1);
264 * Creates a horizontal/vertical fill grid data with the specified span
268 * @return the created grid data
270 public static GridData
createHVFillGridData(int span
) {
271 final GridData gd
= createGridData(0, 0, true, true);
272 gd
.horizontalSpan
= span
;
277 * Creates a grid layout with the specified number of columns and the
281 * the number of columns
283 * the pixel converter
285 * one of <code>MARGINS_DEFAULT</code>, <code>MARGINS_NONE</code>
286 * or <code>MARGINS_DIALOG</code>.
288 * @return the created grid layout
290 public static GridLayout
createGridLayout(int numColumns
,
291 PixelConverter converter
, int margins
) {
292 Assert
.isTrue(margins
== MARGINS_DEFAULT
|| margins
== MARGINS_NONE
293 || margins
== MARGINS_DIALOG
);
295 final GridLayout layout
= new GridLayout(numColumns
, false);
296 layout
.horizontalSpacing
= converter
297 .convertHorizontalDLUsToPixels(IDialogConstants
.HORIZONTAL_SPACING
);
298 layout
.verticalSpacing
= converter
299 .convertVerticalDLUsToPixels(IDialogConstants
.VERTICAL_SPACING
);
303 layout
.marginLeft
= layout
.marginRight
= 0;
304 layout
.marginTop
= layout
.marginBottom
= 0;
307 layout
.marginLeft
= layout
.marginRight
= converter
308 .convertHorizontalDLUsToPixels(IDialogConstants
.HORIZONTAL_MARGIN
);
309 layout
.marginTop
= layout
.marginBottom
= converter
310 .convertVerticalDLUsToPixels(IDialogConstants
.VERTICAL_MARGIN
);
312 case MARGINS_DEFAULT
:
313 layout
.marginLeft
= layout
.marginRight
= layout
.marginWidth
;
314 layout
.marginTop
= layout
.marginBottom
= layout
.marginHeight
;
316 layout
.marginWidth
= layout
.marginHeight
= 0;
321 * Creates a label with the specified message
326 * @return the created label
328 public static Label
createLabel(Composite parent
, String message
) {
329 return createLabel(parent
, message
, 1);
333 * Creates a label with the specified message and span
339 * @return the created label
341 public static Label
createLabel(Composite parent
, String message
, int span
) {
342 final Label label
= new Label(parent
, SWT
.WRAP
);
344 label
.setText(message
);
345 label
.setLayoutData(createHFillGridData(span
));
350 * Creates a check box with the specified message
355 * @return the created check box
357 public static Button
createCheckBox(Composite parent
, String message
) {
358 return createCheckBox(parent
, message
, 1);
362 * Creates a check box with the specified message and span
368 * @return the created check box
370 public static Button
createCheckBox(Composite parent
, String message
,
372 final Button button
= new Button(parent
, SWT
.CHECK
);
373 button
.setText(message
);
374 button
.setLayoutData(createHFillGridData(span
));
379 * Creates a radio button with the specified message
384 * @return the created radio button
386 public static Button
createRadioButton(Composite parent
, String message
) {
387 return createRadioButton(parent
, message
, 1);
391 * Creates a radio button with the specified message and span
397 * @return the created radio button
399 public static Button
createRadioButton(Composite parent
, String message
,
401 final Button button
= new Button(parent
, SWT
.RADIO
);
402 button
.setText(message
);
403 button
.setLayoutData(createHFillGridData(span
));
408 * Creates a text control
412 * @return the created text control
414 public static Text
createText(Composite parent
) {
415 return createText(parent
, 1);
419 * Creates a text control with the specified span
424 * @return the created text control
426 public static Text
createText(Composite parent
, int span
) {
427 final Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.BORDER
);
428 text
.setLayoutData(createHFillGridData(span
));
433 * Creates a place holder with the specified height and span
436 * @param heightInChars
439 * @return the created place holder
441 public static Control
createPlaceholder(Composite parent
,
442 int heightInChars
, int span
) {
443 Assert
.isTrue(heightInChars
> 0);
444 final Control placeHolder
= new Composite(parent
, SWT
.NONE
);
445 final GridData gd
= new GridData(SWT
.BEGINNING
, SWT
.TOP
, false, false);
446 gd
.heightHint
= new PixelConverter(parent
)
447 .convertHeightInCharsToPixels(heightInChars
);
448 gd
.horizontalSpan
= span
;
449 placeHolder
.setLayoutData(gd
);
454 * Creates a place holder with the specified height
457 * @param heightInChars
458 * @return the created place holder
460 public static Control
createPlaceholder(Composite parent
, int heightInChars
) {
461 return createPlaceholder(parent
, heightInChars
, 1);
465 * Creates a pixel converter
469 * @return the created pixel converter
471 public static PixelConverter
createDialogPixelConverter(Control control
) {
472 Dialog
.applyDialogFont(control
);
473 return new PixelConverter(control
);
477 * Calculates the size of the specified controls, using the specified
483 * @return the size of the control(s)
485 public static int calculateControlSize(PixelConverter converter
,
486 Control
[] controls
) {
487 return calculateControlSize(converter
, controls
, 0, controls
.length
- 1);
491 * Calculates the size of the specified subset of controls, using the
492 * specified converter
499 * @return the created control
501 public static int calculateControlSize(PixelConverter converter
,
502 Control
[] controls
, int start
, int end
) {
503 int minimum
= converter
504 .convertHorizontalDLUsToPixels(IDialogConstants
.BUTTON_WIDTH
);
505 for (int i
= start
; i
<= end
; i
++) {
506 final int length
= controls
[i
]
507 .computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
;
508 if (minimum
< length
)
515 * Equalizes the specified controls using the specified converter
520 public static void equalizeControls(PixelConverter converter
,
521 Control
[] controls
) {
522 equalizeControls(converter
, controls
, 0, controls
.length
- 1);
526 * Equalizes the specified subset of controls using the specified converter
533 public static void equalizeControls(PixelConverter converter
,
534 Control
[] controls
, int start
, int end
) {
535 final int size
= calculateControlSize(converter
, controls
, start
, end
);
536 for (int i
= start
; i
<= end
; i
++) {
537 final Control button
= controls
[i
];
538 if (button
.getLayoutData() instanceof GridData
) {
539 ((GridData
) button
.getLayoutData()).widthHint
= size
;
545 * Gets the width of the longest string in <code>strings</code>, using the
546 * specified pixel converter
551 * @return the width of the longest string
553 public static int getWidthInCharsForLongest(PixelConverter converter
,
556 for (int i
= 0; i
< strings
.length
; i
++) {
557 final int length
= converter
.convertWidthInCharsToPixels(strings
[i
]
559 if (minimum
< length
)
565 private static class PixelConverter
{
567 private final FontMetrics fFontMetrics
;
569 public PixelConverter(Control control
) {
570 GC gc
= new GC(control
);
572 gc
.setFont(control
.getFont());
573 fFontMetrics
= gc
.getFontMetrics();
579 public int convertHeightInCharsToPixels(int chars
) {
580 return Dialog
.convertHeightInCharsToPixels(fFontMetrics
, chars
);
583 public int convertHorizontalDLUsToPixels(int dlus
) {
584 return Dialog
.convertHorizontalDLUsToPixels(fFontMetrics
, dlus
);
587 public int convertVerticalDLUsToPixels(int dlus
) {
588 return Dialog
.convertVerticalDLUsToPixels(fFontMetrics
, dlus
);
591 public int convertWidthInCharsToPixels(int chars
) {
592 return Dialog
.convertWidthInCharsToPixels(fFontMetrics
, chars
);