KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > presentation > EcoreActionBarContributor


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-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: EcoreActionBarContributor.java,v 1.6 2005/06/08 06:17:55 nickb Exp $
16  */

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

63 public class EcoreActionBarContributor
64   extends EditingDomainActionBarContributor
65   implements ISelectionChangedListener
66 {
67   public static class Reflective extends EcoreActionBarContributor
68   {
69     public Reflective()
70     {
71     }
72
73     protected IMenuManager createSubmenuManager()
74     {
75       return new MenuManager(EcoreEditorPlugin.getPlugin().getString("_UI_ReflectiveEditor_menu"), "org.eclipse.emf.ecoreMenuID");
76     }
77   }
78
79   /**
80    * This keeps track of the active editor.
81    * <!-- begin-user-doc -->
82    * <!-- end-user-doc -->
83    * @generated
84    */

85   protected IEditorPart activeEditorPart;
86
87   /**
88    * This keeps track of the current selection provider.
89    * <!-- begin-user-doc -->
90    * <!-- end-user-doc -->
91    * @generated
92    */

93   protected ISelectionProvider selectionProvider;
94
95   /**
96    * This action opens the Properties view.
97    * <!-- begin-user-doc -->
98    * <!-- end-user-doc -->
99    * @generated
100    */

101   protected IAction showPropertiesViewAction =
102     new Action(EcoreEditorPlugin.INSTANCE.getString("_UI_ShowPropertiesView_menu_item"))
103     {
104       public void run()
105       {
106         try
107         {
108           getPage().showView("org.eclipse.ui.views.PropertySheet");
109         }
110         catch (PartInitException exception)
111         {
112           EcoreEditorPlugin.INSTANCE.log(exception);
113         }
114       }
115     };
116
117   /**
118    * This action refreshes the viewer of the current editor if the editor
119    * implements {@link org.eclipse.emf.common.ui.viewer.IViewerProvider}.
120    * <!-- begin-user-doc -->
121    * <!-- end-user-doc -->
122    * @generated
123    */

124   protected IAction refreshViewerAction =
125     new Action(EcoreEditorPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item"))
126     {
127       public boolean isEnabled()
128       {
129         return activeEditorPart instanceof IViewerProvider;
130       }
131
132       public void run()
133       {
134         if (activeEditorPart instanceof IViewerProvider)
135         {
136           Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer();
137           if (viewer != null)
138           {
139             viewer.refresh();
140           }
141         }
142       }
143     };
144
145   /**
146    * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateChildAction} corresponding to each descriptor
147    * generated for the current selection by the item provider.
148    * <!-- begin-user-doc -->
149    * <!-- end-user-doc -->
150    * @generated
151    */

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

160   protected IMenuManager createChildMenuManager;
161
162   /**
163    * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} corresponding to each descriptor
164    * generated for the current selection by the item provider.
165    * <!-- begin-user-doc -->
166    * <!-- end-user-doc -->
167    * @generated
168    */

169   protected Collection JavaDoc createSiblingActions;
170
171   /**
172    * This is the menu manager into which menu contribution items should be added for CreateSibling actions.
173    * <!-- begin-user-doc -->
174    * <!-- end-user-doc -->
175    * @generated
176    */

177   protected IMenuManager createSiblingMenuManager;
178
179   /**
180    * This creates an instance of the contributor.
181    * <!-- begin-user-doc -->
182    * <!-- end-user-doc -->
183    * @generated
184    */

185   public EcoreActionBarContributor()
186   {
187     loadResourceAction = new LoadResourceAction();
188     validateAction = new ValidateAction();
189   }
190
191   /**
192    * This adds Separators for editor additions to the tool bar.
193    * <!-- begin-user-doc -->
194    * <!-- end-user-doc -->
195    * @generated
196    */

197   public void contributeToToolBar(IToolBarManager toolBarManager)
198   {
199     toolBarManager.add(new Separator("ecore-settings"));
200     toolBarManager.add(new Separator("ecore-additions"));
201   }
202
203   /**
204    * This adds to the menu bar a menu and some separators for editor additions,
205    * as well as the sub-menus for object creation items.
206    * <!-- begin-user-doc -->
207    * <!-- end-user-doc -->
208    * @generated NOT
209    */

210   public void contributeToMenu(IMenuManager menuManager)
211   {
212     super.contributeToMenu(menuManager);
213
214     IMenuManager submenuManager = createSubmenuManager();
215
216     menuManager.insertAfter("additions", submenuManager);
217     submenuManager.add(new Separator("settings"));
218     submenuManager.add(new Separator("actions"));
219     submenuManager.add(new Separator("additions"));
220     submenuManager.add(new Separator("additions-end"));
221
222     // Prepare for CreateChild item addition or removal.
223
//
224
createChildMenuManager = new MenuManager(EcoreEditorPlugin.getPlugin().getString("_UI_CreateChild_menu_item"));
225     submenuManager.insertBefore("additions", createChildMenuManager);
226
227     // Prepare for CreateSibling item addition or removal.
228
//
229
createSiblingMenuManager = new MenuManager(EcoreEditorPlugin.getPlugin().getString("_UI_CreateSibling_menu_item"));
230     submenuManager.insertBefore("additions", createSiblingMenuManager);
231     
232     // Force an update because Eclipse hides empty menus now.
233
//
234
submenuManager.addMenuListener
235       (new IMenuListener()
236        {
237          public void menuAboutToShow(IMenuManager menuManager)
238          {
239            menuManager.updateAll(true);
240          }
241        });
242
243     addGlobalActions(submenuManager);
244   }
245
246   protected IMenuManager createSubmenuManager()
247   {
248     return new MenuManager(EcoreEditorPlugin.getPlugin().getString("_UI_EcoreEditor_menu"), "org.eclipse.emf.ecoreMenuID");
249   }
250
251   /**
252    * When the active editor changes, this remembers the change and registers with it as a selection provider.
253    * <!-- begin-user-doc -->
254    * <!-- end-user-doc -->
255    * @generated
256    */

257   public void setActiveEditor(IEditorPart part)
258   {
259     super.setActiveEditor(part);
260     activeEditorPart = part;
261
262     // Switch to the new selection provider.
263
//
264
if (selectionProvider != null)
265     {
266       selectionProvider.removeSelectionChangedListener(this);
267     }
268     if (part == null)
269     {
270       selectionProvider = null;
271     }
272     else
273     {
274       selectionProvider = part.getSite().getSelectionProvider();
275       selectionProvider.addSelectionChangedListener(this);
276
277       // Fake a selection changed event to update the menus.
278
//
279
if (selectionProvider.getSelection() != null)
280       {
281         selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection()));
282       }
283     }
284   }
285
286   /**
287    * This implements {@link org.eclipse.jface.viewers.ISelectionChangedListener},
288    * handling {@link org.eclipse.jface.viewers.SelectionChangedEvent}s by querying for the children and siblings
289    * that can be added to the selected object and updating the menus accordingly.
290    * <!-- begin-user-doc -->
291    * <!-- end-user-doc -->
292    * @generated
293    */

