KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > sdo > presentation > SDOActionBarContributor


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2003-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: SDOActionBarContributor.java,v 1.5 2005/06/08 06:21:18 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.sdo.presentation;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23
24 import org.eclipse.emf.common.ui.viewer.IViewerProvider;
25 import org.eclipse.emf.ecore.sdo.action.EvaluatePathAction;
26 import org.eclipse.emf.edit.domain.EditingDomain;
27 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
28 import org.eclipse.emf.edit.ui.action.CreateChildAction;
29 import org.eclipse.emf.edit.ui.action.CreateSiblingAction;
30 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor;
31 import org.eclipse.emf.edit.ui.action.LoadResourceAction;
32 import org.eclipse.emf.edit.ui.action.ValidateAction;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.action.ActionContributionItem;
35 import org.eclipse.jface.action.IAction;
36 import org.eclipse.jface.action.IContributionItem;
37 import org.eclipse.jface.action.IContributionManager;
38 import org.eclipse.jface.action.IMenuListener;
39 import org.eclipse.jface.action.IMenuManager;
40 import org.eclipse.jface.action.IToolBarManager;
41 import org.eclipse.jface.action.MenuManager;
42 import org.eclipse.jface.action.Separator;
43 import org.eclipse.jface.action.SubContributionItem;
44 import org.eclipse.jface.viewers.ISelection;
45 import org.eclipse.jface.viewers.ISelectionChangedListener;
46 import org.eclipse.jface.viewers.ISelectionProvider;
47 import org.eclipse.jface.viewers.IStructuredSelection;
48 import org.eclipse.jface.viewers.SelectionChangedEvent;
49 import org.eclipse.jface.viewers.Viewer;
50 import org.eclipse.ui.IEditorPart;
51 import org.eclipse.ui.PartInitException;
52 import org.eclipse.ui.actions.BaseSelectionListenerAction;
53
54 /**
55  * This is the action bar contributor for the SDO model editor.
56  * <!-- begin-user-doc -->
57  * <!-- end-user-doc -->
58  * @generated
59  */

