KickJava   Java API By Example, From Geeks To Geeks.

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


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.IWorkbenchWizard;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
25 import org.eclipse.ui.ide.IDE;
26 import org.hibernate.console.ImageConstants;
27 import org.hibernate.eclipse.console.HibernateConsolePlugin;
28 import org.hibernate.eclipse.console.utils.EclipseImages;
29
30 /**
31  * Creates a new reveng.xml
32  */

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

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

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

84     public boolean performFinish() {
85         final IFile file = cPage.createNewFile();
86         IRunnableWithProgress op = new IRunnableWithProgress() {
87             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
88                 try {
89                     doFinish(file, monitor);
90                 } catch (CoreException e) {
91                     throw new InvocationTargetException JavaDoc(e);
92                 } finally {
93                     monitor.done();
94                 }
95             }
96         };
97         try {
98             getContainer().run(true, false, op);
99         } catch (InterruptedException JavaDoc e) {
100             return false;
101         } catch (InvocationTargetException JavaDoc e) {
102             Throwable JavaDoc realException = e.getTargetException();
103             HibernateConsolePlugin.getDefault().showError(getShell(), "Error", realException);
104             return false;
105         }
106         return true;
107     }
108     
109     /**
110      * @param props
111      * @param dialect
112      * @param dialect2
113      */

114     private void putIfNotNull(Properties JavaDoc props, String JavaDoc key, String JavaDoc value) {
115         if(value!=null) {
116             props.put(key,value);
117         }
118     }
119
120     /**
121      * The worker method. It will find the container, create the
122      * file if missing or just replace its contents, and open
123      * the editor on the newly created file.
124      * @param file
125      * @param props
126      */

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

162
163     private InputStream JavaDoc openContentStream() {
164         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
165         stringWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
166                 "<!DOCTYPE hibernate-reverse-engineering PUBLIC \"-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd\" >\r\n" +
167                 "\r\n" +
168                 "<hibernate-reverse-engineering>\r\n" +
169                 "</hibernate-reverse-engineering>");
170         try {
171             return new ByteArrayInputStream JavaDoc(stringWriter.toString().getBytes("UTF-8"));
172         } catch (UnsupportedEncodingException JavaDoc uec) {
173             HibernateConsolePlugin.getDefault().logErrorMessage("Problems converting to UTF-8", uec);
174             return new ByteArrayInputStream JavaDoc(stringWriter.toString().getBytes());
175         }
176     }
177
178     /**
179      * We will accept the selection in the workbench to see if
180      * we can initialize from it.
181      * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
182      */

183     public void init(IWorkbench workbench, IStructuredSelection selection) {
184         this.selection = selection;
185     }
186 }
Popular Tags