KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > editor > Doml > presentation > DomlActionBarContributor


1 /**
2  * <copyright>
3  * </copyright>
4  *
5  * $Id$
6  */

7 package org.enhydra.dods.editor.Doml.presentation;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 import org.eclipse.emf.common.ui.viewer.IViewerProvider;
14
15 import org.eclipse.emf.edit.domain.EditingDomain;
16 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
17
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 import org.enhydra.dods.editor.Doml.provider.DomlEditPlugin;
47
48 /**
49  * This is the action bar contributor for the Doml model editor.
50  * <!-- begin-user-doc -->
51  * <!-- end-user-doc -->
52  * @generated
53  */

54 public class DomlActionBarContributor
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(DomlEditPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) {
81             public void run() {
82                 try {
83                     getPage().showView("org.eclipse.ui.views.PropertySheet");
84                 }
85                 catch (PartInitException exception) {
86                     DomlEditPlugin.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(DomlEditPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) {
100             public boolean isEnabled() {
101                 return activeEditorPart instanceof IViewerProvider;
102             }
103
104             public void run() {
105                 if (activeEditorPart instanceof IViewerProvider) {
106                     Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer();
107                     if (viewer != null) {
108                         viewer.refresh();
109                     }
110                 }
111             }
112         };
113
114     /**
115      * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateChildAction} corresponding to each descriptor
116      * generated for the current selection by the item provider.
117      * <!-- begin-user-doc -->
118      * <!-- end-user-doc -->
119      * @generated
120      */

121     protected Collection JavaDoc createChildActions;
122
123     /**
124      * This is the menu manager into which menu contribution items should be added for CreateChild actions.
125      * <!-- begin-user-doc -->
126      * <!-- end-user-doc -->
127      * @generated
128      */

129     protected IMenuManager createChildMenuManager;
130
131     /**
132      * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} corresponding to each descriptor
133      * generated for the current selection by the item provider.
134      * <!-- begin-user-doc -->
135      * <!-- end-user-doc -->
136      * @generated
137      */

138     protected Collection JavaDoc createSiblingActions;
139
140     /**
141      * This is the menu manager into which menu contribution items should be added for CreateSibling actions.
142      * <!-- begin-user-doc -->
143      * <!-- end-user-doc -->
144      * @generated
145      */

146     protected IMenuManager createSiblingMenuManager;
147
148     /**
149      * This creates an instance of the contributor.
150      * <!-- begin-user-doc -->
151      * <!-- end-user-doc -->
152      * @generated
153      */

154     public DomlActionBarContributor() {
155         loadResourceAction = new LoadResourceAction();
156         validateAction = new ValidateAction();
157     }
158
159     /**
160      * This adds Separators for editor additions to the tool bar.
161      * <!-- begin-user-doc -->
162      * <!-- end-user-doc -->
163      * @generated
164      */

165     public void contributeToToolBar(IToolBarManager toolBarManager) {
166         toolBarManager.add(new Separator("doml-settings"));
167         toolBarManager.add(new Separator("doml-additions"));
168     }
169
170     /**
171      * This adds to the menu bar a menu and some separators for editor additions,
172      * as well as the sub-menus for object creation items.
173      * <!-- begin-user-doc -->
174      * <!-- end-user-doc -->
175      * @generated
176      */

177     public void contributeToMenu(IMenuManager menuManager) {
178         super.contributeToMenu(menuManager);
179
180         IMenuManager submenuManager = new MenuManager(DomlEditPlugin.INSTANCE.getString("_UI_DomlEditor_menu"), "org.enhydra.dods.editor.DomlMenuID");
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(DomlEditPlugin.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(DomlEditPlugin.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     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.SelectionChangedEvents} 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 JavaDoc newChildDescriptors = null;
260         Collection JavaDoc newSiblingDescriptors = null;
261
262         ISelection selection = event.getSelection();
263         if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) {
264             Object JavaDoc 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 JavaDoc generateCreateChildActions(Collection JavaDoc descriptors, ISelection selection) {
295         Collection JavaDoc actions = new ArrayList JavaDoc();
296         if (descriptors != null) {
297             for (Iterator JavaDoc i = descriptors.iterator(); i.hasNext(); ) {
298                 actions.add(new CreateChildAction(activeEditorPart, selection, i.next()));
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 JavaDoc generateCreateSiblingActions(Collection JavaDoc descriptors, ISelection selection) {
312         Collection JavaDoc actions = new ArrayList JavaDoc();
313         if (descriptors != null) {
314             for (Iterator JavaDoc i = descriptors.iterator(); i.hasNext(); ) {
315                 actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next()));
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>ID</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 JavaDoc actions, String JavaDoc contributionID) {
331         if (actions != null) {
332             for (Iterator JavaDoc i = actions.iterator(); i.hasNext(); ) {
333                 IAction action = (IAction)i.next();
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 JavaDoc 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     public void menuAboutToShow(IMenuManager menuManager) {
381         super.menuAboutToShow(menuManager);
382         MenuManager submenuManager = null;
383
384         submenuManager = new MenuManager(DomlEditPlugin.INSTANCE.getString("_UI_CreateChild_menu_item"));
385         populateManager(submenuManager, createChildActions, null);
386         menuManager.insertBefore("additions", submenuManager);
387
388         submenuManager = new MenuManager(DomlEditPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item"));
389         populateManager(submenuManager, createSiblingActions, null);
390         menuManager.insertBefore("additions", 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     protected void addGlobalActions(IMenuManager menuManager) {
400         menuManager.insertAfter("additions-end", new Separator("ui-actions"));
401         menuManager.insertAfter("ui-actions", showPropertiesViewAction);
402
403         refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
404         menuManager.insertAfter("ui-actions", refreshViewerAction);
405
406         super.addGlobalActions(menuManager);
407     }
408
409 }
410
Popular Tags