Some javadoc added
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / util / SWTUtil.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.util;
6
7
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;
20
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.util.Assert;
23
24 /**
25  * Utility class to simplify access to some SWT resources. 
26  */
27 public class SWTUtil {
28         
29         /**
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.
33          */
34         public static Display getStandardDisplay() {
35                 Display display;
36                 display= Display.getCurrent();
37                 if (display == null)
38                         display= Display.getDefault();
39                 return display;         
40         }
41         
42         /**
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.
45          * 
46          * @return the shell for the given widget
47          */
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();
61                                                         
62                 return null;    
63         }
64
65
66         /**
67          * Returns a width hint for a button control.
68          */
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);
73         }
74
75         /**
76          * Returns a height hint for a button control.
77          */             
78         public static int getButtonHeigthHint(Button button) {
79                 PixelConverter converter= new PixelConverter(button);
80                 return converter.convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
81         }       
82
83         
84         /**
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>.
88          * 
89          * @param       the button for which to set the dimension hint
90          */             
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);            
97                 }
98         }               
99         
100
101 }