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.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 33 34 public class NewReverseEngineeringFileWizard extends Wizard implements INewWizard { 35 private ISelection selection; 36 private WizardNewFileCreationPage cPage; 37 38 41 public NewReverseEngineeringFileWizard() { 42 super(); 43 setDefaultPageImageDescriptor(EclipseImages.getImageDescriptor(ImageConstants.NEW_WIZARD)); 44 setNeedsProgressMonitor(true); 45 } 46 47 48 static class ExtendedWizardNewFileCreationPage extends WizardNewFileCreationPage { 49 50 public ExtendedWizardNewFileCreationPage(String 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 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 84 public boolean performFinish() { 85 final IFile file = cPage.createNewFile(); 86 IRunnableWithProgress op = new IRunnableWithProgress() { 87 public void run(IProgressMonitor monitor) throws InvocationTargetException { 88 try { 89 doFinish(file, monitor); 90 } catch (CoreException e) { 91 throw new InvocationTargetException (e); 92 } finally { 93 monitor.done(); 94 } 95 } 96 }; 97 try { 98 getContainer().run(true, false, op); 99 } catch (InterruptedException e) { 100 return false; 101 } catch (InvocationTargetException e) { 102 Throwable realException = e.getTargetException(); 103 HibernateConsolePlugin.getDefault().showError(getShell(), "Error", realException); 104 return false; 105 } 106 return true; 107 } 108 109 114 private void putIfNotNull(Properties props, String key, String value) { 115 if(value!=null) { 116 props.put(key,value); 117 } 118 } 119 120 127 128 private void doFinish( 129 final IFile file, IProgressMonitor monitor) 130 throws CoreException { 131 monitor.beginTask("Creating " + file.getName(), 2); 133 try { 134 InputStream 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 e) { 142 } 143 monitor.worked(1); 144 monitor.setTaskName("Opening file for editing..."); 145 getShell().getDisplay().asyncExec(new Runnable () { 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 162 163 private InputStream openContentStream() { 164 StringWriter stringWriter = new StringWriter (); 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 (stringWriter.toString().getBytes("UTF-8")); 172 } catch (UnsupportedEncodingException uec) { 173 HibernateConsolePlugin.getDefault().logErrorMessage("Problems converting to UTF-8", uec); 174 return new ByteArrayInputStream (stringWriter.toString().getBytes()); 175 } 176 } 177 178 183 public void init(IWorkbench workbench, IStructuredSelection selection) { 184 this.selection = selection; 185 } 186 } | Popular Tags |