KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > 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.pde.internal.ui.util;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.resource.JFaceResources;
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.graphics.Point;
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.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      * Returns a width hint for a button control.
74      */

75     public static int getButtonWidthHint(Button button) {
76         if (button.getFont().equals(JFaceResources.getDefaultFont()))
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      * Sets width and height hint for the button control.
85      * <b>Note:</b> This is a NOP if the button's layout data is not
86      * an instance of <code>GridData</code>.
87      *
88      * @param the button for which to set the dimension hint
89      */

90     public static void setButtonDimensionHint(Button button) {
91         Dialog.applyDialogFont(button);
92         Assert.isNotNull(button);
93         Object JavaDoc gd = button.getLayoutData();
94         if (gd instanceof GridData) {
95             ((GridData) gd).widthHint = getButtonWidthHint(button);
96         }
97     }
98
99     public static void setDialogSize(Dialog dialog, int width, int height) {
100         Point computedSize =
101             dialog.getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
102         width = Math.max(computedSize.x, width);
103         height = Math.max(computedSize.y, height);
104         dialog.getShell().setSize(width, height);
105     }
106 }
107
Popular Tags