StagingView wrongly sorted by state initially
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / SWTUtils.java
blob227534b4979cede7407cac1706b9c19d26e52711
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
12 * Contributors:
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;
35 /**
36 * A collection of factory methods for creating common SWT controls
38 public class SWTUtils {
40 /** */
41 public static final int MARGINS_DEFAULT = -1;
43 /** */
44 public static final int MARGINS_NONE = 0;
46 /** */
47 public static final int MARGINS_DIALOG = 1;
49 /**
50 * Creates a preference link which will open in the specified container
52 * @param container
53 * @param parent
54 * @param pageId
55 * @param text
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);
64 return area;
67 /**
68 * Creates a grid data with the specified metrics
70 * @param width
71 * @param height
72 * @param hFill
73 * @param vFill
75 * @return the created grid data
77 public static GridData createGridData(int width, int height, boolean hFill,
78 boolean vFill) {
79 return createGridData(width, height, hFill ? SWT.FILL : SWT.BEGINNING,
80 vFill ? SWT.FILL : SWT.CENTER, hFill, vFill);
83 /**
84 * Creates a grid data with the specified metrics
86 * @param width
87 * @param height
88 * @param hAlign
89 * @param vAlign
90 * @param hGrab
91 * @param vGrab
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);
98 gd.widthHint = width;
99 gd.heightHint = height;
100 return gd;
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
115 * @param 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;
123 return gd;
127 * Creates a horizontal fill composite with the specified margins
129 * @param parent
130 * @param 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
140 * columns
142 * @param parent
143 * @param margins
144 * @param columns
146 * @return the created composite
148 public static Composite createHFillComposite(Composite parent, int margins,
149 int columns) {
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));
155 return composite;
159 * Creates a horizontal/vertical fill composite with the specified margins
161 * @param parent
162 * @param 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
172 * and columns
174 * @param parent
175 * @param margins
176 * @param columns
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));
187 return composite;
191 * Creates a horizontal fill group with the specified text and margins
193 * @param parent
194 * @param text
195 * @param margins
196 * @return the created group
198 public static Group createHFillGroup(Composite parent, String text,
199 int margins) {
200 return createHFillGroup(parent, text, margins, 1);
204 * Creates a horizontal fill group with the specified text, margins and rows
206 * @param parent
207 * @param text
208 * @param margins
209 * @param 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());
218 if (text != null)
219 group.setText(text);
220 group.setLayout(createGridLayout(rows, new PixelConverter(parent),
221 margins));
222 return group;
226 * Creates a horizontal/vertical fill group with the specified text and
227 * margins
229 * @param parent
230 * @param text
231 * @param margins
233 * @return the created group
235 public static Group createHVFillGroup(Composite parent, String text,
236 int margins) {
237 return createHVFillGroup(parent, text, margins, 1);
241 * Creates a horizontal/vertical fill group with the specified text, margins
242 * and rows
244 * @param parent
245 * @param text
246 * @param margins
247 * @param rows
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());
256 if (text != null)
257 group.setText(text);
258 group.setLayout(createGridLayout(rows, new PixelConverter(parent),
259 margins));
260 return group;
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
275 * @param 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;
282 return gd;
286 * Creates a grid layout with the specified number of columns and the
287 * standard spacings.
289 * @param numColumns
290 * the number of columns
291 * @param converter
292 * the pixel converter
293 * @param margins
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);
310 switch (margins) {
311 case MARGINS_NONE:
312 layout.marginLeft = layout.marginRight = 0;
313 layout.marginTop = layout.marginBottom = 0;
314 break;
315 case MARGINS_DIALOG:
316 layout.marginLeft = layout.marginRight = converter
317 .convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
318 layout.marginTop = layout.marginBottom = converter
319 .convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
320 break;
321 case MARGINS_DEFAULT:
322 layout.marginLeft = layout.marginRight = layout.marginWidth;
323 layout.marginTop = layout.marginBottom = layout.marginHeight;
324 break;
325 default:
326 break;
328 layout.marginWidth = layout.marginHeight = 0;
329 return layout;
333 * Creates a label with the specified message
335 * @param parent
336 * @param 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
347 * @param parent
348 * @param message
349 * @param 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);
355 if (message != null)
356 label.setText(message);
357 label.setLayoutData(createHFillGridData(span));
358 return label;
362 * Creates a check box with the specified message
364 * @param parent
365 * @param 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
376 * @param parent
377 * @param message
378 * @param span
380 * @return the created check box
382 public static Button createCheckBox(Composite parent, String message,
383 int span) {
384 final Button button = new Button(parent, SWT.CHECK);
385 button.setText(message);
386 button.setLayoutData(createHFillGridData(span));
387 return button;
391 * Creates a radio button with the specified message
393 * @param parent
394 * @param 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
405 * @param parent
406 * @param message
407 * @param span
409 * @return the created radio button
411 public static Button createRadioButton(Composite parent, String message,
412 int span) {
413 final Button button = new Button(parent, SWT.RADIO);
414 button.setText(message);
415 button.setLayoutData(createHFillGridData(span));
416 return button;
420 * Creates a text control
422 * @param parent
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
433 * @param parent
434 * @param 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));
441 return text;
445 * Creates a place holder with the specified height and span
447 * @param parent
448 * @param heightInChars
449 * @param span
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);
462 return placeHolder;
466 * Creates a place holder with the specified height
468 * @param parent
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
479 * @param control
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
490 * converter
492 * @param converter
493 * @param controls
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
506 * @param converter
507 * @param controls
508 * @param start
509 * @param end
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)
521 minimum = length;
523 return minimum;
527 * Equalizes the specified controls using the specified converter
529 * @param converter
530 * @param controls
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
540 * @param converter
541 * @param controls
542 * @param start
543 * @param end
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
560 * @param converter
561 * @param strings
563 * @return the width of the longest string
565 public static int getWidthInCharsForLongest(PixelConverter converter,
566 String[] strings) {
567 int minimum = 0;
568 for (int i = 0; i < strings.length; i++) {
569 final int length = converter.convertWidthInCharsToPixels(strings[i]
570 .length());
571 if (minimum < length)
572 minimum = length;
574 return minimum;
577 private static class PixelConverter {
579 private final FontMetrics fFontMetrics;
581 public PixelConverter(Control control) {
582 GC gc = new GC(control);
583 try {
584 gc.setFont(control.getFont());
585 fFontMetrics = gc.getFontMetrics();
586 } finally {
587 gc.dispose();
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);