1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2003, 2009 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;
|
14
|
import java.util.Comparator;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.Iterator;
|
17
|
import java.util.Map;
|
18
|
import java.util.SortedSet;
|
19
|
import java.util.TreeMap;
|
20
|
import java.util.TreeSet;
|
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
|
*
|
68
|
* @see PlatformUI#getWorkbench()
|
69
|
* @see IWorkbench#getNewWizardRegistry()
|
70
|
* @see IWorkbench#getImportWizardRegistry()
|
71
|
* @see IWorkbench#getExportWizardRegistry()
|
72
|
* @since 3.2
|
73
|
*
|
74
|
*/
|
75
|
public final class WizardActionGroup extends ActionGroup {
|
76
|
|
77
|
/**
|
78
|
* The type for commonWizard extensions with the value "new" for their type
|
79
|
* attribute.
|
80
|
*/
|
81
|
public static final String TYPE_NEW = "new"; //$NON-NLS-1$
|
82
|
|
83
|
/**
|
84
|
* The type for commonWizard extensions with the value "new" for their type
|
85
|
* attribute.
|
86
|
*/
|
87
|
public static final String TYPE_IMPORT = "import"; //$NON-NLS-1$
|
88
|
|
89
|
/**
|
90
|
* The type for commonWizard extensions with the value "new" for their type
|
91
|
* attribute.
|
92
|
*/
|
93
|
public static final String TYPE_EXPORT = "export"; //$NON-NLS-1$
|
94
|
|
95
|
private static final CommonWizardDescriptor[] NO_DESCRIPTORS = new CommonWizardDescriptor[0];
|
96
|
|
97
|
private static final String[] NO_IDS = new String[0];
|
98
|
|
99
|
private CommonWizardDescriptor[] descriptors;
|
100
|
|
101
|
/* a map of (id, IAction)-pairs. */
|
102
|
private Map actions;
|
103
|
|
104
|
/*
|
105
|
* the window is passed to created WizardShortcutActions for the shell and
|
106
|
* selection service.
|
107
|
*/
|
108
|
private IWorkbenchWindow window;
|
109
|
|
110
|
/* the correct wizard registry for this action group (getRegistry()) */
|
111
|
private IWizardRegistry wizardRegistry;
|
112
|
|
113
|
private boolean disposed = false;
|
114
|
|
115
|
private String type;
|
116
|
|
117
|
private INavigatorContentService contentService;
|
118
|
|
119
|
/**
|
120
|
*
|
121
|
* @param aWindow
|
122
|
* The window that will be used to acquire a Shell and a
|
123
|
* Selection Service
|
124
|
* @param aWizardRegistry
|
125
|
* The wizard registry will be used to locate the correct wizard
|
126
|
* descriptions.
|
127
|
* @param aType
|
128
|
* Indicates the value of the type attribute of the commonWizard
|
129
|
* extension point. Use any of the TYPE_XXX constants defined on
|
130
|
* this class.
|
131
|
* @see PlatformUI#getWorkbench()
|
132
|
* @see IWorkbench#getNewWizardRegistry()
|
133
|
* @see IWorkbench#getImportWizardRegistry()
|
134
|
* @see IWorkbench#getExportWizardRegistry()
|
135
|
*/
|
136
|
public WizardActionGroup(IWorkbenchWindow aWindow,
|
137
|
IWizardRegistry aWizardRegistry, String aType) {
|
138
|
super();
|
139
|
Assert.isNotNull(aWindow);
|
140
|
Assert.isNotNull(aWizardRegistry);
|
141
|
Assert
|
142
|
.isTrue(aType != null
|
143
|
&& (TYPE_NEW.equals(aType) || TYPE_IMPORT.equals(aType) || TYPE_EXPORT
|
144
|
.equals(aType)));
|
145
|
window = aWindow;
|
146
|
wizardRegistry = aWizardRegistry;
|
147
|
type = aType;
|
148
|
|
149
|
}
|
150
|
|
151
|
|
152
|
/**
|
153
|
*
|
154
|
* @param aWindow
|
155
|
* The window that will be used to acquire a Shell and a
|
156
|
* Selection Service
|
157
|
* @param aWizardRegistry
|
158
|
* The wizard registry will be used to locate the correct wizard
|
159
|
* descriptions.
|
160
|
* @param aType
|
161
|
* Indicates the value of the type attribute of the commonWizard
|
162
|
* extension point. Use any of the TYPE_XXX constants defined on
|
163
|
* this class.
|
164
|
* @param aContentService
|
165
|
* The content service to use when deciding visibility.
|
166
|
* @see PlatformUI#getWorkbench()
|
167
|
* @see IWorkbench#getNewWizardRegistry()
|
168
|
* @see IWorkbench#getImportWizardRegistry()
|
169
|
* @see IWorkbench#getExportWizardRegistry()
|
170
|
*/
|
171
|
public WizardActionGroup(IWorkbenchWindow aWindow,
|
172
|
IWizardRegistry aWizardRegistry, String aType, INavigatorContentService aContentService) {
|
173
|
this(aWindow, aWizardRegistry, aType);
|
174
|
contentService = aContentService;
|
175
|
|
176
|
}
|
177
|
|
178
|
public void setContext(ActionContext aContext) {
|
179
|
Assert.isTrue(!disposed);
|
180
|
|
181
|
super.setContext(aContext);
|
182
|
if (aContext != null) {
|
183
|
ISelection selection = aContext.getSelection();
|
184
|
Object element = null;
|
185
|
if (selection instanceof IStructuredSelection) {
|
186
|
element = ((IStructuredSelection) selection).getFirstElement();
|
187
|
}
|
188
|
if(element == null) {
|
189
|
element = Collections.EMPTY_LIST;
|
190
|
}
|
191
|
// null should be okay here
|
192
|
setWizardActionDescriptors(CommonWizardDescriptorManager.getInstance()
|
193
|
.getEnabledCommonWizardDescriptors(element, type, contentService));
|
194
|
} else {
|
195
|
setWizardActionDescriptors(NO_DESCRIPTORS);
|
196
|
}
|
197
|
}
|
198
|
|
199
|
/*
|
200
|
* (non-Javadoc)
|
201
|
*
|
202
|
* @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
|
203
|
*/
|
204
|
public void fillContextMenu(IMenuManager menu) {
|
205
|
Assert.isTrue(!disposed);
|
206
|
|
207
|
if (descriptors != null) {
|
208
|
Map groups = findGroups();
|
209
|
SortedSet sortedWizards = null;
|
210
|
String menuGroupId = null;
|
211
|
for (Iterator menuGroupItr = groups.keySet().iterator(); menuGroupItr.hasNext();) {
|
212
|
menuGroupId = (String) menuGroupItr.next();
|
213
|
sortedWizards = (SortedSet) groups.get(menuGroupId);
|
214
|
menu.add(new Separator(menuGroupId));
|
215
|
for (Iterator wizardItr = sortedWizards.iterator(); wizardItr.hasNext();) {
|
216
|
menu.add((IAction) wizardItr.next());
|
217
|
}
|
218
|
}
|
219
|
}
|
220
|
}
|
221
|
|
222
|
/**
|
223
|
* @return A Map of menuGroupIds to SortedSets of IActions.
|
224
|
*/
|
225
|
private synchronized Map/*<String, SortedSet<IAction>>*/ findGroups() {
|
226
|
IAction action = null;
|
227
|
Map groups = new TreeMap();
|
228
|
SortedSet sortedWizards = null;
|
229
|
String menuGroupId = null;
|
230
|
for (int i = 0; i < descriptors.length; i++) {
|
231
|
menuGroupId = descriptors[i].getMenuGroupId() != null ?
|
232
|
descriptors[i].getMenuGroupId() : CommonWizardDescriptor.DEFAULT_MENU_GROUP_ID;
|
233
|
sortedWizards = (SortedSet) groups.get(menuGroupId);
|
234
|
if(sortedWizards == null) {
|
235
|
groups.put(descriptors[i].getMenuGroupId(), sortedWizards = new TreeSet(ActionComparator.INSTANCE));
|
236
|
}
|
237
|
if ((action = getAction(descriptors[i].getWizardId())) != null) {
|
238
|
sortedWizards.add(action);
|
239
|
}
|
240
|
}
|
241
|
return groups;
|
242
|
}
|
243
|
|
244
|
|
245
|
public void dispose() {
|
246
|
super.dispose();
|
247
|
actions = null;
|
248
|
window = null;
|
249
|
descriptors = null;
|
250
|
wizardRegistry = null;
|
251
|
disposed = true;
|
252
|
}
|
253
|
|
254
|
/*
|
255
|
* (non-Javadoc) Returns the action for the given wizard id, or null if not
|
256
|
* found.
|
257
|
*/
|
258
|
protected IAction getAction(String id) {
|
259
|
if (id == null || id.length() == 0) {
|
260
|
return null;
|
261
|
}
|
262
|
|
263
|
// Keep a cache, rather than creating a new action each time,
|
264
|
// so that image caching in ActionContributionItem works.
|
265
|
IAction action = (IAction) getActions().get(id);
|
266
|
if (action == null) {
|
267
|
IWizardDescriptor descriptor = wizardRegistry.findWizard(id);
|
268
|
if (descriptor != null) {
|
269
|
action = new WizardShortcutAction(window, descriptor);
|
270
|
getActions().put(id, action);
|
271
|
}
|
272
|
}
|
273
|
|
274
|
return action;
|
275
|
}
|
276
|
|
277
|
/**
|
278
|
* @return a map of (id, IAction)-pairs.
|
279
|
*/
|
280
|
protected Map getActions() {
|
281
|
if (actions == null) {
|
282
|
actions = new HashMap();
|
283
|
}
|
284
|
return actions;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* @return Returns the wizardActionIds.
|
289
|
*/
|
290
|
public synchronized String[] getWizardActionIds() {
|
291
|
if(descriptors != null && descriptors.length > 0) {
|
292
|
String[] wizardActionIds = new String[descriptors.length];
|
293
|
for (int i = 0; i < descriptors.length; i++) {
|
294
|
wizardActionIds[i] = descriptors[i].getWizardId();
|
295
|
}
|
296
|
return wizardActionIds;
|
297
|
}
|
298
|
return NO_IDS;
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* @param theWizardDescriptors
|
303
|
* The wizard action ids to set. These should be defined through
|
304
|
* <b>org.eclipse.ui.xxxWizards</b>
|
305
|
*/
|
306
|
private synchronized void setWizardActionDescriptors(CommonWizardDescriptor[] theWizardDescriptors) {
|
307
|
descriptors = theWizardDescriptors;
|
308
|
}
|
309
|
|
310
|
private static class ActionComparator implements Comparator {
|
311
|
|
312
|
private static final ActionComparator INSTANCE = new ActionComparator();
|
313
|
/* (non-Javadoc)
|
314
|
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
315
|
*/
|
316
|
public int compare(Object arg0, Object arg1) {
|
317
|
return ((IAction)arg0).getText().compareTo(((IAction)arg1).getText());
|
318
|
}
|
319
|
}
|
320
|
}
|