KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Olj > presentation > OljActionBarContributor


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

7 package Olj.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 /**
47  * This is the action bar contributor for the Olj model editor.
48  * <!-- begin-user-doc -->
49  * <!-- end-user-doc -->
50  * @generated
51  */

52 public class OljActionBarContributor
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(OljEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item")) {
79             public void run() {
80                 try {
81                     getPage().showView("org.eclipse.ui.views.PropertySheet");
82                 }
83                 catch (PartInitException exception) {
84                     OljEditorPlugin.INSTANCE.log(exception);
85                 }
86             }
87         };
88
89     /**
90      * This action refreshes the viewer of the current editor if the editor
91      * implements {@link org.eclipse.emf.common.ui.viewer.IViewerProvider}.
92      * <!-- begin-user-doc -->
93      * <!-- end-user-doc -->
94      * @generated
95      */

96     protected IAction refreshViewerAction =
97         new Action(OljEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) {
98             public boolean isEnabled() {
99                 return activeEditorPart instanceof IViewerProvider;
100             }
101
102             public void run() {
103                 if (activeEditorPart instanceof IViewerProvider) {
104                     Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer();
105                     if (viewer != null) {
106                         viewer.refresh();
107                     }
108                 }
109             }
110         };
111
112     /**
113      * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateChildAction} corresponding to each descriptor
114      * generated for the current selection by the item provider.
115      * <!-- begin-user-doc -->
116      * <!-- end-user-doc -->
117      * @generated
118      */

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

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

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

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

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

163     public void contributeToToolBar(IToolBarManager toolBarManager) {
164         toolBarManager.add(new Separator("olj-settings"));
165         toolBarManager.add(new Separator("olj-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     public void contributeToMenu(IMenuManager menuManager) {
176         super.contributeToMenu(menuManager);
177
178         IMenuManager submenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_OljEditor_menu"), "OljMenuID");
179         menuManager.insertAfter("additions", submenuManager);
180         submenuManager.add(new Separator("settings"));
181         submenuManager.add(new Separator("actions"));
182         submenuManager.add(new Separator("additions"));
183         submenuManager.add(new Separator("additions-end"));
184
185         // Prepare for CreateChild item addition or removal.
186
//
187
createChildMenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item"));
188         submenuManager.insertBefore("additions", createChildMenuManager);
189
190         // Prepare for CreateSibling item addition or removal.
191
//
192
createSiblingMenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item"));
193         submenuManager.insertBefore("additions", createSiblingMenuManager);
194
195         // Force an update because Eclipse hides empty menus now.
196
//
197
submenuManager.addMenuListener
198             (new IMenuListener() {
199                  public void menuAboutToShow(IMenuManager menuManager) {
200                      menuManager.updateAll(true);
201                  }
202              });
203
204         addGlobalActions(submenuManager);
205     }
206
207     /**
208      * When the active editor changes, this remembers the change and registers with it as a selection provider.
209      * <!-- begin-user-doc -->
210      * <!-- end-user-doc -->
211      * @generated
212      */

213     public void setActiveEditor(IEditorPart part) {
214         super.setActiveEditor(part);
215         activeEditorPart = part;
216
217         // Switch to the new selection provider.
218
//
219
if (selectionProvider != null) {
220             selectionProvider.removeSelectionChangedListener(this);
221         }
222         if (part == null) {
223             selectionProvider = null;
224         }
225         else {
226             selectionProvider = part.getSite().getSelectionProvider();
227             selectionProvider.addSelectionChangedListener(this);
228
229             // Fake a selection changed event to update the menus.
230
//
231
if (selectionProvider.getSelection() != null) {
232                 selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection()));
233             }
234         }
235     }
236
237     /**
238      * This implements {@link org.eclipse.jface.viewers.ISelectionChangedListener},
239      * handling {@link org.eclipse.jface.viewers.SelectionChangedEvents} by querying for the children and siblings
240      * that can be added to the selected object and updating the menus accordingly.
241      * <!-- begin-user-doc -->
242      * <!-- end-user-doc -->
243      * @generated
244      */

245     public void selectionChanged(SelectionChangedEvent event) {
246         // Remove any menu items for old selection.
247
//
248
if (createChildMenuManager != null) {
249             depopulateManager(createChildMenuManager, createChildActions);
250         }
251         if (createSiblingMenuManager != null) {
252             depopulateManager(createSiblingMenuManager, createSiblingActions);
253         }
254
255         // Query the new selection for appropriate new child/sibling descriptors
256
//
257
Collection JavaDoc newChildDescriptors = null;
258         Collection JavaDoc newSiblingDescriptors = null;
259
260         ISelection selection = event.getSelection();
261         if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1) {
262             Object JavaDoc object = ((IStructuredSelection)selection).getFirstElement();
263
264             EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain();
265
266             newChildDescriptors = domain.getNewChildDescriptors(object, null);
267             newSiblingDescriptors = domain.getNewChildDescriptors(null, object);
268         }
269
270         // Generate actions for selection; populate and redraw the menus.
271
//
272
createChildActions = generateCreateChildActions(newChildDescriptors, selection);
273         createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection);
274
275         if (createChildMenuManager != null) {
276             populateManager(createChildMenuManager, createChildActions, null);
277             createChildMenuManager.update(true);
278         }
279         if (createSiblingMenuManager != null) {
280             populateManager(createSiblingMenuManager, createSiblingActions, null);
281             createSiblingMenuManager.update(true);
282         }
283     }
284
285     /**
286      * This generates a {@link org.eclipse.emf.edit.ui.action.CreateChildAction} for each object in <code>descriptors</code>,
287      * and returns the collection of these actions.
288      * <!-- begin-user-doc -->
289      * <!-- end-user-doc -->
290      * @generated
291      */