60 public class SDOActionBarContributor
61   extends EditingDomainActionBarContributor
62   implements ISelectionChangedListener
63 {
64   /**
65    * This keeps track of the active editor.
66    * <!-- begin-user-doc -->
67    * <!-- end-user-doc -->
68    * @generated
69    */

70   protected IEditorPart activeEditorPart;
71
72   /**
73    * This keeps track of the current selection provider.
74    * <!-- begin-user-doc -->
75    * <!-- end-user-doc -->
76    * @generated
77    */

78   protected ISelectionProvider selectionProvider;
79
80   /**
81    * This action opens the Properties view.
82    * <!-- begin-user-doc -->
83    * <!-- end-user-doc -->
84    * @generated
85    */

86   protected IAction showPropertiesViewAction =
87     new Action(SDOEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item"))
88     {
89       public void run()
90       {
91         try
92         {
93           getPage().showView("org.eclipse.ui.views.PropertySheet");
94         }
95         catch (PartInitException exception)
96         {
97           SDOEditorPlugin.INSTANCE.log(exception);
98         }
99       }
100     };
101
102   /**
103    * This action refreshes the viewer of the current editor if the editor
104    * implements {@link org.eclipse.emf.common.ui.viewer.IViewerProvider}.
105    * <!-- begin-user-doc -->
106    * <!-- end-user-doc -->
107    * @generated
108    */

109   protected IAction refreshViewerAction =
110     new Action(SDOEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item"))
111     {
112       public boolean isEnabled()
113       {
114         return activeEditorPart instanceof IViewerProvider;
115       }
116
117       public void run()
118       {
119         if (activeEditorPart instanceof IViewerProvider)
120         {
121           Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer();
122           if (viewer != null)
123           {
124             viewer.refresh();
125           }
126         }
127       }
128     };
129
130   /**
131    * This will contain one {@link CreateChildAction} corresponding to each descriptor
132    * generated for the current selection by the item provider.
133    * <!-- begin-user-doc -->
134    * <!-- end-user-doc -->
135    * @generated NOT
136    */

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

145   protected IMenuManager createChildMenuManager;
146
147   /**
148    * This will contain one {@link CreateSiblingAction} corresponding to each descriptor
149    * generated for the current selection by the item provider.
150    * <!-- begin-user-doc -->
151    * <!-- end-user-doc -->
152    * @generated NOT
153    */

154   protected Collection JavaDoc createSiblingActions;
155
156   /**
157    * This is the menu manager into which menu contribution items should be added for CreateSibling actions.
158    * <!-- begin-user-doc -->
159    * <!-- end-user-doc -->
160    * @generated NOT
161    */

162   protected IMenuManager createSiblingMenuManager;
163
164   /**
165    * This creates an instance of the contributor.
166    * <!-- begin-user-doc -->
167    * <!-- end-user-doc -->
168    * @generated
169    */

170   public SDOActionBarContributor()
171   {
172     loadResourceAction = new LoadResourceAction();
173     validateAction = new ValidateAction();
174   }
175
176   /**
177    * This adds Separators for editor additions to the tool bar.
178    * <!-- begin-user-doc -->
179    * <!-- end-user-doc -->
180    * @generated
181    */

182   public void contributeToToolBar(IToolBarManager toolBarManager)
183   {
184     toolBarManager.add(new Separator("sdo-settings"));
185     toolBarManager.add(new Separator("sdo-additions"));
186   }
187
188   /**
189    * This adds to the menu bar a menu and some separators for editor additions,
190    * as well as the sub-menus for object creation items.
191    * <!-- begin-user-doc -->
192    * <!-- end-user-doc -->
193    * @generated NOT
194    */

195   public void contributeToMenu(IMenuManager menuManager)
196   {
197     super.contributeToMenu(menuManager);
198
199     IMenuManager submenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_SDOEditor_menu"), "org.eclipse.emf.ecore.sdoMenuID");
200     menuManager.insertAfter("additions", submenuManager);
201     submenuManager.add(new Separator("settings"));
202     submenuManager.add(new Separator("actions"));
203     submenuManager.add(new Separator("additions"));
204     submenuManager.add(new Separator("additions-end"));
205
206     // Prepare for CreateChild item addition or removal.
207
//
208
createChildMenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item"));
209     submenuManager.insertBefore("additions", createChildMenuManager);
210
211     // Prepare for CreateSibling item addition or removal.
212
//
213
createSiblingMenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item"));
214     submenuManager.insertBefore("additions", createSiblingMenuManager);
215
216     // Force an update because Eclipse hides empty menus now.
217
//
218
submenuManager.addMenuListener
219       (new IMenuListener()
220        {
221          public void menuAboutToShow(IMenuManager menuManager)
222          {
223            menuManager.updateAll(true);
224          }
225        });
226
227     addGlobalActions(submenuManager);
228   }
229
230   /**
231    * When the active editor changes, this remembers the change and registers with it as a selection provider.
232    * <!-- begin-user-doc -->
233    * <!-- end-user-doc -->
234    * @generated
235    */

236   public void setActiveEditor(IEditorPart part)
237   {
238     super.setActiveEditor(part);
239     activeEditorPart = part;
240
241     // Switch to the new selection provider.
242
//
243
if (selectionProvider != null)
244     {
245       selectionProvider.removeSelectionChangedListener(this);
246     }
247     if (part == null)
248     {
249       selectionProvider = null;
250     }
251     else
252     {
253       selectionProvider = part.getSite().getSelectionProvider();
254       selectionProvider.addSelectionChangedListener(this);
255
256       // Fake a selection changed event to update the menus.
257
//
258
if (selectionProvider.getSelection() != null)
259       {
260         selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection()));
261       }
262     }
263   }
264
265   /**
266    * This implements {@link ISelectionChangedListener},
267    * handling {@link SelectionChangedEvent} by querying for the children and siblings
268    * that can be added to the selected object and updating the menus accordingly.
269    * <!-- begin-user-doc -->
270    * <!-- end-user-doc -->
271    * @generated NOT
272    */

273   public void selectionChanged(SelectionChangedEvent event)
274   {
275     // Remove any menu items for old selection.
276
//
277
if (createChildMenuManager != null)
278     {
279       depopulateManager(createChildMenuManager, createChildActions);
280     }
281     if (createSiblingMenuManager != null)
282     {
283       depopulateManager(createSiblingMenuManager, createSiblingActions);
284     }
285
286     // Query the new selection for appropriate new child/sibling descriptors
287
//
288
Collection JavaDoc newChildDescriptors = null;
289     Collection JavaDoc newSiblingDescriptors = null;
290
291     ISelection selection = event.getSelection();
292     if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1)
293     {
294       Object JavaDoc object = ((IStructuredSelection)selection).getFirstElement();
295
296       EditingDomain domain =
297         ((IEditingDomainProvider) activeEditorPart).getEditingDomain();
298
299       newChildDescriptors = domain.getNewChildDescriptors(object, null);
300       newSiblingDescriptors = domain.getNewChildDescriptors(null, object);
301     }
302
303     // Generate actions for selection; populate and redraw the menus.
304
//
305
createChildActions = generateCreateChildActions(newChildDescriptors, selection);
306     createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection);
307
308     if (createChildMenuManager != null)
309     {
310       populateManager(createChildMenuManager, createChildActions, null);
311       createChildMenuManager.update(true);
312     }
313     if (createSiblingMenuManager != null)
314     {
315       populateManager(createSiblingMenuManager, createSiblingActions, null);
316       createSiblingMenuManager.update(true);
317     }
318   }
319
320   /**
321    * This generates a {@link CreateChildAction} for each object in <code>descriptors</code>,
322    * and returns the collection of these actions.
323    * <!-- begin-user-doc -->
324    * <!-- end-user-doc -->
325    * @generated NOT
326    */

