1 11 package org.eclipse.ltk.internal.ui.refactoring.scripting; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.UnsupportedEncodingException ; 20 import java.net.URI ; 21 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.core.runtime.CoreException; 24 25 import org.eclipse.ltk.core.refactoring.RefactoringCore; 26 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 27 28 import org.eclipse.ltk.internal.core.refactoring.IRefactoringSerializationConstants; 29 import org.eclipse.ltk.internal.ui.refactoring.IRefactoringHelpContextIds; 30 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin; 31 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.dnd.Clipboard; 34 import org.eclipse.swt.dnd.TextTransfer; 35 import org.eclipse.swt.layout.GridData; 36 import org.eclipse.swt.layout.GridLayout; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Group; 39 40 import org.eclipse.jface.dialogs.Dialog; 41 import org.eclipse.jface.wizard.WizardPage; 42 43 import org.eclipse.ui.PlatformUI; 44 45 50 public final class ApplyRefactoringScriptWizardPage extends WizardPage { 51 52 53 private static final String PAGE_NAME= "ApplyRefactoringScriptWizardPage"; 55 56 private boolean fFirstTime= true; 57 58 59 private RefactoringScriptLocationControl fLocationControl= null; 60 61 62 private final ApplyRefactoringScriptWizard fWizard; 63 64 70 public ApplyRefactoringScriptWizardPage(final ApplyRefactoringScriptWizard wizard) { 71 super(PAGE_NAME); 72 Assert.isNotNull(wizard); 73 fWizard= wizard; 74 setTitle(ScriptingMessages.ApplyRefactoringScriptWizard_title); 75 setDescription(ScriptingMessages.ApplyRefactoringScriptWizard_description); 76 } 77 78 81 public boolean canFlipToNextPage() { 82 return super.canFlipToNextPage(); 83 } 84 85 88 public void createControl(final Composite parent) { 89 initializeDialogUnits(parent); 90 final Composite composite= new Composite(parent, SWT.NONE); 91 composite.setLayout(new GridLayout()); 92 composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL)); 93 94 final Group group= new Group(composite, SWT.NONE); 95 group.setText(ScriptingMessages.ApplyRefactoringScriptWizardPage_location_caption); 96 final GridLayout layout= new GridLayout(); 97 layout.marginWidth= 0; 98 group.setLayout(layout); 99 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 100 101 fLocationControl= new RefactoringScriptLocationControl(fWizard, group) { 102 103 protected final void handleClipboardScriptChanged() { 104 super.handleClipboardScriptChanged(); 105 ApplyRefactoringScriptWizardPage.this.fWizard.setRefactoringHistory(null); 106 setErrorMessage(null); 107 setPageComplete(true); 108 handleClipboardChanged(); 109 } 110 111 protected final void handleExternalLocationChanged() { 112 super.handleExternalLocationChanged(); 113 ApplyRefactoringScriptWizardPage.this.fWizard.setRefactoringHistory(null); 114 setErrorMessage(null); 115 setPageComplete(true); 116 handleLocationChanged(); 117 } 118 }; 119 setPageComplete(false); 120 fLocationControl.loadHistory(); 121 fFirstTime= false; 122 setControl(composite); 123 Dialog.applyDialogFont(composite); 124 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IRefactoringHelpContextIds.REFACTORING_APPLY_SCRIPT_PAGE); 125 } 126 127 130 private void handleClipboardChanged() { 131 Clipboard clipboard= null; 132 try { 133 clipboard= new Clipboard(fLocationControl.getDisplay()); 134 final Object contents= clipboard.getContents(TextTransfer.getInstance()); 135 if (contents == null) { 136 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_empty_clipboard); 137 setPageComplete(false); 138 return; 139 } 140 if (contents instanceof String ) { 141 final String script= (String ) contents; 142 try { 143 final ByteArrayInputStream stream= new ByteArrayInputStream (script.getBytes(IRefactoringSerializationConstants.OUTPUT_ENCODING)); 144 fWizard.setRefactoringHistory(RefactoringCore.getHistoryService().readRefactoringHistory(stream, RefactoringDescriptor.NONE)); 145 } catch (UnsupportedEncodingException exception) { 146 } catch (CoreException exception) { 148 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_no_script_clipboard); 149 setPageComplete(false); 150 return; 151 } 152 } else { 153 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_no_text_clipboard); 154 setPageComplete(false); 155 } 156 } finally { 157 if (clipboard != null) 158 clipboard.dispose(); 159 } 160 } 161 162 165 private void handleLocationChanged() { 166 final URI uri= fLocationControl.getRefactoringScript(); 167 if (uri == null) { 168 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_invalid_location); 169 setPageComplete(false); 170 return; 171 } 172 final File file= new File (uri); 173 if (!file.exists()) { 174 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_invalid_script_file); 175 setPageComplete(false); 176 return; 177 } 178 InputStream stream= null; 179 try { 180 stream= new BufferedInputStream (new FileInputStream (file)); 181 fWizard.setRefactoringHistory(RefactoringCore.getHistoryService().readRefactoringHistory(stream, RefactoringDescriptor.NONE)); 182 } catch (IOException exception) { 183 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_error_cannot_read); 184 setPageComplete(false); 185 return; 186 } catch (CoreException exception) { 187 setErrorMessage(ScriptingMessages.ApplyRefactoringScriptWizardPage_invalid_format); 188 setPageComplete(false); 189 return; 190 } finally { 191 if (stream != null) { 192 try { 193 stream.close(); 194 } catch (IOException exception) { 195 } 197 } 198 } 199 } 200 201 204 public void performFinish() { 205 fLocationControl.saveHistory(); 206 } 207 208 211 public void setErrorMessage(final String message) { 212 if (!fFirstTime) 213 super.setErrorMessage(message); 214 else 215 setMessage(message, NONE); 216 } 217 218 221 public void setVisible(final boolean visible) { 222 if (visible) { 223 final URI uri= fWizard.getRefactoringScript(); 224 if (uri != null) { 225 fWizard.setRefactoringScript(null); 226 try { 227 fLocationControl.setRefactoringScript(uri); 228 handleLocationChanged(); 229 } catch (IllegalArgumentException exception) { 230 RefactoringUIPlugin.log(exception); 231 } 232 } 233 } 234 super.setVisible(visible); 235 } 236 } 237 | Popular Tags |