1 package org.hibernate.eclipse.console.wizards; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.io.StringWriter ; 7 import java.io.UnsupportedEncodingException ; 8 import java.lang.reflect.InvocationTargetException ; 9 import java.util.Properties ; 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 34 35 public class NewConfigurationWizard extends Wizard implements INewWizard { 36 private NewConfigurationWizardPage page; 37 private ISelection selection; 38 private WizardNewFileCreationPage cPage; 39 40 43 public NewConfigurationWizard() { 44 super(); 45 setDefaultPageImageDescriptor(EclipseImages.getImageDescriptor(ImageConstants.NEW_WIZARD)); 46 setNeedsProgressMonitor(true); 47 } 48 49 50 static class ExtendedWizardNewFileCreationPage extends WizardNewFileCreationPage { 51 52 public ExtendedWizardNewFileCreationPage(String 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 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 88 public boolean performFinish() { 89 final Properties props = new Properties (); 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 { 99 try { 100 doFinish(file, props, monitor); 101 } catch (CoreException e) { 102 throw new InvocationTargetException (e); 103 } finally { 104 monitor.done(); 105 } 106 } 107 }; 108 try { 109 getContainer().run(true, false, op); 110 } catch (InterruptedException e) { 111 return false; 112 } catch (InvocationTargetException e) { 113 Throwable realException = e.getTargetException(); 114 HibernateConsolePlugin.getDefault().showError(getShell(), "Error", realException); 115 return false; 116 } 117 return true; 118 } 119 120 125 private void putIfNotNull(Properties props, String key, String value) { 126 if(value!=null) { 127 props.put(key,value); 128 } 129 } 130 131 138 139 private void doFinish( 140 final IFile file, Properties props, IProgressMonitor monitor) 141 throws CoreException { 142 monitor.beginTask("Creating " + file.getName(), 2); 144 try { 145 InputStream 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 e) { 153 } 154 monitor.worked(1); 155 monitor.setTaskName("Opening file for editing..."); 156 getShell().getDisplay().asyncExec(new Runnable () { 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 173 174 private InputStream openContentStream(Properties props) { 175 StringWriter stringWriter = new StringWriter (); 176 HibernateConfigurationExporter hce = new HibernateConfigurationExporter(); 177 hce.setCustomProperties(props); 178 hce.setOutput(stringWriter); 179 hce.start(); 180 try { 181 return new ByteArrayInputStream (stringWriter.toString().getBytes("UTF-8")); 182 } catch (UnsupportedEncodingException uec) { 183 HibernateConsolePlugin.getDefault().logErrorMessage("Problems converting to UTF-8", uec); 184 return new ByteArrayInputStream (stringWriter.toString().getBytes()); 185 } 186 } 187 188 193 public void init(IWorkbench workbench, IStructuredSelection selection) { 194 this.selection = selection; 195 } 196 } | Popular Tags |