1 | /** |
2 | * <copyright> |
3 | * </copyright> |
4 | * |
5 | * $Id$ |
6 | */ |
7 | package de.uka.ipd.sdq.pcm.resultdecorator.resourceenvironmentdecorator.presentation; |
8 | |
9 | import java.util.ArrayList; |
10 | import java.util.Collection; |
11 | |
12 | import org.eclipse.emf.common.ui.viewer.IViewerProvider; |
13 | import org.eclipse.emf.edit.domain.EditingDomain; |
14 | import org.eclipse.emf.edit.domain.IEditingDomainProvider; |
15 | import org.eclipse.emf.edit.ui.action.ControlAction; |
16 | import org.eclipse.emf.edit.ui.action.CreateChildAction; |
17 | import org.eclipse.emf.edit.ui.action.CreateSiblingAction; |
18 | import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; |
19 | import org.eclipse.emf.edit.ui.action.LoadResourceAction; |
20 | import org.eclipse.emf.edit.ui.action.ValidateAction; |
21 | import org.eclipse.jface.action.Action; |
22 | import org.eclipse.jface.action.ActionContributionItem; |
23 | import org.eclipse.jface.action.IAction; |
24 | import org.eclipse.jface.action.IContributionItem; |
25 | import org.eclipse.jface.action.IContributionManager; |
26 | import org.eclipse.jface.action.IMenuListener; |
27 | import org.eclipse.jface.action.IMenuManager; |
28 | import org.eclipse.jface.action.IToolBarManager; |
29 | import org.eclipse.jface.action.MenuManager; |
30 | import org.eclipse.jface.action.Separator; |
31 | import org.eclipse.jface.action.SubContributionItem; |
32 | import org.eclipse.jface.viewers.ISelection; |
33 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
34 | import org.eclipse.jface.viewers.ISelectionProvider; |
35 | import org.eclipse.jface.viewers.IStructuredSelection; |
36 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
37 | import org.eclipse.jface.viewers.Viewer; |
38 | import org.eclipse.ui.IEditorPart; |
39 | import org.eclipse.ui.PartInitException; |
40 | |
41 | import de.uka.ipd.sdq.pcm.resultdecorator.presentation.ResultdecoratorEditorPlugin; |
42 | |
43 | /** |
44 | * This is the action bar contributor for the Resourceenvironmentdecorator model editor. |
45 | * <!-- begin-user-doc --> |
46 | * <!-- end-user-doc --> |
47 | * @generated |
48 | */ |
49 | public class ResourceenvironmentdecoratorActionBarContributor |
50 | extends EditingDomainActionBarContributor |
51 | implements ISelectionChangedListener { |
52 | /** |
53 | * This keeps track of the active editor. |
54 | * <!-- begin-user-doc --> |
55 | * <!-- end-user-doc --> |
56 | * @generated |
57 | */ |
58 | protected IEditorPart activeEditorPart; |
59 | |
60 | /** |
61 | * This keeps track of the current selection provider. |
62 | * <!-- begin-user-doc --> |
63 | * <!-- end-user-doc --> |
64 | * @generated |
65 | */ |
66 | protected ISelectionProvider selectionProvider; |
67 | |
68 | /** |
69 | * This action opens the Properties view. |
70 | * <!-- begin-user-doc --> |
71 | * <!-- end-user-doc --> |
72 | * @generated |
73 | */ |
74 | protected IAction showPropertiesViewAction = |
75 | new Action(ResultdecoratorEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) { |
76 | @Override |
77 | public void run() { |
78 | try { |
79 | getPage().showView("org.eclipse.ui.views.PropertySheet"); |
80 | } |
81 | catch (PartInitException exception) { |
82 | ResultdecoratorEditorPlugin.INSTANCE.log(exception); |
83 | } |
84 | } |
85 | }; |
86 | |
87 | /** |
88 | * This action refreshes the viewer of the current editor if the editor |
89 | * implements {@link org.eclipse.emf.common.ui.viewer.IViewerProvider}. |
90 | * <!-- begin-user-doc --> |
91 | * <!-- end-user-doc --> |
92 | * @generated |
93 | */ |
94 | protected IAction refreshViewerAction = |
95 | new Action(ResultdecoratorEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) { |
96 | @Override |
97 | public boolean isEnabled() { |
98 | return activeEditorPart instanceof IViewerProvider; |
99 | } |
100 | |
101 | @Override |
102 | public void run() { |
103 | if (activeEditorPart instanceof IViewerProvider) { |
104 | Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer(); |
105 | if (viewer != null) { |
106 | viewer.refresh(); |
107 | } |
108 | } |
109 | } |
110 | }; |
111 | |
112 | /** |
113 | * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateChildAction} corresponding to each descriptor |
114 | * generated for the current selection by the item provider. |
115 | * <!-- begin-user-doc --> |
116 | * <!-- end-user-doc --> |
117 | * @generated |
118 | */ |
119 | protected Collection<IAction> createChildActions; |
120 | |
121 | /** |
122 | * This is the menu manager into which menu contribution items should be added for CreateChild actions. |
123 | * <!-- begin-user-doc --> |
124 | * <!-- end-user-doc --> |
125 | * @generated |
126 | */ |
127 | protected IMenuManager createChildMenuManager; |
128 | |
129 | /** |
130 | * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} corresponding to each descriptor |
131 | * generated for the current selection by the item provider. |
132 | * <!-- begin-user-doc --> |
133 | * <!-- end-user-doc --> |
134 | * @generated |
135 | */ |
136 | protected Collection<IAction> createSiblingActions; |
137 | |
138 | /** |
139 | * This is the menu manager into which menu contribution items should be added for CreateSibling actions. |
140 | * <!-- begin-user-doc --> |
141 | * <!-- end-user-doc --> |
142 | * @generated |
143 | */ |
144 | protected IMenuManager createSiblingMenuManager; |
145 | |
146 | /** |
147 | * This creates an instance of the contributor. |
148 | * <!-- begin-user-doc --> |
149 | * <!-- end-user-doc --> |
150 | * @generated |
151 | */ |
152 | public ResourceenvironmentdecoratorActionBarContributor() { |
153 | super(ADDITIONS_LAST_STYLE); |
154 | loadResourceAction = new LoadResourceAction(); |
155 | validateAction = new ValidateAction(); |
156 | controlAction = new ControlAction(); |
157 | } |
158 | |
159 | /** |
160 | * This adds Separators for editor additions to the tool bar. |
161 | * <!-- begin-user-doc --> |
162 | * <!-- end-user-doc --> |
163 | * @generated |
164 | */ |
165 | @Override |
166 | public void contributeToToolBar(IToolBarManager toolBarManager) { |
167 | toolBarManager.add(new Separator("resourceenvironmentdecorator-settings")); |
168 | toolBarManager.add(new Separator("resourceenvironmentdecorator-additions")); |
169 | } |
170 | |
171 | /** |
172 | * This adds to the menu bar a menu and some separators for editor additions, |
173 | * as well as the sub-menus for object creation items. |
174 | * <!-- begin-user-doc --> |
175 | * <!-- end-user-doc --> |
176 | * @generated |
177 | */ |
178 | @Override |
179 | public void contributeToMenu(IMenuManager menuManager) { |
180 | super.contributeToMenu(menuManager); |
181 | |
182 | IMenuManager submenuManager = new MenuManager(ResultdecoratorEditorPlugin.INSTANCE.getString("_UI_ResourceenvironmentdecoratorEditor_menu"), "de.uka.ipd.sdq.pcm.resultdecorator.resourceenvironmentdecoratorMenuID"); |
183 | menuManager.insertAfter("additions", submenuManager); |
184 | submenuManager.add(new Separator("settings")); |
185 | submenuManager.add(new Separator("actions")); |
186 | submenuManager.add(new Separator("additions")); |
187 | submenuManager.add(new Separator("additions-end")); |
188 | |
189 | // Prepare for CreateChild item addition or removal. |
190 | // |
191 | createChildMenuManager = new MenuManager(ResultdecoratorEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); |
192 | submenuManager.insertBefore("additions", createChildMenuManager); |
193 | |
194 | // Prepare for CreateSibling item addition or removal. |
195 | // |
196 | createSiblingMenuManager = new MenuManager(ResultdecoratorEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); |
197 | submenuManager.insertBefore("additions", createSiblingMenuManager); |
198 | |
199 | // Force an update because Eclipse hides empty menus now. |
200 | // |
201 | submenuManager.addMenuListener |
202 | (new IMenuListener() { |
203 | public void menuAboutToShow(IMenuManager menuManager) { |
204 | menuManager.updateAll(true); |
205 | } |
206 | }); |
207 | |
208 | addGlobalActions(submenuManager); |
209 | } |
210 | |
211 | /** |
212 | * When the active editor changes, this remembers the change and registers with it as a selection provider. |
213 | * <!-- begin-user-doc --> |
214 | * <!-- end-user-doc --> |
215 | * @generated |
216 | */ |
217 | @Override |
218 | public void setActiveEditor(IEditorPart part) { |
219 | super.setActiveEditor(part); |
220 | activeEditorPart = part; |
221 | |
222 | // Switch to the new selection provider. |
223 | // |
224 | if (selectionProvider != null) { |
225 | selectionProvider.removeSelectionChangedListener(this); |
226 | } |
227 | if (part == null) { |
228 | selectionProvider = null; |
229 | } |
230 | else { |
231 | selectionProvider = part.getSite().getSelectionProvider(); |
232 | selectionProvider.addSelectionChangedListener(this); |
233 | |
234 | // Fake a selection changed event to update the menus. |
235 | // |
236 | if (selectionProvider.getSelection() != null) { |
237 | selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection())); |
238 | } |
239 | } |
240 | } |
241 | |
242 | /** |
243 | * This implements {@link org.eclipse.jface.viewers.ISelectionChangedListener}, |
244 | * handling {@link org.eclipse.jface.viewers.SelectionChangedEvent}s by querying for the children and siblings |
245 | * that can be added to the selected object and updating the menus accordingly. |
246 | * <!-- begin-user-doc --> |
247 | * <!-- end-user-doc --> |
248 | * @generated |
249 | */ |
250 | public void selectionChanged(SelectionChangedEvent event) { |
251 | // Remove any menu items for old selection. |
252 | // |
253 | if (createChildMenuManager != null) { |
254 | depopulateManager(createChildMenuManager, createChildActions); |
255 | } |
256 | if (createSiblingMenuManager != null) { |
257 | depopulateManager(createSiblingMenuManager, createSiblingActions); |
258 | } |
259 | |
260 | // Query the new selection for appropriate new child/sibling descriptors |
261 | // |
262 | Collection<?> newChildDescriptors = null; |
263 | Collection<?> newSiblingDescriptors = null; |
264 | |
265 | ISelection selection = event.getSelection(); |
266 | if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) { |
267 | Object object = ((IStructuredSelection)selection).getFirstElement(); |
268 | |
269 | EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain(); |
270 | |
271 | newChildDescriptors = domain.getNewChildDescriptors(object, null); |
272 | newSiblingDescriptors = domain.getNewChildDescriptors(null, object); |
273 | } |
274 | |
275 | // Generate actions for selection; populate and redraw the menus. |
276 | // |
277 | createChildActions = generateCreateChildActions(newChildDescriptors, selection); |
278 | createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection); |
279 | |
280 | if (createChildMenuManager != null) { |
281 | populateManager(createChildMenuManager, createChildActions, null); |
282 | createChildMenuManager.update(true); |
283 | } |
284 | if (createSiblingMenuManager != null) { |
285 | populateManager(createSiblingMenuManager, createSiblingActions, null); |
286 | createSiblingMenuManager.update(true); |
287 | } |
288 | } |
289 | |
290 | /** |
291 | * This generates a {@link org.eclipse.emf.edit.ui.action.CreateChildAction} for each object in <code>descriptors</code>, |
292 | * and returns the collection of these actions. |
293 | * <!-- begin-user-doc --> |
294 | * <!-- end-user-doc --> |
295 | * @generated |
296 | */ |
297 | protected Collection<IAction> generateCreateChildActions(Collection<?> descriptors, ISelection selection) { |
298 | Collection<IAction> actions = new ArrayList<IAction>(); |
299 | if (descriptors != null) { |
300 | for (Object descriptor : descriptors) { |
301 | actions.add(new CreateChildAction(activeEditorPart, selection, descriptor)); |
302 | } |
303 | } |
304 | return actions; |
305 | } |
306 | |
307 | /** |
308 | * This generates a {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} for each object in <code>descriptors</code>, |
309 | * and returns the collection of these actions. |
310 | * <!-- begin-user-doc --> |
311 | * <!-- end-user-doc --> |
312 | * @generated |
313 | */ |
314 | protected Collection<IAction> generateCreateSiblingActions(Collection<?> descriptors, ISelection selection) { |
315 | Collection<IAction> actions = new ArrayList<IAction>(); |
316 | if (descriptors != null) { |
317 | for (Object descriptor : descriptors) { |
318 | actions.add(new CreateSiblingAction(activeEditorPart, selection, descriptor)); |
319 | } |
320 | } |
321 | return actions; |
322 | } |
323 | |
324 | /** |
325 | * This populates the specified <code>manager</code> with {@link org.eclipse.jface.action.ActionContributionItem}s |
326 | * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection, |
327 | * by inserting them before the specified contribution item <code>contributionID</code>. |
328 | * If <code>contributionID</code> is <code>null</code>, they are simply added. |
329 | * <!-- begin-user-doc --> |
330 | * <!-- end-user-doc --> |
331 | * @generated |
332 | */ |
333 | protected void populateManager(IContributionManager manager, Collection<? extends IAction> actions, String contributionID) { |
334 | if (actions != null) { |
335 | for (IAction action : actions) { |
336 | if (contributionID != null) { |
337 | manager.insertBefore(contributionID, action); |
338 | } |
339 | else { |
340 | manager.add(action); |
341 | } |
342 | } |
343 | } |
344 | } |
345 | |
346 | /** |
347 | * This removes from the specified <code>manager</code> all {@link org.eclipse.jface.action.ActionContributionItem}s |
348 | * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection. |
349 | * <!-- begin-user-doc --> |
350 | * <!-- end-user-doc --> |
351 | * @generated |
352 | */ |
353 | protected void depopulateManager(IContributionManager manager, Collection<? extends IAction> actions) { |
354 | if (actions != null) { |
355 | IContributionItem[] items = manager.getItems(); |
356 | for (int i = 0; i < items.length; i++) { |
357 | // Look into SubContributionItems |
358 | // |
359 | IContributionItem contributionItem = items[i]; |
360 | while (contributionItem instanceof SubContributionItem) { |
361 | contributionItem = ((SubContributionItem)contributionItem).getInnerItem(); |
362 | } |
363 | |
364 | // Delete the ActionContributionItems with matching action. |
365 | // |
366 | if (contributionItem instanceof ActionContributionItem) { |
367 | IAction action = ((ActionContributionItem)contributionItem).getAction(); |
368 | if (actions.contains(action)) { |
369 | manager.remove(contributionItem); |
370 | } |
371 | } |
372 | } |
373 | } |
374 | } |
375 | |
376 | /** |
377 | * This populates the pop-up menu before it appears. |
378 | * <!-- begin-user-doc --> |
379 | * <!-- end-user-doc --> |
380 | * @generated |
381 | */ |
382 | @Override |
383 | public void menuAboutToShow(IMenuManager menuManager) { |
384 | super.menuAboutToShow(menuManager); |
385 | MenuManager submenuManager = null; |
386 | |
387 | submenuManager = new MenuManager(ResultdecoratorEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); |
388 | populateManager(submenuManager, createChildActions, null); |
389 | menuManager.insertBefore("edit", submenuManager); |
390 | |
391 | submenuManager = new MenuManager(ResultdecoratorEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); |
392 | populateManager(submenuManager, createSiblingActions, null); |
393 | menuManager.insertBefore("edit", submenuManager); |
394 | } |
395 | |
396 | /** |
397 | * This inserts global actions before the "additions-end" separator. |
398 | * <!-- begin-user-doc --> |
399 | * <!-- end-user-doc --> |
400 | * @generated |
401 | */ |
402 | @Override |
403 | protected void addGlobalActions(IMenuManager menuManager) { |
404 | menuManager.insertAfter("additions-end", new Separator("ui-actions")); |
405 | menuManager.insertAfter("ui-actions", showPropertiesViewAction); |
406 | |
407 | refreshViewerAction.setEnabled(refreshViewerAction.isEnabled()); |
408 | menuManager.insertAfter("ui-actions", refreshViewerAction); |
409 | |
410 | super.addGlobalActions(menuManager); |
411 | } |
412 | |
413 | /** |
414 | * This ensures that a delete action will clean up all references to deleted objects. |
415 | * <!-- begin-user-doc --> |
416 | * <!-- end-user-doc --> |
417 | * @generated |
418 | */ |
419 | @Override |
420 | protected boolean removeAllReferencesOnDelete() { |
421 | return true; |
422 | } |
423 | |
424 | } |