KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > parts > SWTUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.update.internal.ui.parts;
12 /**
13  * @version 1.0
14  * @author
15  */

16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jface.dialogs.*;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.resource.*;
20 import org.eclipse.swt.*;
21 import org.eclipse.swt.dnd.*;
22 import org.eclipse.swt.graphics.*;
23 import org.eclipse.swt.layout.*;
24 import org.eclipse.swt.widgets.*;
25
26 /**
27  * Utility class to simplify access to some SWT resources.
28  */

29 public class SWTUtil {
30
31     /**
32      * Returns the standard display to be used. The method first checks, if
33      * the thread calling this method has an associated disaply. If so, this
34      * display is returned. Otherwise the method returns the default display.
35      */

36     public static Display getStandardDisplay() {
37         Display display;
38         display = Display.getCurrent();
39         if (display == null)
40             display = Display.getDefault();
41         return display;
42     }
43
44     /**
45      * Returns the shell for the given widget. If the widget doesn't represent
46      * a SWT object that manage a shell, <code>null</code> is returned.
47      *
48      * @return the shell for the given widget
49      */

50     public static Shell getShell(Widget widget) {
51         if (widget instanceof Control)
52             return ((Control) widget).getShell();
53         if (widget instanceof Caret)
54             return ((Caret) widget).getParent().getShell();
55         if (widget instanceof DragSource)
56             return ((DragSource) widget).getControl().getShell();
57         if (widget instanceof DropTarget)
58             return ((DropTarget) widget).getControl().getShell();
59         if (widget instanceof Menu)
60             return ((Menu) widget).getParent().getShell();
61         if (widget instanceof ScrollBar)
62             return ((ScrollBar) widget).getParent().getShell();
63
64         return null;
65     }
66
67     /**
68      * Returns a width hint for a button control.
69      */

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

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