327   protected Collection JavaDoc generateCreateChildActions(Collection JavaDoc descriptors, ISelection selection)
328   {
329     Collection JavaDoc actions = new LinkedList JavaDoc();
330     if (descriptors != null)
331     {
332       for (Iterator JavaDoc i = descriptors.iterator(); i.hasNext(); )
333       {
334         actions.add(new CreateChildAction(activeEditorPart, selection, i.next()));
335       }
336     }
337     return actions;
338   }
339
340   /**
341    * This generates a {@link CreateSiblingAction} for each object in <code>descriptors</code>,
342    * and returns the collection of these actions.
343    * <!-- begin-user-doc -->
344    * <!-- end-user-doc -->
345    * @generated NOT
346    */

347   protected Collection JavaDoc generateCreateSiblingActions(Collection JavaDoc descriptors, ISelection selection)
348   {
349     Collection JavaDoc actions = new LinkedList JavaDoc();
350     if (descriptors != null)
351     {
352       for (Iterator JavaDoc i = descriptors.iterator(); i.hasNext(); )
353       {
354         actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next()));
355       }
356     }
357     return actions;
358   }
359
360   /**
361    * This populates the specified <code>manager</code> with {@link ActionContributionItem}s
362    * based on the {@link IAction}s contained in the <code>actions</code> collection,
363    * by inserting them before the specified contribution item <code>contributionID</code>.
364    * If <code>ID</code> is <code>null</code>, they are simply added.
365    * <!-- begin-user-doc -->
366    * <!-- end-user-doc -->
367    * @generated NOT
368    */

369   protected void populateManager(IContributionManager manager, Collection JavaDoc actions, String JavaDoc contributionID)
370   {
371     if (actions != null)
372     {
373       for (Iterator JavaDoc i = actions.iterator(); i.hasNext(); )
374       {
375         IAction action = (IAction) i.next();
376         if (contributionID != null)
377         {
378           manager.insertBefore(contributionID, action);
379         }
380         else
381         {
382           manager.add(action);
383         }
384       }
385     }
386   }
387     
388   /**
389    * This removes from the specified <code>manager</code> all {@link ActionContributionItem}s
390    * based on the {@link IAction}s contained in the <code>actions</code> collection.
391    * <!-- begin-user-doc -->
392    * <!-- end-user-doc -->
393    * @generated NOT
394    */

395   protected void depopulateManager(IContributionManager manager, Collection JavaDoc actions)
396   {
397     if (actions != null)
398     {
399       IContributionItem[] items = manager.getItems();
400       for (int i = 0; i < items.length; i++)
401       {
402         // Look into SubContributionItems
403
//
404
IContributionItem contributionItem = items[i];
405         while (contributionItem instanceof SubContributionItem)
406         {
407           contributionItem = ((SubContributionItem)contributionItem).getInnerItem();
408         }
409
410         // Delete the ActionContributionItems with matching action.
411
//
412
if (contributionItem instanceof ActionContributionItem)
413         {
414           IAction action = ((ActionContributionItem) contributionItem).getAction();
415           if (actions.contains(action))
416           {
417             manager.remove(contributionItem);
418           }
419         }
420       }
421     }
422   }
423
424   /**
425    * This populates the pop-up menu before it appears.
426    * <!-- begin-user-doc -->
427    * <!-- end-user-doc -->
428    * @generated NOT
429    */

430   public void menuAboutToShow(IMenuManager menuManager)
431   {
432     refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
433     
434     super.menuAboutToShow(menuManager);
435     MenuManager submenuManager = null;
436
437     submenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item"));
438     populateManager(submenuManager, createChildActions, null);
439     menuManager.insertBefore("additions", submenuManager);
440
441     submenuManager = new MenuManager(SDOEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item"));
442     populateManager(submenuManager, createSiblingActions, null);
443     menuManager.insertBefore("additions", submenuManager);
444
445     menuManager.insertBefore("additions", new Separator());
446     menuManager.insertBefore
447       ("additions",
448         new BaseSelectionListenerAction(SDOEditorPlugin.INSTANCE.getString("_UI_EvaluatePath_menu_item"))
449         {
450           EvaluatePathAction evaluatePathAction = new EvaluatePathAction();
451           
452           {
453             evaluatePathAction.selectionChanged(this, (IStructuredSelection)((ISelectionProvider)activeEditor).getSelection());
454           }
455           
456           public void run()
457           {
458             evaluatePathAction.run(this);
459           }
460         });
461   }
462
463   /**
464    * This inserts global actions before the "additions-end" separator.
465    * <!-- begin-user-doc -->
466    * <!-- end-user-doc -->
467    * @generated
468    */

469   protected void addGlobalActions(IMenuManager menuManager)
470   {
471     menuManager.insertAfter("additions-end", new Separator("ui-actions"));
472     menuManager.insertAfter("ui-actions", showPropertiesViewAction);
473
474     refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
475     menuManager.insertAfter("ui-actions", refreshViewerAction);
476
477     super.addGlobalActions(menuManager);
478   }
479
480 }
481
Popular Tags