KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > examples > jet > article2 > ui > NewTypesafeEnumCreationWizard


1 package org.eclipse.emf.examples.jet.article2.ui;
2
3
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5
6
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.resources.IResource;
9 import org.eclipse.core.resources.IWorkspaceRunnable;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.core.runtime.IStatus;
13 import org.eclipse.core.runtime.OperationCanceledException;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.jdt.core.JavaCore;
16 import org.eclipse.jface.dialogs.DialogSettings;
17 import org.eclipse.jface.dialogs.ErrorDialog;
18 import org.eclipse.jface.dialogs.IDialogSettings;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.wizard.Wizard;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.INewWizard;
25 import org.eclipse.ui.IWorkbench;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.PartInitException;
28 import org.eclipse.ui.ide.IDE;
29 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
30
31 import org.eclipse.emf.examples.jet.article2.TypesafeEnumPlugin;
32 import org.eclipse.emf.examples.jet.article2.codegen.Config;
33 import org.eclipse.emf.examples.jet.article2.codegen.JETGateway;
34
35
36 /**
37  * This <code>Wizard</code> sets up the wizard pages for the typesafe
38  * enumeration
39  *
40  * @author Remko Popma
41  * @version $Revision: 1.2 $ ($Date: 2004/06/01 16:17:41 $)
42  */

