KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > ecore > genmodel > presentation > GenModelActionBarContributor


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

17 package org.eclipse.emf.codegen.ecore.genmodel.presentation;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26 import org.eclipse.emf.codegen.ecore.genmodel.GenBase;
27 import org.eclipse.emf.codegen.ecore.genmodel.provider.GenModelEditPlugin;
28 import org.eclipse.emf.common.ui.viewer.IViewerProvider;
29 import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor;
30 import org.eclipse.jface.action.Action;
31 import org.eclipse.jface.action.IAction;
32 import org.eclipse.jface.action.IContributionItem;
33 import org.eclipse.jface.action.IMenuManager;
34 import org.eclipse.jface.action.IToolBarManager;
35 import org.eclipse.jface.action.MenuManager;
36 import org.eclipse.jface.action.Separator;
37 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
38 import org.eclipse.jface.viewers.ISelection;
39 import org.eclipse.jface.viewers.ISelectionChangedListener;
40 import org.eclipse.jface.viewers.ISelectionProvider;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.jface.viewers.SelectionChangedEvent;
43 import org.eclipse.jface.viewers.Viewer;
44 import org.eclipse.ui.IEditorPart;
45 import org.eclipse.ui.PartInitException;
46 import org.eclipse.ui.actions.WorkspaceModifyOperation;
47 //
48
//import org.eclipse.emf.edit.ui.action.LoadResourceAction;
49
//import org.eclipse.emf.edit.ui.action.ValidateAction;
50
// import java.util.LinkedList;
51
// import org.eclipse.emf.codegen.ecore.genmodel.GenPropertyKind;
52
// import org.eclipse.jface.action.ActionContributionItem;
53
// import org.eclipse.jface.action.IAction;
54
// import org.eclipse.jface.action.IContributionManager;
55
// import org.eclipse.jface.action.SubContributionItem;
56

57
58 /**
59  * This is the action bar contributor for the GenModel model editor.
60  * <!-- begin-user-doc -->
61  * <!-- end-user-doc -->
62  * @generated
63  */

