1 11 package org.eclipse.ltk.internal.ui.refactoring.scripting; 12 13 import java.io.BufferedInputStream ; 14 import java.io.BufferedOutputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileNotFoundException ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.UnsupportedEncodingException ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.net.URI ; 26 import java.util.HashSet ; 27 import java.util.Set ; 28 29 import org.eclipse.core.runtime.Assert; 30 import org.eclipse.core.runtime.CoreException; 31 import org.eclipse.core.runtime.IProgressMonitor; 32 33 import org.eclipse.ltk.core.refactoring.RefactoringCore; 34 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 35 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy; 36 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory; 37 38 import org.eclipse.ltk.internal.core.refactoring.IRefactoringSerializationConstants; 39 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryManager; 40 import org.eclipse.ltk.internal.ui.refactoring.Messages; 41 import org.eclipse.ltk.internal.ui.refactoring.RefactoringPluginImages; 42 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages; 43 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin; 44 45 import org.eclipse.swt.SWTError; 46 import org.eclipse.swt.dnd.Clipboard; 47 import org.eclipse.swt.dnd.TextTransfer; 48 import org.eclipse.swt.dnd.Transfer; 49 50 import org.eclipse.jface.dialogs.IDialogConstants; 51 import org.eclipse.jface.dialogs.IDialogSettings; 52 import org.eclipse.jface.dialogs.MessageDialog; 53 import org.eclipse.jface.operation.IRunnableWithProgress; 54 import org.eclipse.jface.wizard.IWizardContainer; 55 import org.eclipse.jface.wizard.Wizard; 56 57 62 public final class CreateRefactoringScriptWizard extends Wizard { 63 64 65 private static String DIALOG_SETTINGS_KEY= "CreateRefactoringScriptWizard"; 67 68 private boolean fNewSettings; 69 70 71 private RefactoringDescriptorProxy[] fRefactoringDescriptors= {}; 72 73 74 private RefactoringHistory fRefactoringHistory; 75 76 77 private URI fScriptLocation= null; 78 79 80 private boolean fUseClipboard= false; 81 82 83 private final CreateRefactoringScriptWizardPage fWizardPage; 84 85 88 public CreateRefactoringScriptWizard() { 89 setNeedsProgressMonitor(false); 90 setWindowTitle(ScriptingMessages.CreateRefactoringScriptWizard_caption); 91 setDefaultPageImageDescriptor(RefactoringPluginImages.DESC_WIZBAN_CREATE_SCRIPT); 92 final IDialogSettings settings= RefactoringUIPlugin.getDefault().getDialogSettings(); 93 final IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY); 94 if (section == null) 95 fNewSettings= true; 96 else { 97 fNewSettings= false; 98 setDialogSettings(section); 99 } 100 fWizardPage= new CreateRefactoringScriptWizardPage(this); 101 } 102 103 106 public void addPages() { 107 super.addPages(); 108 addPage(fWizardPage); 109 } 110 111 114 public boolean canFinish() { 115 return (fUseClipboard || fScriptLocation != null) && fRefactoringDescriptors.length > 0; 116 } 117 118 123 public RefactoringHistory getRefactoringHistory() { 124 return fRefactoringHistory; 125 } 126 127 133 private boolean performExport() { 134 RefactoringDescriptorProxy[] writable= fRefactoringDescriptors; 135 if (fScriptLocation != null) { 136 final File file= new File (fScriptLocation); 137 if (file.exists()) { 138 final MessageDialog message= new MessageDialog(getShell(), getShell().getText(), null, Messages.format(ScriptingMessages.CreateRefactoringScriptWizard_overwrite_query, new String [] { ScriptingMessages.CreateRefactoringScriptWizard_merge_button, ScriptingMessages.CreateRefactoringScriptWizard_overwrite_button}), MessageDialog.QUESTION, new String [] { ScriptingMessages.CreateRefactoringScriptWizard_merge_button, ScriptingMessages.CreateRefactoringScriptWizard_overwrite_button, IDialogConstants.CANCEL_LABEL}, 0); 139 final int result= message.open(); 140 if (result == 0) { 141 InputStream stream= null; 142 try { 143 stream= new BufferedInputStream (new FileInputStream (file)); 144 final RefactoringDescriptorProxy[] existing= RefactoringCore.getHistoryService().readRefactoringHistory(stream, RefactoringDescriptor.NONE).getDescriptors(); 145 final Set set= new HashSet (); 146 for (int index= 0; index < existing.length; index++) 147 set.add(existing[index]); 148 for (int index= 0; index < fRefactoringDescriptors.length; index++) 149 set.add(fRefactoringDescriptors[index]); 150 writable= new RefactoringDescriptorProxy[set.size()]; 151 set.toArray(writable); 152 } catch (FileNotFoundException exception) { 153 MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, exception.getLocalizedMessage()); 154 return true; 155 } catch (CoreException exception) { 156 final Throwable throwable= exception.getStatus().getException(); 157 if (throwable instanceof IOException ) { 158 MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, throwable.getLocalizedMessage()); 159 return true; 160 } else { 161 RefactoringUIPlugin.log(exception); 162 return false; 163 } 164 } finally { 165 if (stream != null) { 166 try { 167 stream.close(); 168 } catch (IOException exception) { 169 } 171 } 172 } 173 } else if (result == 2) 174 return false; 175 } 176 OutputStream stream= null; 177 try { 178 stream= new BufferedOutputStream (new FileOutputStream (file)); 179 writeRefactoringDescriptorProxies(writable, stream); 180 return true; 181 } catch (CoreException exception) { 182 final Throwable throwable= exception.getStatus().getException(); 183 if (throwable instanceof IOException ) { 184 MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, throwable.getLocalizedMessage()); 185 return true; 186 } else { 187 RefactoringUIPlugin.log(exception); 188 return false; 189 } 190 } catch (FileNotFoundException exception) { 191 MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, exception.getLocalizedMessage()); 192 return true; 193 } finally { 194 if (stream != null) { 195 try { 196 stream.close(); 197 } catch (IOException exception) { 198 } 200 } 201 } 202 } else if (fUseClipboard) { 203 try { 204 final ByteArrayOutputStream stream= new ByteArrayOutputStream (2048); 205 writeRefactoringDescriptorProxies(writable, stream); 206 try { 207 final String string= stream.toString(IRefactoringSerializationConstants.OUTPUT_ENCODING); 208 Clipboard clipboard= null; 209 try { 210 clipboard= new Clipboard(getShell().getDisplay()); 211 try { 212 clipboard.setContents(new Object [] { string}, new Transfer[] { TextTransfer.getInstance()}); 213 return true; 214 } catch (SWTError error) { 215 MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, error.getLocalizedMessage()); 216 return false; 217 } 218 } finally { 219 if (clipboard != null) 220 clipboard.dispose(); 221 } 222 } catch (UnsupportedEncodingException exception) { 223 return false; 225 } 226 } catch (CoreException exception) { 227 final Throwable throwable= exception.getStatus().getException(); 228 if (throwable instanceof IOException ) { 229 MessageDialog.openError(getShell(), RefactoringUIMessages.ChangeExceptionHandler_refactoring, throwable.getLocalizedMessage()); 230 return true; 231 } else { 232 RefactoringUIPlugin.log(exception); 233 return false; 234 } 235 } 236 } 237 return false; 238 } 239 240 243 public boolean performFinish() { 244 if (fNewSettings) { 245 final IDialogSettings settings= RefactoringUIPlugin.getDefault().getDialogSettings(); 246 IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY); 247 section= settings.addNewSection(DIALOG_SETTINGS_KEY); 248 setDialogSettings(section); 249 } 250 fWizardPage.performFinish(); 251 return performExport(); 252 } 253 254 260 public void setRefactoringDescriptors(final RefactoringDescriptorProxy[] proxies) { 261 Assert.isNotNull(proxies); 262 fRefactoringDescriptors= proxies; 263 final IWizardContainer wizard= getContainer(); 264 if (wizard.getCurrentPage() != null) 265 wizard.updateButtons(); 266 } 267 268 274 public void setRefactoringHistory(final RefactoringHistory history) { 275 Assert.isNotNull(history); 276 fRefactoringHistory= history; 277 } 278 279 285 public void setRefactoringScript(final URI location) { 286 fScriptLocation= location; 287 final IWizardContainer wizard= getContainer(); 288 if (wizard.getCurrentPage() != null) 289 wizard.updateButtons(); 290 } 291 292 300 public void setUseClipboard(final boolean clipboard) { 301 fUseClipboard= clipboard; 302 final IWizardContainer wizard= getContainer(); 303 if (wizard.getCurrentPage() != null) 304 wizard.updateButtons(); 305 } 306 307 317 private void writeRefactoringDescriptorProxies(final RefactoringDescriptorProxy[] writable, final OutputStream stream) throws CoreException { 318 RefactoringHistoryManager.sortRefactoringDescriptorsAscending(writable); 319 try { 320 getContainer().run(false, false, new IRunnableWithProgress() { 321 322 public void run(final IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 323 try { 324 RefactoringCore.getHistoryService().writeRefactoringDescriptors(writable, stream, RefactoringDescriptor.NONE, false, monitor); 325 } catch (CoreException exception) { 326 throw new InvocationTargetException (exception); 327 } 328 } 329 }); 330 } catch (InvocationTargetException exception) { 331 final Throwable throwable= exception.getTargetException(); 332 if (throwable instanceof CoreException) { 333 final CoreException extended= (CoreException) throwable; 334 throw extended; 335 } 336 } catch (InterruptedException exception) { 337 } 339 } 340 } | Popular Tags |