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