KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > widgets > SWTUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.internal.forms.widgets;
12 import org.eclipse.swt.dnd.DragSource;
13 import org.eclipse.swt.dnd.DropTarget;
14 import org.eclipse.swt.widgets.Caret;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.swt.widgets.Menu;
18 import org.eclipse.swt.widgets.ScrollBar;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.swt.widgets.Widget;
21
22 /**
23  * Utility class to simplify access to some SWT resources.
24  */

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

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

46     public static Shell getShell(Widget widget) {
47         if (widget instanceof Control)
48             return ((Control) widget).getShell();
49         if (widget instanceof Caret)
50             return ((Caret) widget).getParent().getShell();
51         if (widget instanceof DragSource)
52             return ((DragSource) widget).getControl().getShell();
53         if (widget instanceof DropTarget)
54             return ((DropTarget) widget).getControl().getShell();
55         if (widget instanceof Menu)
56             return ((Menu) widget).getParent().getShell();
57         if (widget instanceof ScrollBar)
58             return ((ScrollBar) widget).getParent().getShell();
59
60         return null;
61     }
62 }
63
Popular Tags