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