294   public void selectionChanged(SelectionChangedEvent event)
295   {
296     // Remove any menu items for old selection.
297
//
298
if (createChildMenuManager != null)
299     {
300       depopulateManager(createChildMenuManager, createChildActions);
301     }
302     if (createSiblingMenuManager != null)
303     {
304       depopulateManager(createSiblingMenuManager, createSiblingActions);
305     }
306
307     // Query the new selection for appropriate new child/sibling descriptors
308
//
309
Collection JavaDoc newChildDescriptors = null;
310     Collection JavaDoc newSiblingDescriptors = null;
311
312     ISelection selection = event.getSelection();
313     if (selection instanceof IStructuredSelection && ((IStructuredSelection)selection).size() == 1)
314     {
315       Object JavaDoc object = ((IStructuredSelection)selection).getFirstElement();
316
317       EditingDomain domain = ((IEditingDomainProvider)activeEditorPart).getEditingDomain();
318
319       newChildDescriptors = domain.getNewChildDescriptors(object, null);
320       newSiblingDescriptors = domain.getNewChildDescriptors(null, object);
321     }
322
323     // Generate actions for selection; populate and redraw the menus.
324
//
325
createChildActions = generateCreateChildActions(newChildDescriptors, selection);
326     createSiblingActions = generateCreateSiblingActions(newSiblingDescriptors, selection);
327
328     if (createChildMenuManager != null)
329     {
330       populateManager(createChildMenuManager, createChildActions, null);
331       createChildMenuManager.update(true);
332     }
333     if (createSiblingMenuManager != null)
334     {
335       populateManager(createSiblingMenuManager, createSiblingActions, null);
336       createSiblingMenuManager.update(true);
337     }
338   }
339
340   /**
341    * This generates a {@link org.eclipse.emf.edit.ui.action.CreateChildAction} 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
346    */

