979463c5ef9049c8170898131984434541b3994c
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / util / SWTUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.util;
12
13 import org.eclipse.jface.dialogs.IDialogConstants;
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.jface.util.Assert;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.dnd.DragSource;
18 import org.eclipse.swt.dnd.DropTarget;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Caret;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Menu;
25 import org.eclipse.swt.widgets.ScrollBar;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Table;
28 import org.eclipse.swt.widgets.Widget;
29
30 /**
31  * Utility class to simplify access to some SWT resources.
32  */
33 public class SWTUtil {
34
35         /**
36          * Returns the standard display to be used. The method first checks, if the
37          * thread calling this method has an associated disaply. If so, this display
38          * is returned. Otherwise the method returns the default display.
39          */
40         public static Display getStandardDisplay() {
41                 Display display;
42                 display = Display.getCurrent();
43                 if (display == null)
44                         display = Display.getDefault();
45                 return display;
46         }
47
48         /**
49          * Returns the shell for the given widget. If the widget doesn't represent a
50          * SWT object that manage a shell, <code>null</code> is returned.
51          * 
52          * @return the shell for the given widget
53          */
54         public static Shell getShell(Widget widget) {
55                 if (widget instanceof Control)
56                         return ((Control) widget).getShell();
57                 if (widget instanceof Caret)
58                         return ((Caret) widget).getParent().getShell();
59                 if (widget instanceof DragSource)
60                         return ((DragSource) widget).getControl().getShell();
61                 if (widget instanceof DropTarget)
62                         return ((DropTarget) widget).getControl().getShell();
63                 if (widget instanceof Menu)
64                         return ((Menu) widget).getParent().getShell();
65                 if (widget instanceof ScrollBar)
66                         return ((ScrollBar) widget).getParent().getShell();
67
68                 return null;
69         }
70
71         /**
72          * Returns a width hint for a button control.
73          */
74         public static int getButtonWidthHint(Button button) {
75                 button.setFont(JFaceResources.getDialogFont());
76                 PixelConverter converter = new PixelConverter(button);
77                 int widthHint = converter
78                                 .convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
79                 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT,
80                                 true).x);
81         }
82
83         /**
84          * Returns a height hint for a button control.
85          */
86         public static int getButtonHeightHint(Button button) {
87                 button.setFont(JFaceResources.getDialogFont());
88                 PixelConverter converter = new PixelConverter(button);
89                 return converter
90                                 .convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
91         }
92
93         /**
94          * Sets width and height hint for the button control. <b>Note:</b> This is
95          * a NOP if the button's layout data is not an instance of
96          * <code>GridData</code>.
97          * 
98          * @param button
99          *            the button for which to set the dimension hint
100          */
101         public static void setButtonDimensionHint(Button button) {
102                 Assert.isNotNull(button);
103                 Object gd = button.getLayoutData();
104                 if (gd instanceof GridData) {
105                         ((GridData) gd).heightHint = getButtonHeightHint(button);
106                         ((GridData) gd).widthHint = getButtonWidthHint(button);
107                         ((GridData) gd).horizontalAlignment = GridData.FILL;
108                 }
109         }
110
111         public static int getTableHeightHint(Table table, int rows) {
112                 if (table.getFont().equals(JFaceResources.getDefaultFont()))
113                         table.setFont(JFaceResources.getDialogFont());
114                 int result = table.getItemHeight() * rows + table.getHeaderHeight();
115                 if (table.getLinesVisible())
116                         result += table.getGridLineWidth() * (rows - 1);
117                 return result;
118         }
119
120 }