64 public class GenModelActionBarContributor
65   extends EditingDomainActionBarContributor
66   implements ISelectionChangedListener
67 {
68   /**
69    * This keeps track of the active editor.
70    */

71   protected IEditorPart activeEditorPart;
72
73   /**
74    * This gets the selection from the active editor.
75    */

76   protected ISelection getActiveEditorSelection()
77   {
78     return activeEditorPart == null ? null :
79       ((GenModelEditor)activeEditorPart).getSelection();
80   }
81
82   /**
83    * This keeps track of the current selection provider.
84    */

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

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

116   protected IAction refreshViewerAction =
117     new Action(GenModelEditPlugin.INSTANCE.getString("_UI_RefreshViewer_menu_item"))
118     {
119       public boolean isEnabled()
120       {
121         return activeEditorPart instanceof IViewerProvider;
122       }
123
124       public void run()
125       {
126         if (activeEditorPart instanceof IViewerProvider)
127         {
128           Viewer viewer = ((IViewerProvider)activeEditorPart).getViewer();
129           if (viewer != null)
130           {
131             viewer.refresh();
132           }
133         }
134       }
135     };
136
137   /**
138    * This is the menu manager for the "Generate" menu.
139    */

140   protected IMenuManager generateMenuManager;
141
142   protected IAction generateAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateModel_menu_item"))
143   {
144     protected boolean canGenerate(GenBase genObject)
145     {
146       return genObject.canGenerate();
147     }
148
149     protected void generate(GenBase genObject, IProgressMonitor progressMonitor)
150     {
151       genObject.generate(progressMonitor);
152     }
153   };
154
155   protected IAction generateEditAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateEdit_menu_item"))
156   {
157     protected boolean canGenerate(GenBase genObject)
158     {
159       return genObject.canGenerateEdit();
160     }
161
162     protected void generate(GenBase genObject, IProgressMonitor progressMonitor)
163     {
164       genObject.generateEdit(progressMonitor);
165     }
166   };
167
168   protected IAction generateEditorAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateEditor_menu_item"))
169   {
170     protected boolean canGenerate(GenBase genObject)
171     {
172       return genObject.canGenerateEditor();
173     }
174
175     protected void generate(GenBase genObject, IProgressMonitor progressMonitor)
176     {
177       genObject.generateEditor(progressMonitor);
178     }
179   };
180   
181   protected IAction generateTestsAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateTests_menu_item"))
182   {
183     protected boolean canGenerate(GenBase genObject)
184     {
185       return genObject.canGenerateTests();
186     }
187     
188     protected void generate(GenBase genObject, IProgressMonitor progressMonitor)
189     {
190       genObject.generateTests(progressMonitor);
191     }
192   };
193   
194   protected IAction generateSchemaAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateSchema_menu_item"))
195   {
196     protected boolean canGenerate(GenBase genObject)
197     {
198       return genObject.canGenerateSchema();
199     }
200
201     protected void generate(GenBase genObject, IProgressMonitor progressMonitor)
202     {
203       progressMonitor.beginTask("", 1);
204       genObject.generateSchema(new SubProgressMonitor(progressMonitor, 1));
205     }
206   };
207
208   protected IAction generateAllAction = new GenerateAction(GenModelEditPlugin.INSTANCE.getString("_UI_GenerateAll_menu_item"))
209   {
210     protected boolean canGenerate(GenBase genObject)
211     {
212       return genObject.canGenerate() || genObject.canGenerateEdit() ||
213         genObject.canGenerateEditor();
214     }
215
216     protected void generate(GenBase genObject, IProgressMonitor progressMonitor)
217     {
218       progressMonitor.beginTask("", 4);
219       genObject.generate(new SubProgressMonitor(progressMonitor, 1));
220       genObject.generateEdit(new SubProgressMonitor(progressMonitor, 1));
221       genObject.generateEditor(new SubProgressMonitor(progressMonitor, 1));
222       genObject.generateTests(new SubProgressMonitor(progressMonitor, 1));
223     }
224   };
225
226   /**
227    * This is a base class for the "Generate..." actions.
228    */

229   protected abstract class GenerateAction extends Action
230   {
231     public GenerateAction(String JavaDoc text)
232     {
233       super(text);
234     }
235
236     protected abstract boolean canGenerate(GenBase genObject);
237     protected abstract void generate(GenBase genObject, IProgressMonitor progressMonitor);
238
239     public boolean isEnabled()
240     {
241       ISelection s = getActiveEditorSelection();
242       if (!(s instanceof IStructuredSelection))
243       {
244         return false;
245       }
246
247       IStructuredSelection ss = (IStructuredSelection) s;
248       if (ss.size() == 0)
249       {
250         return false;
251       }
252
253       for (Iterator JavaDoc iter = ss.iterator(); iter.hasNext(); )
254       {
255         Object JavaDoc selected = iter.next();
256         if (!(selected instanceof GenBase) || !canGenerate((GenBase)selected))
257         {
258           return false;
259         }
260       }
261       return true;
262     }
263   
264     public void run()
265     {
266       // Do the work within an operation because this is a long running activity that modifies the workbench.
267
WorkspaceModifyOperation operation = new WorkspaceModifyOperation()
268       {
269         // This is the method that gets invoked when the operation runs.
270
//
271
protected void execute(IProgressMonitor progressMonitor) throws CoreException
272         {
273           Collection JavaDoc selection = ((IStructuredSelection)getActiveEditorSelection()).toList();
274           progressMonitor.beginTask("", selection.size());
275           try
276           {
277             for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext(); )
278             {
279               generate((GenBase)iter.next(), new SubProgressMonitor(progressMonitor, 1));
280             }
281           }
282           catch (Exception JavaDoc exception)
283           {
284             GenModelEditPlugin.INSTANCE.log(exception);
285           }
286           progressMonitor.done();
287         }
288       };
289     
290       // This runs the options, and shows progress.
291
// (It appears to be a bad thing to fork this onto another thread.)
292
//
293
try
294       {
295         new ProgressMonitorDialog(activeEditorPart.getSite().getShell()).run(true, false, operation);
296       }
297       catch (Exception JavaDoc exception)
298       {
299         // Something went wrong that shouldn't.
300
//
301
GenModelEditPlugin.INSTANCE.log(exception);
302       }
303     }
304   }
305
306   /**
307    * This creates an instance of the contributor.
308    */

309   public GenModelActionBarContributor()
310   {
311   }
312
313   /**
314    * This adds menu contributions for the generate actions.
315    */

