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