KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > 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.jdt.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      */

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

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

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

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