KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > util > SWTUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.ui.util;
12
13
14 import org.eclipse.core.runtime.Assert;
15
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 import org.eclipse.jface.dialogs.IDialogConstants;
31 import org.eclipse.jface.resource.JFaceResources;
32
33 /**
34  * Utility class to simplify access to some SWT resources.
35  */

36 public class SWTUtil {
37     
38     /**
39      * Returns the standard display to be used. The method first checks, if
40      * the thread calling this method has an associated disaply. If so, this
41      * display is returned. Otherwise the method returns the default display.
42      * @return Returns the standard display to be used.
43      */

44     public static Display getStandardDisplay() {
45         Display display;
46         display= Display.getCurrent();
47         if (display == null)
48             display= Display.getDefault();
49         return display;
50     }
51     
52     /**
53      * Returns the shell for the given widget. If the widget doesn't represent
54      * a SWT object that manage a shell, <code>null</code> is returned.
55      *
56      * @param widget The widget to get the shell for
57      * @return the shell for the given widget
58      */

59     public static Shell getShell(Widget widget) {
60         if (widget instanceof Control)
61             return ((Control)widget).getShell();
62         if (widget instanceof Caret)
63             return ((Caret)widget).getParent().getShell();
64         if (widget instanceof DragSource)
65             return ((DragSource)widget).getControl().getShell();
66         if (widget instanceof DropTarget)
67             return ((DropTarget)widget).getControl().getShell();
68         if (widget instanceof Menu)
69             return ((Menu)widget).getParent().getShell();
70         if (widget instanceof ScrollBar)
71             return ((ScrollBar)widget).getParent().getShell();
72                             
73         return null;
74     }
75
76
77     /**
78      * Returns a width hint for a button control.
79      * @param button The button to calculate the width for
80      * @return The width of the button
81      */

82     public static int getButtonWidthHint(Button button) {
83         button.setFont(JFaceResources.getDialogFont());
84         PixelConverter converter= new PixelConverter(button);
85         int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
86         return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
87     }
88
89     /**
90      * Sets width and height hint for the button control.
91      * <b>Note:</b> This is a NOP if the button's layout data is not
92      * an instance of <code>GridData</code>.
93      *
94      * @param button the button for which to set the dimension hint
95      */

96     public static void setButtonDimensionHint(Button button) {
97         Assert.isNotNull(button);
98         Object JavaDoc gd= button.getLayoutData();
99         if (gd instanceof GridData) {
100             ((GridData)gd).widthHint= getButtonWidthHint(button);
101             ((GridData)gd).horizontalAlignment = GridData.FILL;
102         }
103     }
104     
105     public static int getTableHeightHint(Table table, int rows) {
106         if (table.getFont().equals(JFaceResources.getDefaultFont()))
107             table.setFont(JFaceResources.getDialogFont());
108         int result= table.getItemHeight() * rows + table.getHeaderHeight();
109         if (table.getLinesVisible())
110             result+= table.getGridLineWidth() * (rows - 1);
111         return result;
112     }
113     
114
115 }
116
Popular Tags