292     protected Collection JavaDoc generateCreateChildActions(Collection JavaDoc descriptors, ISelection selection) {
293         Collection JavaDoc actions = new ArrayList JavaDoc();
294         if (descriptors != null) {
295             for (Iterator JavaDoc i = descriptors.iterator(); i.hasNext(); ) {
296                 actions.add(new CreateChildAction(activeEditorPart, selection, i.next()));
297             }
298         }
299         return actions;
300     }
301
302     /**
303      * This generates a {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} for each object in <code>descriptors</code>,
304      * and returns the collection of these actions.
305      * <!-- begin-user-doc -->
306      * <!-- end-user-doc -->
307      * @generated
308      */

309     protected Collection JavaDoc generateCreateSiblingActions(Collection JavaDoc descriptors, ISelection selection) {
310         Collection JavaDoc actions = new ArrayList JavaDoc();
311         if (descriptors != null) {
312             for (Iterator JavaDoc i = descriptors.iterator(); i.hasNext(); ) {
313                 actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next()));
314             }
315         }
316         return actions;
317     }
318
319     /**
320      * This populates the specified <code>manager</code> with {@link org.eclipse.jface.action.ActionContributionItem}s
321      * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection,
322      * by inserting them before the specified contribution item <code>contributionID</code>.
323      * If <code>ID</code> is <code>null</code>, they are simply added.
324      * <!-- begin-user-doc -->
325      * <!-- end-user-doc -->
326      * @generated
327      */

328     protected void populateManager(IContributionManager manager, Collection JavaDoc actions, String JavaDoc contributionID) {
329         if (actions != null) {
330             for (Iterator JavaDoc i = actions.iterator(); i.hasNext(); ) {
331                 IAction action = (IAction)i.next();
332                 if (contributionID != null) {
333                     manager.insertBefore(contributionID, action);
334                 }
335                 else {
336                     manager.add(action);
337                 }
338             }
339         }
340     }
341         
342     /**
343      * This removes from the specified <code>manager</code> all {@link org.eclipse.jface.action.ActionContributionItem}s
344      * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection.
345      * <!-- begin-user-doc -->
346      * <!-- end-user-doc -->
347      * @generated
348      */

349     protected void depopulateManager(IContributionManager manager, Collection JavaDoc actions) {
350         if (actions != null) {
351             IContributionItem[] items = manager.getItems();
352             for (int i = 0; i < items.length; i++) {
353                 // Look into SubContributionItems
354
//
355
IContributionItem contributionItem = items[i];
356                 while (contributionItem instanceof SubContributionItem) {
357                     contributionItem = ((SubContributionItem)contributionItem).getInnerItem();
358                 }
359
360                 // Delete the ActionContributionItems with matching action.
361
//
362
if (contributionItem instanceof ActionContributionItem) {
363                     IAction action = ((ActionContributionItem)contributionItem).getAction();
364                     if (actions.contains(action)) {
365                         manager.remove(contributionItem);
366                     }
367                 }
368             }
369         }
370     }
371
372     /**
373      * This populates the pop-up menu before it appears.
374      * <!-- begin-user-doc -->
375      * <!-- end-user-doc -->
376      * @generated
377      */

378     public void menuAboutToShow(IMenuManager menuManager) {
379         super.menuAboutToShow(menuManager);
380         MenuManager submenuManager = null;
381
382         submenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item"));
383         populateManager(submenuManager, createChildActions, null);
384         menuManager.insertBefore("additions", submenuManager);
385
386         submenuManager = new MenuManager(OljEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item"));
387         populateManager(submenuManager, createSiblingActions, null);
388         menuManager.insertBefore("additions", submenuManager);
389     }
390
391     /**
392      * This inserts global actions before the "additions-end" separator.
393      * <!-- begin-user-doc -->
394      * <!-- end-user-doc -->
395      * @generated
396      */

397     protected void addGlobalActions(IMenuManager menuManager) {
398         menuManager.insertAfter("additions-end", new Separator("ui-actions"));
399         menuManager.insertAfter("ui-actions", showPropertiesViewAction);
400
401         refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
402         menuManager.insertAfter("ui-actions", refreshViewerAction);
403
404         super.addGlobalActions(menuManager);
405     }
406
407 }
408
Popular Tags