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