5344dc03ba1acd122af783cf6a9d9ee10a4dda31
[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.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;
22
23 /**
24  * Utility class to simplify access to some SWT resources. 
25  */
26 public class SWTUtil {
27         
28         /**
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.
32          */
33         public static Display getStandardDisplay() {
34                 Display display;
35                 display= Display.getCurrent();
36                 if (display == null)
37                         display= Display.getDefault();
38                 return display;         
39         }
40         
41         /**
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.
44          * 
45          * @return the shell for the given widget
46          */
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();
60                                                         
61                 return null;    
62         }
63
64
65         /**
66          * Returns a width hint for a button control.
67          */
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);
72         }
73
74         /**
75          * Returns a height hint for a button control.
76          */             
77         public static int getButtonHeigthHint(Button button) {
78                 PixelConverter converter= new PixelConverter(button);
79                 return converter.convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
80         }       
81
82         
83         /**
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>.
87          * 
88          * @param       the button for which to set the dimension hint
89          */             
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);            
96                 }
97         }               
98         
99
100 }