1) Fixed issue #860: Added category.xml to the project.
[phpeclipse.git] / net.sourceforge.phpeclipse / 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 //incastrix
16 //import org.eclipse.jface.text.Assert;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.dnd.DragSource;
20 import org.eclipse.swt.dnd.DropTarget;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Caret;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.swt.widgets.ScrollBar;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.Widget;
31
32 /**
33  * Utility class to simplify access to some SWT resources.
34  */
35 public class SWTUtil {
36
37         /**
38          * Returns the standard display to be used. The method first checks, if the
39          * thread calling this method has an associated disaply. If so, this display
40          * is returned. Otherwise the method returns the default display.
41          */
42         public static Display getStandardDisplay() {
43                 Display display;
44                 display = Display.getCurrent();
45                 if (display == null)
46                         display = Display.getDefault();
47                 return display;
48         }
49
50         /**
51          * Returns the shell for the given widget. If the widget doesn't represent a
52          * SWT object that manage a shell, <code>null</code> is returned.
53          * 
54          * @return the shell for the given widget
55          */
56         public static Shell getShell(Widget widget) {
57                 if (widget instanceof Control)
58                         return ((Control) widget).getShell();
59                 if (widget instanceof Caret)
60                         return ((Caret) widget).getParent().getShell();
61                 if (widget instanceof DragSource)
62                         return ((DragSource) widget).getControl().getShell();
63                 if (widget instanceof DropTarget)
64                         return ((DropTarget) widget).getControl().getShell();
65                 if (widget instanceof Menu)
66                         return ((Menu) widget).getParent().getShell();
67                 if (widget instanceof ScrollBar)
68                         return ((ScrollBar) widget).getParent().getShell();
69
70                 return null;
71         }
72
73         /**
74          * Returns a width hint for a button control.
75          */
76         public static int getButtonWidthHint(Button button) {
77                 button.setFont(JFaceResources.getDialogFont());
78                 PixelConverter converter = new PixelConverter(button);
79                 int widthHint = converter
80                                 .convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
81                 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT,
82                                 true).x);
83         }
84
85         /**
86          * Returns a height hint for a button control.
87          */
88         public static int getButtonHeightHint(Button button) {
89                 button.setFont(JFaceResources.getDialogFont());
90                 PixelConverter converter = new PixelConverter(button);
91                 return converter
92                                 .convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
93         }
94
95         /**
96          * Sets width and height hint for the button control. <b>Note:</b> This is
97          * a NOP if the button's layout data is not an instance of
98          * <code>GridData</code>.
99          * 
100          * @param button
101          *            the button for which to set the dimension hint
102          */
103         public static void setButtonDimensionHint(Button button) {
104                 Assert.isNotNull(button);
105                 Object gd = button.getLayoutData();
106                 if (gd instanceof GridData) {
107                         ((GridData) gd).heightHint = getButtonHeightHint(button);
108                         ((GridData) gd).widthHint = getButtonWidthHint(button);
109                         ((GridData) gd).horizontalAlignment = GridData.FILL;
110                 }
111         }
112
113         public static int getTableHeightHint(Table table, int rows) {
114                 if (table.getFont().equals(JFaceResources.getDefaultFont()))
115                         table.setFont(JFaceResources.getDialogFont());
116                 int result = table.getItemHeight() * rows + table.getHeaderHeight();
117                 if (table.getLinesVisible())
118                         result += table.getGridLineWidth() * (rows - 1);
119                 return result;
120         }
121
122 }