Improve error reporting in the branch dialog
[egit/imyousuf.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / SWTUtils.java
blob8ebec05da879f88721bec2ee4abde1bd2ca16996
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.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;
26 /**
27 * A collection of factory methods for creating common SWT controls
29 public class SWTUtils {
31 /** */
32 public static final int MARGINS_DEFAULT = -1;
34 /** */
35 public static final int MARGINS_NONE = 0;
37 /** */
38 public static final int MARGINS_DIALOG = 1;
40 /**
41 * Creates a preference link which will open in the specified container
43 * @param container
44 * @param parent
45 * @param pageId
46 * @param text
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);
55 return area;
58 /**
59 * Creates a grid data with the specified metrics
61 * @param width
62 * @param height
63 * @param hFill
64 * @param vFill
66 * @return the created grid data
68 public static GridData createGridData(int width, int height, boolean hFill,
69 boolean vFill) {
70 return createGridData(width, height, hFill ? SWT.FILL : SWT.BEGINNING,
71 vFill ? SWT.FILL : SWT.CENTER, hFill, vFill);
74 /**
75 * Creates a grid data with the specified metrics
77 * @param width
78 * @param height
79 * @param hAlign
80 * @param vAlign
81 * @param hGrab
82 * @param vGrab
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);
89 gd.widthHint = width;
90 gd.heightHint = height;
91 return gd;
94 /**
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
106 * @param 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;
114 return gd;
118 * Creates a horizontal fill composite with the specified margins
120 * @param parent
121 * @param 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
131 * columns
133 * @param parent
134 * @param margins
135 * @param columns
137 * @return the created composite
139 public static Composite createHFillComposite(Composite parent, int margins,
140 int columns) {
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));
146 return composite;
150 * Creates a horizontal/vertical fill composite with the specified margins
152 * @param parent
153 * @param 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
163 * and columns
165 * @param parent
166 * @param margins
167 * @param columns
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));
178 return composite;
182 * Creates a horizontal fill group with the specified text and margins
184 * @param parent
185 * @param text
186 * @param margins
187 * @return the created group
189 public static Group createHFillGroup(Composite parent, String text,
190 int margins) {
191 return createHFillGroup(parent, text, margins, 1);
195 * Creates a horizontal fill group with the specified text, margins and rows
197 * @param parent
198 * @param text
199 * @param margins
200 * @param 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());
209 if (text != null)
210 group.setText(text);
211 group.setLayout(createGridLayout(rows, new PixelConverter(parent),
212 margins));
213 return group;
217 * Creates a horizontal/vertical fill group with the specified text and
218 * margins
220 * @param parent
221 * @param text
222 * @param margins
224 * @return the created group
226 public static Group createHVFillGroup(Composite parent, String text,
227 int margins) {
228 return createHVFillGroup(parent, text, margins, 1);
232 * Creates a horizontal/vertical fill group with the specified text, margins
233 * and rows
235 * @param parent
236 * @param text
237 * @param margins
238 * @param rows
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());
247 if (text != null)
248 group.setText(text);
249 group.setLayout(createGridLayout(rows, new PixelConverter(parent),
250 margins));
251 return group;
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
266 * @param 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;
273 return gd;
277 * Creates a grid layout with the specified number of columns and the
278 * standard spacings.
280 * @param numColumns
281 * the number of columns
282 * @param converter
283 * the pixel converter
284 * @param margins
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);
301 switch (margins) {
302 case MARGINS_NONE:
303 layout.marginLeft = layout.marginRight = 0;
304 layout.marginTop = layout.marginBottom = 0;
305 break;
306 case MARGINS_DIALOG:
307 layout.marginLeft = layout.marginRight = converter
308 .convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
309 layout.marginTop = layout.marginBottom = converter
310 .convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
311 break;
312 case MARGINS_DEFAULT:
313 layout.marginLeft = layout.marginRight = layout.marginWidth;
314 layout.marginTop = layout.marginBottom = layout.marginHeight;
316 layout.marginWidth = layout.marginHeight = 0;
317 return layout;
321 * Creates a label with the specified message
323 * @param parent
324 * @param 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
335 * @param parent
336 * @param message
337 * @param 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);
343 if (message != null)
344 label.setText(message);
345 label.setLayoutData(createHFillGridData(span));
346 return label;
350 * Creates a check box with the specified message
352 * @param parent
353 * @param 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
364 * @param parent
365 * @param message
366 * @param span
368 * @return the created check box
370 public static Button createCheckBox(Composite parent, String message,
371 int span) {
372 final Button button = new Button(parent, SWT.CHECK);
373 button.setText(message);
374 button.setLayoutData(createHFillGridData(span));
375 return button;
379 * Creates a radio button with the specified message
381 * @param parent
382 * @param 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
393 * @param parent
394 * @param message
395 * @param span
397 * @return the created radio button
399 public static Button createRadioButton(Composite parent, String message,
400 int span) {
401 final Button button = new Button(parent, SWT.RADIO);
402 button.setText(message);
403 button.setLayoutData(createHFillGridData(span));
404 return button;
408 * Creates a text control
410 * @param parent
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
421 * @param parent
422 * @param 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));
429 return text;
433 * Creates a place holder with the specified height and span
435 * @param parent
436 * @param heightInChars
437 * @param span
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);
450 return placeHolder;
454 * Creates a place holder with the specified height
456 * @param parent
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
467 * @param control
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
478 * converter
480 * @param converter
481 * @param controls
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
494 * @param converter
495 * @param controls
496 * @param start
497 * @param end
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)
509 minimum = length;
511 return minimum;
515 * Equalizes the specified controls using the specified converter
517 * @param converter
518 * @param controls
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
528 * @param converter
529 * @param controls
530 * @param start
531 * @param end
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
548 * @param converter
549 * @param strings
551 * @return the width of the longest string
553 public static int getWidthInCharsForLongest(PixelConverter converter,
554 String[] strings) {
555 int minimum = 0;
556 for (int i = 0; i < strings.length; i++) {
557 final int length = converter.convertWidthInCharsToPixels(strings[i]
558 .length());
559 if (minimum < length)
560 minimum = length;
562 return minimum;
565 private static class PixelConverter {
567 private final FontMetrics fFontMetrics;
569 public PixelConverter(Control control) {
570 GC gc = new GC(control);
571 try {
572 gc.setFont(control.getFont());
573 fFontMetrics = gc.getFontMetrics();
574 } finally {
575 gc.dispose();
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);