KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > ecore2xml > presentation > Ecore2XMLActionBarContributor


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 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: Ecore2XMLActionBarContributor.java,v 1.2 2005/04/20 20:25:12 khussey Exp $
16  */

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

64 public class Ecore2XMLActionBarContributor
65   extends EditingDomainActionBarContributor
66   implements ISelectionChangedListener
67 {
68   /**
69    * This keeps track of the active editor.
70    * <!-- begin-user-doc -->
71    * <!-- end-user-doc -->
72    * @generated
73    */

74   protected IEditorPart activeEditorPart;
75
76   /**
77    * This keeps track of the current selection provider.
78    * <!-- begin-user-doc -->
79    * <!-- end-user-doc -->
80    * @generated
81    */

82   protected ISelectionProvider selectionProvider;
83
84   /**
85    * This action opens the Properties view.
86    * <!-- begin-user-doc -->
87    * <!-- end-user-doc -->
88    * @generated
89    */

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

113   protected IAction refreshViewerAction =
114     new Action(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item")) //$NON-NLS-1$
115
{
116       public boolean isEnabled()
117       {
118         return activeEditorPart instanceof IViewerProvider;
119       }
120
121       public void run()
122       {
123         if (activeEditorPart instanceof IViewerProvider)
124         {
125           Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer();
126           if (viewer != null)
127           {
128             viewer.refresh();
129           }
130         }
131       }
132     };
133
134   /**
135    * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateChildAction} 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 JavaDoc createChildActions;
142
143   /**
144    * This is the menu manager into which menu contribution items should be added for CreateChild actions.
145    * <!-- begin-user-doc -->
146    * <!-- end-user-doc -->
147    * @generated
148    */

149   protected IMenuManager createChildMenuManager;
150
151   /**
152    * This will contain one {@link org.eclipse.emf.edit.ui.action.CreateSiblingAction} corresponding to each descriptor
153    * generated for the current selection by the item provider.
154    * <!-- begin-user-doc -->
155    * <!-- end-user-doc -->
156    * @generated
157    */

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

166   protected IMenuManager createSiblingMenuManager;
167
168   /**
169    * This creates an instance of the contributor.
170    * <!-- begin-user-doc -->
171    * <!-- end-user-doc -->
172    * @generated
173    */

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

186   public void contributeToToolBar(IToolBarManager toolBarManager)
187   {
188     toolBarManager.add(new Separator("ecore2xml-settings")); //$NON-NLS-1$
189
toolBarManager.add(new Separator("ecore2xml-additions")); //$NON-NLS-1$
190
}
191
192   /**
193    * This adds to the menu bar a menu and some separators for editor additions,
194    * as well as the sub-menus for object creation items.
195    * <!-- begin-user-doc -->
196    * <!-- end-user-doc -->
197    * @generated
198    */

199   public void contributeToMenu(IMenuManager menuManager)
200   {
201     super.contributeToMenu(menuManager);
202
203     IMenuManager submenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditor_menu"), "org.eclipse.emf.mapping.ecore2xmlMenuID"); //$NON-NLS-1$ //$NON-NLS-2$
204
menuManager.insertAfter("additions", submenuManager); //$NON-NLS-1$
205
submenuManager.add(new Separator("settings")); //$NON-NLS-1$
206
submenuManager.add(new Separator("actions")); //$NON-NLS-1$
207
submenuManager.add(new Separator("additions")); //$NON-NLS-1$
208
submenuManager.add(new Separator("additions-end")); //$NON-NLS-1$
209

210     // Prepare for CreateChild item addition or removal.
211
//
212
createChildMenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); //$NON-NLS-1$
213
submenuManager.insertBefore("additions", createChildMenuManager); //$NON-NLS-1$
214

215     // Prepare for CreateSibling item addition or removal.
216
//
217
createSiblingMenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); //$NON-NLS-1$
218
submenuManager.insertBefore("additions", createSiblingMenuManager); //$NON-NLS-1$
219

220     // Force an update because Eclipse hides empty menus now.
221
//
222
submenuManager.addMenuListener
223       (new IMenuListener()
224        {
225          public void menuAboutToShow(IMenuManager menuManager)
226          {
227            menuManager.updateAll(true);
228          }
229        });
230
231     addGlobalActions(submenuManager);
232   }
233
234   /**
235    * When the active editor changes, this remembers the change and registers with it as a selection provider.
236    * <!-- begin-user-doc -->
237    * <!-- end-user-doc -->
238    * @generated
239    */

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

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

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

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

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

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

433   public void menuAboutToShow(IMenuManager menuManager)
434   {
435     super.menuAboutToShow(menuManager);
436     MenuManager submenuManager = null;
437
438     submenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_CreateChild_menu_item")); //$NON-NLS-1$
439
populateManager(submenuManager, createChildActions, null);
440     menuManager.insertBefore("additions", submenuManager); //$NON-NLS-1$
441

442     submenuManager = new MenuManager(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_CreateSibling_menu_item")); //$NON-NLS-1$
443
populateManager(submenuManager, createSiblingActions, null);
444     menuManager.insertBefore("additions", submenuManager); //$NON-NLS-1$
445
}
446
447   /**
448    * This inserts global actions before the "additions-end" separator.
449    * <!-- begin-user-doc -->
450    * <!-- end-user-doc -->
451    * @generated
452    */

453   protected void addGlobalActions(IMenuManager menuManager)
454   {
455     menuManager.insertAfter("additions-end", new Separator("ui-actions")); //$NON-NLS-1$ //$NON-NLS-2$
456
menuManager.insertAfter("ui-actions", showPropertiesViewAction); //$NON-NLS-1$
457

458     refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
459     menuManager.insertAfter("ui-actions", refreshViewerAction); //$NON-NLS-1$
460

461     super.addGlobalActions(menuManager);
462   }
463
464 }
465
Popular Tags