KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > navigator > WizardActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.navigator;
12
13 import java.util.Collections JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.SortedSet JavaDoc;
19 import java.util.TreeMap JavaDoc;
20 import java.util.TreeSet JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.action.IMenuManager;
25 import org.eclipse.jface.action.Separator;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.ui.IWorkbench;
29 import org.eclipse.ui.IWorkbenchWindow;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.actions.ActionContext;
32 import org.eclipse.ui.actions.ActionGroup;
33 import org.eclipse.ui.internal.navigator.wizards.CommonWizardDescriptor;
34 import org.eclipse.ui.internal.navigator.wizards.CommonWizardDescriptorManager;
35 import org.eclipse.ui.internal.navigator.wizards.WizardShortcutAction;
36 import org.eclipse.ui.wizards.IWizardDescriptor;
37 import org.eclipse.ui.wizards.IWizardRegistry;
38
39 /**
40  *
41  * Populates context menus with shortcut actions for defined wizards. Wizards
42  * may be defined by any of the following extension points:
43  * <p>
44  * <ul>
45  * <li><b>org.eclipse.ui.newWizards</b></li>
46  * <li><b>org.eclipse.ui.importWizards</b></li>
47  * <li><b>org.eclipse.ui.exportWizards</b></li>
48  * </ul>
49  * </p>
50  * <p>
51  * Here are the required steps for using this feature correctly:
52  * <ol>
53  * <li>Declare all new/import/export wizards from the extension points above,
54  * or locate the existing wizards that you intend to reuse.</li>
55  * <li>Declare <b>org.eclipse.ui.navigator.navigatorContent/commonWizard</b>
56  * elements to identify which wizards should be associated with what items in
57  * your viewer or navigator.</li>
58  * <li>If you are using Resources in your viewer and have bound the resource
59  * extension declared in <b>org.eclipse.ui.navigator.resources</b>, then you
60  * will get most of this functionality for free.</li>
61  * <li>Otherwise, you may choose to build your own custom menu. In which case,
62  * you may instantiate this class, and hand it the menu or submenu that you want
63  * to list out the available wizard shortcuts via
64  * {@link WizardActionGroup#fillContextMenu(IMenuManager)}.</li>
65  * </ol>
66  * </p>
67  * <p>
68  * Clients may instantiate, but not subclass WizardActionGroup.
69  * </p>
70  *
71  * @see PlatformUI#getWorkbench()
72  * @see IWorkbench#getNewWizardRegistry()
73  * @see IWorkbench#getImportWizardRegistry()
74  * @see IWorkbench#getExportWizardRegistry()
75  * @since 3.2
76  *
77  */

