KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.eclipse.console.wizards;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.StringWriter JavaDoc;
7 import java.io.UnsupportedEncodingException JavaDoc;
8 import java.lang.reflect.InvocationTargetException JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IProgressMonitor;
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.IWorkbenchPage;
21 import org.eclipse.ui.PartInitException;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
24 import org.eclipse.ui.ide.IDE;
25 import org.hibernate.cfg.Environment;
26 import org.hibernate.console.ImageConstants;
27 import org.hibernate.eclipse.console.HibernateConsolePlugin;
28 import org.hibernate.eclipse.console.utils.EclipseImages;
29 import org.hibernate.tool.hbm2x.HibernateConfigurationExporter;
30
31 /**
32  * Creates a new hibernate.cfg.xml
33  */

34
35 public class NewConfigurationWizard extends Wizard implements INewWizard {
36     private NewConfigurationWizardPage page;
37     private ISelection selection;
38     private WizardNewFileCreationPage cPage;
39
40     /**
41      * Constructor for NewConfigurationWizard.
42      */

43     public NewConfigurationWizard() {
44         super();
45         setDefaultPageImageDescriptor(EclipseImages.getImageDescriptor(ImageConstants.NEW_WIZARD));
46         setNeedsProgressMonitor(true);
47     }
48
49     /** extended to update status messages on first show **/
50     static class ExtendedWizardNewFileCreationPage extends WizardNewFileCreationPage {
51
52         public ExtendedWizardNewFileCreationPage(String JavaDoc pageName, IStructuredSelection selection) {
53             super(pageName, selection);
54         }
55
56         boolean firstTime = true;
57         public void setVisible(boolean visible) {
58             super.setVisible(visible);
59             if(firstTime) {
60                 validatePage();
61                 firstTime = false;
62             }
63         }
64     }
65     /**
66      * Adding the page to the wizard.
67      */

68
69     public void addPages() {
70         cPage =
71         new ExtendedWizardNewFileCreationPage( "Ccfgxml", (IStructuredSelection) selection );
72         cPage.setTitle( "Create Hibernate Configuration file (cfg.xml)" );
73         cPage.setDescription( "Create a new hibernate.cfg.xml." );
74         cPage.setFileName("hibernate.cfg.xml");
75         addPage( cPage );
76         
77         page = new NewConfigurationWizardPage(selection, cPage);
78         addPage(page);
79     }
80     
81     
82
83     /**
84      * This method is called when 'Finish' button is pressed in
85      * the wizard. We will create an operation and run it
86      * using wizard as execution context.
87      */

88     public boolean performFinish() {
89         final Properties JavaDoc props = new Properties JavaDoc();
90         putIfNotNull(props, Environment.SESSION_FACTORY_NAME, page.getSessionFactoryName());
91         putIfNotNull(props, Environment.DIALECT, page.getDialect());
92         putIfNotNull(props, Environment.DRIVER, page.getDriver());
93         putIfNotNull(props, Environment.URL, page.getConnectionURL());
94         putIfNotNull(props, Environment.USER, page.getUsername());
95         putIfNotNull(props, Environment.PASS, page.getPassword());
96         final IFile file = cPage.createNewFile();
97         IRunnableWithProgress op = new IRunnableWithProgress() {
98             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
99                 try {
100                     doFinish(file, props, monitor);
101                 } catch (CoreException e) {
102                     throw new InvocationTargetException JavaDoc(e);
103                 } finally {
104                     monitor.done();
105                 }
106             }
107         };
108         try {
109             getContainer().run(true, false, op);
110         } catch (InterruptedException JavaDoc e) {
111             return false;
112         } catch (InvocationTargetException JavaDoc e) {
113             Throwable JavaDoc realException = e.getTargetException();
114             HibernateConsolePlugin.getDefault().showError(getShell(), "Error", realException);
115             return false;
116         }
117         return true;
118     }
119     
120     /**
121      * @param props
122      * @param dialect
123      * @param dialect2
124      */

125     private void putIfNotNull(Properties JavaDoc props, String JavaDoc key, String JavaDoc value) {
126         if(value!=null) {
127             props.put(key,value);
128         }
129     }
130
131     /**
132      * The worker method. It will find the container, create the
133      * file if missing or just replace its contents, and open
134      * the editor on the newly created file.
135      * @param file
136      * @param props
137      */

138
139     private void doFinish(
140         final IFile file, Properties JavaDoc props, IProgressMonitor monitor)
141         throws CoreException {
142         // create a sample file
143
monitor.beginTask("Creating " + file.getName(), 2);
144         try {
145             InputStream JavaDoc stream = openContentStream(props);
146             if (file.exists()) {
147                 file.setContents(stream, true, true, monitor);
148             } else {
149                 file.create(stream, true, monitor);
150             }
151             stream.close();
152         } catch (IOException JavaDoc e) {
153         }
154         monitor.worked(1);
155         monitor.setTaskName("Opening file for editing...");
156         getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
157             public void run() {
158                 IWorkbenchPage page =
159                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
160                 try {
161                     IDE.openEditor(page, file, true);
162                 } catch (PartInitException e) {
163                 }
164             }
165         });
166         monitor.worked(1);
167     }
168     
169     /**
170      * We will initialize file contents with a sample text.
171      * @throws UnsupportedEncodingException
172      */

173
174     private InputStream JavaDoc openContentStream(Properties JavaDoc props) {
175         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
176         HibernateConfigurationExporter hce = new HibernateConfigurationExporter();
177         hce.setCustomProperties(props);
178         hce.setOutput(stringWriter);
179         hce.start();
180         try {
181             return new ByteArrayInputStream JavaDoc(stringWriter.toString().getBytes("UTF-8"));
182         } catch (UnsupportedEncodingException JavaDoc uec) {
183             HibernateConsolePlugin.getDefault().logErrorMessage("Problems converting to UTF-8", uec);
184             return new ByteArrayInputStream JavaDoc(stringWriter.toString().getBytes());
185         }
186     }
187
188     /**
189      * We will accept the selection in the workbench to see if
190      * we can initialize from it.
191      * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
192      */

193     public void init(IWorkbench workbench, IStructuredSelection selection) {
194         this.selection = selection;
195     }
196 }
Popular Tags