347   protected Collection JavaDoc generateCreateChildActions(Collection JavaDoc descriptors, ISelection selection)
348   {
349     Collection JavaDoc actions = new ArrayList JavaDoc();
350     if (descriptors != null)
351     {
352       for (Iterator JavaDoc i = descriptors.iterator(); i.hasNext(); )
353       {
354         actions.add(new CreateChildAction(activeEditorPart, selection, i.next()));
355       }
356     }
357     return actions;
358   }
359
360   /**
361    * This generates a {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} for each object in <code>descriptors</code>,
362    * and returns the collection of these actions.
363    * <!-- begin-user-doc -->
364    * <!-- end-user-doc -->
365    * @generated
366    */

367   protected Collection JavaDoc generateCreateSiblingActions(Collection JavaDoc descriptors, ISelection selection)
368   {
369     Collection JavaDoc actions = new ArrayList JavaDoc();
370     if (descriptors != null)
371     {
372       for (Iterator JavaDoc i = descriptors.iterator(); i.hasNext(); )
373       {
374         actions.add(new CreateSiblingAction(activeEditorPart, selection, i.next()));
375       }
376     }
377     return actions;
378   }
379
380   /**
381    * This populates the specified <code>manager</code> with {@link org.eclipse.jface.action.ActionContributionItem}s
382    * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection,
383    * by inserting them before the specified contribution item <code>contributionID</code>.
384    * If <code>ID</code> is <code>null</code>, they are simply added.
385    * <!-- begin-user-doc -->
386    * <!-- end-user-doc -->
387    * @generated
388    */

389   protected void populateManager(IContributionManager manager, Collection JavaDoc actions, String JavaDoc contributionID)
390   {
391     if (actions != null)
392     {
393       for (Iterator JavaDoc i = actions.iterator(); i.hasNext(); )
394       {
395         IAction action = (IAction)i.next();
396         if (contributionID != null)
397         {
398           manager.insertBefore(contributionID, action);
399         }
400         else
401         {
402           manager.add(action);
403         }
404       }
405     }
406   }
407     
408   /**
409    * This removes from the specified <code>manager</code> all {@link org.eclipse.jface.action.ActionContributionItem}s
410    * based on the {@link org.eclipse.jface.action.IAction}s contained in the <code>actions</code> collection.
411    * <!-- begin-user-doc -->
412    * <!-- end-user-doc -->
413    * @generated
414    */

415   protected void depopulateManager(IContributionManager manager, Collection JavaDoc actions)
416   {
417     if (actions != null)
418     {
419       IContributionItem[] items = manager.getItems();
420       for (int i = 0; i < items.length; i++)
421       {
422         // Look into SubContributionItems
423
//
424
IContributionItem contributionItem = items[i];
425         while (contributionItem instanceof SubContributionItem)
426         {
427           contributionItem = ((SubContributionItem)contributionItem).getInnerItem();
428         }
429
430         // Delete the ActionContributionItems with matching action.
431
//
432
if (contributionItem instanceof ActionContributionItem)
433         {
434           IAction action = ((ActionContributionItem)contributionItem).getAction();
435           if (actions.contains(action))
436           {
437             manager.remove(contributionItem);
438           }
439         }
440       }
441     }
442   }
443
444   /**
445    * This populates the pop-up menu before it appears.
446    * <!-- begin-user-doc -->
447    * <!-- end-user-doc -->
448    * @generated
449    */

450   public void menuAboutToShow(IMenuManager menuManager)
451   {
452     super.menuAboutToShow(menuManager);
453     MenuManager submenuManager = null;
454
455     submenuManager = new MenuManager(EcoreEditorPlugin.INSTANCE.getString("_UI_CreateChild_menu_item"));
456     populateManager(submenuManager, createChildActions, null);
457     menuManager.insertBefore("additions", submenuManager);
458
459     submenuManager = new MenuManager(EcoreEditorPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item"));
460     populateManager(submenuManager, createSiblingActions, null);
461     menuManager.insertBefore("additions", submenuManager);
462   }
463
464   /**
465    * This inserts global actions before the "additions-end" separator.
466    * <!-- begin-user-doc -->
467    * <!-- end-user-doc -->
468    * @generated
469    */

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