KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > wizards > ConsoleConfigurationCreationWizard


1 /*
2  * Created on 2004-10-13
3  */

4 package org.hibernate.eclipse.console.wizards;
5
6 import java.lang.reflect.InvocationTargetException JavaDoc;
7
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.core.runtime.IPath;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.jface.dialogs.ErrorDialog;
14 import org.eclipse.jface.operation.IRunnableWithProgress;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.wizard.Wizard;
18 import org.eclipse.ui.INewWizard;
19 import org.eclipse.ui.IWorkbench;
20 import org.eclipse.ui.IWorkbenchWizard;
21 import org.hibernate.console.ConsoleConfiguration;
22 import org.hibernate.console.ConsoleConfigurationPreferences;
23 import org.hibernate.console.ImageConstants;
24 import org.hibernate.console.KnownConfigurations;
25 import org.hibernate.eclipse.console.EclipseConsoleConfiguration;
26 import org.hibernate.eclipse.console.HibernateConsolePlugin;
27 import org.hibernate.eclipse.console.utils.EclipseImages;
28
29 /**
30  * @author max
31  */

32 public class ConsoleConfigurationCreationWizard extends Wizard implements
33         INewWizard {
34
35     private ConsoleConfigurationWizardPage page;
36     private ISelection selection;
37
38     /**
39      * Constructor for SampleNewWizard.
40      */

41     public ConsoleConfigurationCreationWizard() {
42         super();
43         setDefaultPageImageDescriptor(EclipseImages.getImageDescriptor(ImageConstants.NEW_WIZARD));
44         setNeedsProgressMonitor(true);
45     }
46     
47     /**
48      * Adding the page to the wizard.
49      */

50
51     public void addPages() {
52         page = new ConsoleConfigurationWizardPage(selection);
53         addPage(page);
54     }
55
56     /**
57      * This method is called when 'Finish' button is pressed in
58      * the wizard. We will create an operation and run it
59      * using wizard as execution context.
60      */

61     public boolean performFinish() {
62         final String JavaDoc configName = page.getConfigurationName();
63         final IPath propertyFile = page.getPropertyFilePath();
64         final IPath fileName = page.getConfigurationFilePath();
65         final IPath[] mappings = page.getMappingFiles();
66         final IPath[] classpaths = page.getClassPath();
67         IRunnableWithProgress op = new IRunnableWithProgress() {
68             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
69                 try {
70                     doFinish(page.getOldConfiguration(), configName, propertyFile, fileName, mappings, classpaths, monitor);
71                 } catch (CoreException e) {
72                     throw new InvocationTargetException JavaDoc(e);
73                 } finally {
74                     monitor.done();
75                 }
76             }
77         };
78         try {
79             getContainer().run(true, false, op);
80         } catch (InterruptedException JavaDoc e) {
81             return false;
82         } catch (InvocationTargetException JavaDoc e) {
83             Throwable JavaDoc realException = e.getTargetException();
84             realException.printStackTrace();
85             IStatus s = null;
86             if(realException instanceof CoreException) {
87                 s = ((CoreException)realException).getStatus();
88             } else {
89                 s = new Status(Status.ERROR,HibernateConsolePlugin.ID, Status.OK, "Probably missing classes or errors with classloading", e);
90                 
91             }
92             ErrorDialog.openError(getShell(), "Create Conscole Configuration Wizard", "Error while finishing Wizard", s);
93             return false;
94         }
95         return true;
96     }
97     
98     /**
99      * The worker method. It will find the container, create the
100      * file if missing or just replace its contents, and open
101      * the editor on the newly created file.
102      * @param mappings
103      * @param classpaths
104      * @param fileName2
105      */

106
107     private void doFinish(
108             EclipseConsoleConfiguration oldConfig,
109             String JavaDoc configName,
110             IPath propertyFilename,
111             IPath cfgFile, IPath[] mappings, IPath[] classpaths, IProgressMonitor monitor)
112         throws CoreException {
113
114         monitor.beginTask("Configuring Hibernate Console" + propertyFilename, IProgressMonitor.UNKNOWN);
115                                 
116         ConsoleConfigurationPreferences ccp = new EclipseConsoleConfigurationPreferences(configName, cfgFile, propertyFilename, mappings, classpaths);
117         
118         final ConsoleConfiguration cfg = new EclipseConsoleConfiguration(ccp);
119             
120         if(oldConfig!=null) {
121             oldConfig.reset(); // reset it no matter what.
122
KnownConfigurations.getInstance().removeConfiguration(oldConfig);
123         }
124         KnownConfigurations.getInstance().addConfiguration(cfg, true);
125         
126         monitor.worked(1);
127     }
128     
129     
130     /**
131      * We will accept the selection in the workbench to see if
132      * we can initialize from it.
133      * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
134      */

135     public void init(IWorkbench workbench, IStructuredSelection selection) {
136         this.selection = selection;
137     }
138 }
139
Popular Tags