2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.util;
8 import org.eclipse.jface.dialogs.IDialogConstants;
9 import org.eclipse.jface.util.Assert;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.dnd.DragSource;
12 import org.eclipse.swt.dnd.DropTarget;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Caret;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Menu;
19 import org.eclipse.swt.widgets.ScrollBar;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.swt.widgets.Widget;
24 * Utility class to simplify access to some SWT resources.
26 public class SWTUtil {
29 * Returns the standard display to be used. The method first checks, if
30 * the thread calling this method has an associated disaply. If so, this
31 * display is returned. Otherwise the method returns the default display.
33 public static Display getStandardDisplay() {
35 display= Display.getCurrent();
37 display= Display.getDefault();
42 * Returns the shell for the given widget. If the widget doesn't represent
43 * a SWT object that manage a shell, <code>null</code> is returned.
45 * @return the shell for the given widget
47 public static Shell getShell(Widget widget) {
48 if (widget instanceof Control)
49 return ((Control)widget).getShell();
50 if (widget instanceof Caret)
51 return ((Caret)widget).getParent().getShell();
52 if (widget instanceof DragSource)
53 return ((DragSource)widget).getControl().getShell();
54 if (widget instanceof DropTarget)
55 return ((DropTarget)widget).getControl().getShell();
56 if (widget instanceof Menu)
57 return ((Menu)widget).getParent().getShell();
58 if (widget instanceof ScrollBar)
59 return ((ScrollBar)widget).getParent().getShell();
66 * Returns a width hint for a button control.
68 public static int getButtonWidthHint(Button button) {
69 PixelConverter converter= new PixelConverter(button);
70 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
71 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
75 * Returns a height hint for a button control.
77 public static int getButtonHeigthHint(Button button) {
78 PixelConverter converter= new PixelConverter(button);
79 return converter.convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
84 * Sets width and height hint for the button control.
85 * <b>Note:</b> This is a NOP if the button's layout data is not
86 * an instance of <code>GridData</code>.
88 * @param the button for which to set the dimension hint
90 public static void setButtonDimensionHint(Button button) {
91 Assert.isNotNull(button);
92 Object gd= button.getLayoutData();
93 if (gd instanceof GridData) {
94 ((GridData)gd).heightHint= getButtonHeigthHint(button);
95 ((GridData)gd).widthHint= getButtonWidthHint(button);