78 public final class WizardActionGroup extends ActionGroup {
79
80     /**
81      * The type for commonWizard extensions with the value "new" for their type
82      * attribute.
83      */

84     public static final String JavaDoc TYPE_NEW = "new"; //$NON-NLS-1$
85

86     /**
87      * The type for commonWizard extensions with the value "new" for their type
88      * attribute.
89      */

90     public static final String JavaDoc TYPE_IMPORT = "import"; //$NON-NLS-1$
91

92     /**
93      * The type for commonWizard extensions with the value "new" for their type
94      * attribute.
95      */

96     public static final String JavaDoc TYPE_EXPORT = "export"; //$NON-NLS-1$
97

98     private static final CommonWizardDescriptor[] NO_DESCRIPTORS = new CommonWizardDescriptor[0];
99     
100     private static final String JavaDoc[] NO_IDS = new String JavaDoc[0];
101     
102     private CommonWizardDescriptor[] descriptors;
103
104     /* a map of (id, IAction)-pairs. */
105     private Map JavaDoc actions;
106
107     /*
108      * the window is passed to created WizardShortcutActions for the shell and
109      * selection service.
110      */

111     private IWorkbenchWindow window;
112
113     /* the correct wizard registry for this action group (getRegistry()) */
114     private IWizardRegistry wizardRegistry;
115
116     private boolean disposed = false;
117
118     private String JavaDoc type;
119
120     private INavigatorContentService contentService;
121
122     /**
123      *
124      * @param aWindow
125      * The window that will be used to acquire a Shell and a
126      * Selection Service
127      * @param aWizardRegistry
128      * The wizard registry will be used to locate the correct wizard
129      * descriptions.
130      * @param aType
131      * Indicates the value of the type attribute of the commonWizard
132      * extension point. Use any of the TYPE_XXX constants defined on
133      * this class.
134      * @see PlatformUI#getWorkbench()
135      * @see IWorkbench#getNewWizardRegistry()
136      * @see IWorkbench#getImportWizardRegistry()
137      * @see IWorkbench#getExportWizardRegistry()
138      */

139     public WizardActionGroup(IWorkbenchWindow aWindow,
140             IWizardRegistry aWizardRegistry, String JavaDoc aType) {
141         super();
142         Assert.isNotNull(aWindow);
143         Assert.isNotNull(aWizardRegistry);
144         Assert
145                 .isTrue(aType != null
146                         && (TYPE_NEW.equals(aType) || TYPE_IMPORT.equals(aType) || TYPE_EXPORT
147                                 .equals(aType)));
148         window = aWindow;
149         wizardRegistry = aWizardRegistry;
150         type = aType;
151
152     }
153     
154
155     /**
156      *
157      * @param aWindow
158      * The window that will be used to acquire a Shell and a
159      * Selection Service
160      * @param aWizardRegistry
161      * The wizard registry will be used to locate the correct wizard
162      * descriptions.
163      * @param aType
164      * Indicates the value of the type attribute of the commonWizard
165      * extension point. Use any of the TYPE_XXX constants defined on
166      * this class.
167      * @param aContentService
168      * The content service to use when deciding visibility.
169      * @see PlatformUI#getWorkbench()
170      * @see IWorkbench#getNewWizardRegistry()
171      * @see IWorkbench#getImportWizardRegistry()
172      * @see IWorkbench#getExportWizardRegistry()
173      */

174     public WizardActionGroup(IWorkbenchWindow aWindow,
175             IWizardRegistry aWizardRegistry, String JavaDoc aType, INavigatorContentService aContentService) {
176         this(aWindow, aWizardRegistry, aType);
177         contentService = aContentService;
178
179     }
180
181     public void setContext(ActionContext aContext) {
182         Assert.isTrue(!disposed);
183
184         super.setContext(aContext);
185         if (aContext != null) {
186             ISelection selection = aContext.getSelection();
187             Object JavaDoc element = null;
188             if (selection instanceof IStructuredSelection) {
189                 element = ((IStructuredSelection) selection).getFirstElement();
190             }
191             if(element == null) {
192                 element = Collections.EMPTY_LIST;
193             }
194             // null should be okay here
195
setWizardActionDescriptors(CommonWizardDescriptorManager.getInstance()
196                     .getEnabledCommonWizardDescriptors(element, type, contentService));
197         } else {
198             setWizardActionDescriptors(NO_DESCRIPTORS);
199         }
200     }
201
202     /*
203      * (non-Javadoc)
204      *
205      * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
206      */

207     public void fillContextMenu(IMenuManager menu) {
208         Assert.isTrue(!disposed);
209  
210         if (descriptors != null) {
211             Map JavaDoc groups = findGroups();
212             SortedSet JavaDoc sortedWizards = null;
213             String JavaDoc menuGroupId = null;
214             for (Iterator JavaDoc menuGroupItr = groups.keySet().iterator(); menuGroupItr.hasNext();) {
215                 menuGroupId = (String JavaDoc) menuGroupItr.next();
216                 sortedWizards = (SortedSet JavaDoc) groups.get(menuGroupId);
217                 menu.add(new Separator(menuGroupId));
218                 for (Iterator JavaDoc wizardItr = sortedWizards.iterator(); wizardItr.hasNext();) {
219                     menu.add((IAction) wizardItr.next());
220                 }
221             }
222         }
223     }
224
225     /**
226      * @return A Map of menuGroupIds to SortedSets of IActions.
227      */

228     private synchronized Map JavaDoc/*<String, SortedSet<IAction>>*/ findGroups() {
229         IAction action = null;
230         Map JavaDoc groups = new TreeMap JavaDoc();
231         SortedSet JavaDoc sortedWizards = null;
232         String JavaDoc menuGroupId = null;
233         for (int i = 0; i < descriptors.length; i++) {
234             menuGroupId = descriptors[i].getMenuGroupId() != null ?
235                             descriptors[i].getMenuGroupId() : CommonWizardDescriptor.DEFAULT_MENU_GROUP_ID;
236             sortedWizards = (SortedSet JavaDoc) groups.get(menuGroupId);
237             if(sortedWizards == null) {
238                 groups.put(descriptors[i].getMenuGroupId(), sortedWizards = new TreeSet JavaDoc(ActionComparator.INSTANCE));
239             }
240             if ((action = getAction(descriptors[i].getWizardId())) != null) {
241                 sortedWizards.add(action);
242             }
243         }
244         return groups;
245     }
246
247
248     public void dispose() {
249         super.dispose();
250         actions = null;
251         window = null;
252         descriptors = null;
253         wizardRegistry = null;
254         disposed = true;
255     }
256
257     /*
258      * (non-Javadoc) Returns the action for the given wizard id, or null if not
259      * found.
260      */

261     protected IAction getAction(String JavaDoc id) {
262         if (id == null || id.length() == 0) {
263             return null;
264         }
265
266         // Keep a cache, rather than creating a new action each time,
267
// so that image caching in ActionContributionItem works.
268
IAction action = (IAction) getActions().get(id);
269         if (action == null) {
270             IWizardDescriptor descriptor = wizardRegistry.findWizard(id);
271             if (descriptor != null) {
272                 action = new WizardShortcutAction(window, descriptor);
273                 getActions().put(id, action);
274             }
275         }
276
277         return action;
278     }
279
280     /**
281      * @return a map of (id, IAction)-pairs.
282      */

283     protected Map JavaDoc getActions() {
284         if (actions == null) {
285             actions = new HashMap JavaDoc();
286         }
287         return actions;
288     }
289
290     /**
291      * @return Returns the wizardActionIds.
292      */

293     public synchronized String JavaDoc[] getWizardActionIds() {
294         if(descriptors != null && descriptors.length > 0) {
295             String JavaDoc[] wizardActionIds = new String JavaDoc[descriptors.length];
296             for (int i = 0; i < descriptors.length; i++) {
297                 wizardActionIds[i] = descriptors[i].getWizardId();
298             }
299             return wizardActionIds;
300         }
301         return NO_IDS;
302     }
303
304     /**
305      * @param theWizardDescriptors
306      * The wizard action ids to set. These should be defined through
307      * <b>org.eclipse.ui.xxxWizards</b>
308      */

309     protected synchronized void setWizardActionDescriptors(CommonWizardDescriptor[] theWizardDescriptors) {
310         descriptors = theWizardDescriptors;
311     }
312       
313     private static class ActionComparator implements Comparator JavaDoc {
314         
315         private static final ActionComparator INSTANCE = new ActionComparator();
316         /* (non-Javadoc)
317          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
318          */

319         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
320             return ((IAction)arg0).getText().compareTo(((IAction)arg1).getText());
321         }
322     }
323 }
324
Popular Tags