KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > WWinPluginPulldown


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.ui.internal;
12
13 import org.eclipse.core.runtime.IConfigurationElement;
14 import org.eclipse.core.runtime.ISafeRunnable;
15 import org.eclipse.core.runtime.SafeRunner;
16 import org.eclipse.jface.action.IMenuCreator;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Menu;
19 import org.eclipse.ui.IActionDelegate;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
22 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
23 import org.eclipse.ui.WorkbenchException;
24
25 /**
26  * A workbench window pulldown action.
27  */

28 public class WWinPluginPulldown extends WWinPluginAction {
29
30     /**
31      * The proxy for creating the menu. There is always a menu proxy. This value
32      * can't be <code>null</code>.
33      */

34     private final IMenuCreator menuProxy;
35
36     private class MenuProxy implements IMenuCreator {
37
38         /**
39          * A wrapper for loading the menu that defends against possible
40          * exceptions triggered outside of the workbench.
41          *
42          * @since 3.0
43          */

44         private class MenuLoader implements ISafeRunnable {
45
46             /**
47              * The parent for the menu to be created. This value is
48              * <code>null</code> if the parent is a menu.
49              */

50             private final Control control;
51
52             /**
53              * The delegate from which to load the menu.
54              */

55             private final IWorkbenchWindowPulldownDelegate delegate;
56
57             /**
58              * The loaded menu. This value is <code>null</code> if the load
59              * failed, or if it hasn't been loaded yet.
60              */

61             private Menu menu = null;
62
63             /**
64              * The parent for the menu to be created. This value is
65              * <code>null</code> if the parent is a control.
66              */

67             private final Menu parent;
68
69             /**
70              * Constructs a new instance of <code>MenuLoader</code>
71              *
72              * @param delegate
73              * The delegate from which the menu will be loaded; this
74              * value must not be <code>null</code>.
75              * @param parent
76              * The parent of the menu to be loaded; this value must
77              * not be <code>null</code>.
78              */

79             private MenuLoader(
80                     final IWorkbenchWindowPulldownDelegate2 delegate,
81                     final Menu parent) {
82                 this.delegate = delegate;
83                 this.parent = parent;
84                 this.control = null;
85             }
86
87             /**
88              * Constructs a new instance of <code>MenuLoader</code>
89              *
90              * @param delegate
91              * The delegate from which the menu will be loaded; this
92              * value must not be <code>null</code>.
93              * @param parent
94              * The parent of the menu to be loaded; this value must
95              * not be <code>null</code>.
96              */

97             private MenuLoader(final IWorkbenchWindowPulldownDelegate delegate,
98                     final Control parent) {
99                 this.delegate = delegate;
100                 this.parent = null;
101                 this.control = parent;
102             }
103
104             /**
105              * Returns the menu loaded, if any.
106              *
107              * @return the loaded menu, or <code>null</code> if none.
108              */

109             private Menu getMenu() {
110                 return menu;
111             }
112
113             /**
114              * @see ISafeRunnable#handleException(java.lang.Throwable)
115              */

116             public void handleException(Throwable JavaDoc exception) {
117                 // Do nothing
118
}
119
120             /**
121              * @see ISafeRunnable#run()
122              */

123             public void run() throws Exception JavaDoc {
124                 if (parent == null) {
125                     menu = delegate.getMenu(control);
126                 } else {
127                     menu = ((IWorkbenchWindowPulldownDelegate2) delegate)
128                             .getMenu(parent);
129                 }
130             }
131         }
132
133         /**
134          * @see IMenuCreator#getMenu(Control)
135          */

136         public Menu getMenu(Control parent) {
137             IWorkbenchWindowPulldownDelegate delegate = getPulldownDelegate();
138             if (delegate != null) {
139                 final MenuLoader menuLoader = new MenuLoader(delegate, parent);
140                 SafeRunner.run(menuLoader);
141                 return menuLoader.getMenu();
142             }
143
144             return null;
145         }
146
147         /**
148          * @see IMenuCreator#getMenu(Menu)
149          */

150         public Menu getMenu(Menu parent) {
151             IWorkbenchWindowPulldownDelegate delegate = getPulldownDelegate();
152
153             if (delegate instanceof IWorkbenchWindowPulldownDelegate2) {
154                 IWorkbenchWindowPulldownDelegate2 delegate2 = (IWorkbenchWindowPulldownDelegate2) delegate;
155                 final MenuLoader menuLoader = new MenuLoader(delegate2, parent);
156                 SafeRunner.run(menuLoader);
157                 return menuLoader.getMenu();
158             }
159
160             return null;
161         }
162
163         /**
164          * @see IMenuCreator#dispose()
165          */

166         public void dispose() {
167             // do nothing
168
}
169     }
170
171     /**
172      * Constructs a new instance of <code>WWinPluginPulldown</code>.
173      *
174      * @param actionElement
175      * The registry element from which the pulldown delegate should
176      * be created; must not be <code>null</code>.
177      * @param id
178      * The identifier of this action delegate; may be
179      * <code>null</code>.
180      * @param window
181      * The workbench window on which this pulldown should act; must
182      * not be <code>null</code>.
183      * @param style
184      * The style.
185      */

186     public WWinPluginPulldown(IConfigurationElement actionElement,
187             IWorkbenchWindow window, String JavaDoc id, int style) {
188         super(actionElement, window, id, style);
189         menuProxy = new MenuProxy();
190         setMenuCreator(menuProxy);
191     }
192
193     /*
194      * (non-Javadoc) Method declared on PluginAction.
195      */

196     protected IActionDelegate validateDelegate(Object JavaDoc obj)
197             throws WorkbenchException {
198         if (obj instanceof IWorkbenchWindowPulldownDelegate) {
199             return (IWorkbenchWindowPulldownDelegate) obj;
200         }
201
202         throw new WorkbenchException(
203                 "Action must implement IWorkbenchWindowPulldownDelegate"); //$NON-NLS-1$
204
}
205
206     /**
207      * Returns the pulldown delegate. If it does not exist it is created. Can
208      * return <code>null</code> if delegate creation failed.
209      */

210     protected IWorkbenchWindowPulldownDelegate getPulldownDelegate() {
211         IActionDelegate delegate = getDelegate();
212         if (delegate == null) {
213             createDelegate();
214             delegate = getDelegate();
215         }
216         return (IWorkbenchWindowPulldownDelegate) delegate;
217     }
218 }
219
Popular Tags