43 public class NewTypesafeEnumCreationWizard extends Wizard implements INewWizard
44 {
45
46   private IWorkbench mWorkbench;
47
48   private IStructuredSelection mSelection;
49
50   private static final String JavaDoc DIALOG_SETTINGS_KEY = "NewTypesafeEnumCreationWizard";
51
52   private NewTypesafeEnumCreationWizardPage mPage1;
53
54   private NewTypesafeEnumCreationWizardPageAttributes mPage2;
55
56   private NewTypesafeEnumCreationWizardPageInstances mPage3;
57
58   /**
59    * Constructs a <code>NewTypesafeEnumCreationWizard</code>.
60    */

61   public NewTypesafeEnumCreationWizard()
62   {
63     super();
64     setNeedsProgressMonitor(true);
65     setDefaultPageImageDescriptor(TypesafeEnumPlugin.getImageDescriptor("icons/newclass_wiz.gif"));
66     initDialogSettings();
67     setWindowTitle(WizardMessages.getString("Wizard.title.new")); //$NON-NLS-1$
68
}
69
70   /*
71    * (non-Javadoc)
72    *
73    * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
74    * org.eclipse.jface.viewers.IStructuredSelection)
75    */

76   public void init(IWorkbench workbench, IStructuredSelection currentSelection)
77   {
78     mWorkbench = workbench;
79     mSelection = currentSelection;
80   }
81
82   /*
83    * @see Wizard#addPages
84    */

85   public void addPages()
86   {
87     super.addPages();
88
89     mPage1 = new NewTypesafeEnumCreationWizardPage();
90     addPage(mPage1);
91     mPage1.init(getSelection());
92
93     mPage2 = new NewTypesafeEnumCreationWizardPageAttributes();
94     addPage(mPage2);
95
96     mPage3 = new NewTypesafeEnumCreationWizardPageInstances();
97     addPage(mPage3);
98   }
99
100   /**
101    * Obtains the user input from the wizard pages, creates a <code>Config</code>
102    * object that contains all this information, and delegates the source code
103    * generation to a <code>JETGateway</code> object. Finally, the generated
104    * Java source code file is opened in an editor.
105    */

106   protected void finishPage(IProgressMonitor monitor)
107   throws InterruptedException JavaDoc, CoreException
108   {
109     Config config = new Config();
110     config.setModel(mPage1.getTypesafeEnumModel());
111     config.setPackageName(mPage1.getPackageText());
112     config.setTargetFile(mPage1.getTypeName() + ".java");
113     config.setTargetFolder(mPage1.getPackageFragmentRootText());
114
115     config.setClasspathVariable("JET_TUTORIAL");
116     config.setPluginId(TypesafeEnumPlugin.getPluginId());
117     config.setTemplateRelativeUri("templates/TypeSafeEnumeration.javajet");
118
119     JETGateway gateway = new JETGateway(config);
120     String JavaDoc content = gateway.generate(monitor);
121     IFile file = gateway.save(monitor, content.getBytes());
122
123     selectAndReveal(file);
124     openResource(file);
125   }
126
127   /*
128    * @see Wizard#performFinish
129    */

130   public boolean performFinish()
131   {
132     IWorkspaceRunnable op = new IWorkspaceRunnable()
133       {
134         public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException
135         {
136           try
137           {
138             finishPage(monitor);
139           }
140           catch (InterruptedException JavaDoc e)
141           {
142             throw new OperationCanceledException(e.getMessage());
143           }
144         }
145       };
146
147     try
148     {
149       getContainer().run(false, true, new WorkbenchRunnableAdapter(op));
150     }
151     catch (InvocationTargetException JavaDoc e)
152     {
153       handleFinishException(getShell(), e);
154       return false;
155     }
156     catch (InterruptedException JavaDoc e)
157     {
158       return false;
159     }
160     return true;
161   }
162
163   /**
164    * An <code>IRunnableWithProgress</code> that adapts
165    * <code>IWorkspaceRunnable</code> so that it can be executed inside
166    * <code>IRunnableContext</code>.<code>OperationCanceledException</code>
167    * thrown by the apapted runnabled are caught and rethrown as a
168    * <code>InterruptedException</code>.
169    */

170   private static class WorkbenchRunnableAdapter implements IRunnableWithProgress
171   {
172
173     private IWorkspaceRunnable fWorkspaceRunnable;
174
175     public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable)
176     {
177       fWorkspaceRunnable = runnable;
178     }
179
180     /*
181      * @see IRunnableWithProgress#run(IProgressMonitor)
182      */

183     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc
184     {
185       try
186       {
187         JavaCore.run(fWorkspaceRunnable, monitor);
188       }
189       catch (OperationCanceledException e)
190       {
191         throw new InterruptedException JavaDoc(e.getMessage());
192       }
193       catch (CoreException e)
194       {
195         throw new InvocationTargetException JavaDoc(e);
196       }
197     }
198   }
199
200   protected void handleFinishException(Shell shell, InvocationTargetException JavaDoc e)
201   {
202     String JavaDoc exceptionMessage = "Error when generating the Typesafe Enumeration Class.";
203     if(e.getCause() != null && e.getCause().getMessage() != null)
204     {
205       exceptionMessage = e.getCause().getMessage();
206     }
207     else if(e.getMessage() != null)
208     {
209         exceptionMessage = e.getMessage();
210     }
211     
212     IStatus status = new Status(IStatus.ERROR, TypesafeEnumPlugin.getPluginId(), IStatus.ERROR, exceptionMessage, e);
213     TypesafeEnumPlugin.log(status);
214
215     String JavaDoc title = WizardMessages.getString("Wizard.op_error.title"); //$NON-NLS-1$
216
String JavaDoc message = WizardMessages.getString("Wizard.op_error.message"); //$NON-NLS-1$
217
ErrorDialog.openError(shell, title, message, status);
218   }
219
220   protected void openResource(final IResource resource)
221   {
222     if (resource.getType() == IResource.FILE)
223     {
224       final IWorkbenchPage activePage = TypesafeEnumPlugin.getActivePage();
225       if (activePage != null)
226       {
227         final Display display = getShell().getDisplay();
228         if (display != null)
229         {
230           display.asyncExec(new Runnable JavaDoc()
231             {
232               public void run()
233               {
234                 try
235                 {
236                   IDE.openEditor(activePage, (IFile)resource, true);
237                 }
238                 catch (PartInitException e)
239                 {
240                   TypesafeEnumPlugin.log(e);
241                 }
242               }
243             });
244         }
245       }
246     }
247   }
248
249   protected void selectAndReveal(IResource newResource)
250   {
251     BasicNewResourceWizard.selectAndReveal(newResource, mWorkbench.getActiveWorkbenchWindow());
252   }
253
254   public IStructuredSelection getSelection()
255   {
256     return mSelection;
257   }
258
259   protected void initDialogSettings()
260   {
261     IDialogSettings pluginSettings = TypesafeEnumPlugin.getDefault().getDialogSettings();
262     IDialogSettings wizardSettings = pluginSettings.getSection(DIALOG_SETTINGS_KEY);
263     if (wizardSettings == null)
264     {
265       wizardSettings = new DialogSettings(DIALOG_SETTINGS_KEY);
266       pluginSettings.addSection(wizardSettings);
267     }
268     setDialogSettings(wizardSettings);
269   }
270 }
Popular Tags