Fix build
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / SWTUtils.java
blob7b2912017a35958513ac7730697f24112dee628e
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
8 * Contributors:
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;
31 /**
32 * A collection of factory methods for creating common SWT controls
34 public class SWTUtils {
36 /** */
37 public static final int MARGINS_DEFAULT = -1;
39 /** */
40 public static final int MARGINS_NONE = 0;
42 /** */
43 public static final int MARGINS_DIALOG = 1;
45 /**
46 * Creates a preference link which will open in the specified container
48 * @param container
49 * @param parent
50 * @param pageId
51 * @param text
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);
60 return area;
63 /**
64 * Creates a grid data with the specified metrics
66 * @param width
67 * @param height
68 * @param hFill
69 * @param vFill
71 * @return the created grid data
73 public static GridData createGridData(int width, int height, boolean hFill,
74 boolean vFill) {
75 return createGridData(width, height, hFill ? SWT.FILL : SWT.BEGINNING,
76 vFill ? SWT.FILL : SWT.CENTER, hFill, vFill);
79 /**
80 * Creates a grid data with the specified metrics
82 * @param width
83 * @param height
84 * @param hAlign
85 * @param vAlign
86 * @param hGrab
87 * @param vGrab
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);
94 gd.widthHint = width;
95 gd.heightHint = height;
96 return gd;
99 /**
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
111 * @param 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;
119 return gd;
123 * Creates a horizontal fill composite with the specified margins
125 * @param parent
126 * @param 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
136 * columns
138 * @param parent
139 * @param margins
140 * @param columns
142 * @return the created composite
144 public static Composite createHFillComposite(Composite parent, int margins,
145 int columns) {
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));
151 return composite;
155 * Creates a horizontal/vertical fill composite with the specified margins
157 * @param parent
158 * @param 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
168 * and columns
170 * @param parent
171 * @param margins
172 * @param columns
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));
183 return composite;
187 * Creates a horizontal fill group with the specified text and margins
189 * @param parent
190 * @param text
191 * @param margins
192 * @return the created group
194 public static Group createHFillGroup(Composite parent, String text,
195 int margins) {
196 return createHFillGroup(parent, text, margins, 1);
200 * Creates a horizontal fill group with the specified text, margins and rows
202 * @param parent
203 * @param text
204 * @param margins
205 * @param 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());
214 if (text != null)
215 group.setText(text);
216 group.setLayout(createGridLayout(rows, new PixelConverter(parent),
217 margins));
218 return group;
222 * Creates a horizontal/vertical fill group with the specified text and
223 * margins
225 * @param parent
226 * @param text
227 * @param margins
229 * @return the created group
231 public static Group createHVFillGroup(Composite parent, String text,
232 int margins) {
233 return createHVFillGroup(parent, text, margins, 1);
237 * Creates a horizontal/vertical fill group with the specified text, margins
238 * and rows
240 * @param parent
241 * @param text
242 * @param margins
243 * @param rows
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());
252 if (text != null)
253 group.setText(text);
254 group.setLayout(createGridLayout(rows, new PixelConverter(parent),
255 margins));
256 return group;
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
271 * @param 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;
278 return gd;
282 * Creates a grid layout with the specified number of columns and the
283 * standard spacings.
285 * @param numColumns
286 * the number of columns
287 * @param converter
288 * the pixel converter
289 * @param margins
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);
306 switch (margins) {
307 case MARGINS_NONE:
308 layout.marginLeft = layout.marginRight = 0;
309 layout.marginTop = layout.marginBottom = 0;
310 break;
311 case MARGINS_DIALOG:
312 layout.marginLeft = layout.marginRight = converter
313 .convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
314 layout.marginTop = layout.marginBottom = converter
315 .convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
316 break;
317 case MARGINS_DEFAULT:
318 layout.marginLeft = layout.marginRight = layout.marginWidth;
319 layout.marginTop = layout.marginBottom = layout.marginHeight;
321 layout.marginWidth = layout.marginHeight = 0;
322 return layout;
326 * Creates a label with the specified message
328 * @param parent
329 * @param 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
340 * @param parent
341 * @param message
342 * @param 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);
348 if (message != null)
349 label.setText(message);
350 label.setLayoutData(createHFillGridData(span));
351 return label;
355 * Creates a check box with the specified message
357 * @param parent
358 * @param 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
369 * @param parent
370 * @param message
371 * @param span
373 * @return the created check box
375 public static Button createCheckBox(Composite parent, String message,
376 int span) {
377 final Button button = new Button(parent, SWT.CHECK);
378 button.setText(message);
379 button.setLayoutData(createHFillGridData(span));
380 return button;
384 * Creates a radio button with the specified message
386 * @param parent
387 * @param 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
398 * @param parent
399 * @param message
400 * @param span
402 * @return the created radio button
404 public static Button createRadioButton(Composite parent, String message,
405 int span) {
406 final Button button = new Button(parent, SWT.RADIO);
407 button.setText(message);
408 button.setLayoutData(createHFillGridData(span));
409 return button;
413 * Creates a text control
415 * @param parent
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
426 * @param parent
427 * @param 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));
434 return text;
438 * Creates a place holder with the specified height and span
440 * @param parent
441 * @param heightInChars
442 * @param span
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);
455 return placeHolder;
459 * Creates a place holder with the specified height
461 * @param parent
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
472 * @param control
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
483 * converter
485 * @param converter
486 * @param controls
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
499 * @param converter
500 * @param controls
501 * @param start
502 * @param end
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)
514 minimum = length;
516 return minimum;
520 * Equalizes the specified controls using the specified converter
522 * @param converter
523 * @param controls
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
533 * @param converter
534 * @param controls
535 * @param start
536 * @param end
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
553 * @param converter
554 * @param strings
556 * @return the width of the longest string
558 public static int getWidthInCharsForLongest(PixelConverter converter,
559 String[] strings) {
560 int minimum = 0;
561 for (int i = 0; i < strings.length; i++) {
562 final int length = converter.convertWidthInCharsToPixels(strings[i]
563 .length());
564 if (minimum < length)
565 minimum = length;
567 return minimum;
570 private static class PixelConverter {
572 private final FontMetrics fFontMetrics;
574 public PixelConverter(Control control) {
575 GC gc = new GC(control);
576 try {
577 gc.setFont(control.getFont());
578 fFontMetrics = gc.getFontMetrics();
579 } finally {
580 gc.dispose();
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);