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