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