316   public void contributeToMenu(IMenuManager menuManager)
317   {
318     super.contributeToMenu(menuManager);
319
320     generateMenuManager =
321       new MenuManager(GenModelEditPlugin.INSTANCE.getString("_UI_Generate_menu"), "org.eclipse.emf.codegen.ecore.genmodelMenuID");
322     menuManager.insertAfter("additions", generateMenuManager);
323     generateMenuManager.add(generateAction);
324     generateMenuManager.add(generateEditAction);
325     generateMenuManager.add(generateEditorAction);
326     generateMenuManager.add(generateTestsAction);
327     generateMenuManager.add(generateAllAction);
328
329     generateMenuManager.add(new Separator("schema-actions"));
330     generateMenuManager.add(generateSchemaAction);
331     
332     generateMenuManager.add(new Separator("global-actions"));
333   }
334
335   /**
336    * This adds Separators for editor additions to the tool bar.
337    */

338   public void contributeToToolBar(IToolBarManager toolBarManager)
339   {
340     toolBarManager.add(new Separator("genmodel-settings"));
341     toolBarManager.add(new Separator("genmodel-additions"));
342   }
343
344   /**
345    * When the active editor changes, this remembers the change,
346    */

347   public void setActiveEditor(IEditorPart part)
348   {
349     super.setActiveEditor(part);
350     activeEditorPart = part;
351
352     // Switch to the new selection provider.
353
//
354
if (selectionProvider != null)
355     {
356       selectionProvider.removeSelectionChangedListener(this);
357     }
358     if (part == null)
359     {
360       selectionProvider = null;
361     }
362     else
363     {
364       selectionProvider = part.getSite().getSelectionProvider();
365       selectionProvider.addSelectionChangedListener(this);
366
367       // Fake a selection changed event to update the menus.
368
//
369
if (selectionProvider.getSelection() != null)
370       {
371         selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection()));
372       }
373     }
374   }
375
376   /**
377    * This implements {@link ISelectionChangedListener}, refreshing the
378    * "Generate..." action contribution managers in the pull-down menu.
379    */

380   public void selectionChanged(SelectionChangedEvent event)
381   {
382     IContributionItem[] items = generateMenuManager.getItems();
383     for (int i = 0, len = items.length; i < len; i++) items[i].update();
384   }
385
386   /**
387    * This populates the pop-up menu before it appears.
388    */

389   public void menuAboutToShow(IMenuManager menuManager)
390   {
391     generateAllAction.setEnabled(generateAllAction.isEnabled());
392     generateSchemaAction.setEnabled(generateSchemaAction.isEnabled());
393     generateTestsAction.setEnabled(generateTestsAction.isEnabled());
394     generateEditorAction.setEnabled(generateEditorAction.isEnabled());
395     generateEditAction.setEnabled(generateEditAction.isEnabled());
396     generateAction.setEnabled(generateAction.isEnabled());
397     refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
398     
399     super.menuAboutToShow(menuManager);
400     menuManager.insertBefore("additions", new Separator("generate-actions"));
401     menuManager.insertAfter("generate-actions", generateAllAction);
402     menuManager.insertAfter("generate-actions", generateTestsAction);
403     menuManager.insertAfter("generate-actions", generateEditorAction);
404     menuManager.insertAfter("generate-actions", generateEditAction);
405     menuManager.insertAfter("generate-actions", generateAction);
406
407     menuManager.insertBefore("additions", new Separator("schema-actions"));
408     menuManager.insertAfter("schema-actions", generateSchemaAction);
409   }
410
411   /**
412    * This inserts global actions before the "additions-end" separator.
413    * <!-- begin-user-doc -->
414    * <!-- end-user-doc -->
415    * @generated
416    */

417   protected void addGlobalActions(IMenuManager menuManager)
418   {
419     menuManager.insertAfter("additions-end", new Separator("ui-actions"));
420     menuManager.insertAfter("ui-actions", showPropertiesViewAction);
421
422     refreshViewerAction.setEnabled(refreshViewerAction.isEnabled());
423     menuManager.insertAfter("ui-actions", refreshViewerAction);
424
425     super.addGlobalActions(menuManager);
426   }
427
428 }
429
Popular Tags