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