1 11 package org.eclipse.ltk.internal.ui.refactoring.scripting; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URI ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.ltk.internal.ui.refactoring.RefactoringLocationControl; 20 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin; 21 import org.eclipse.ltk.internal.ui.refactoring.util.SWTUtil; 22 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.events.DisposeEvent; 25 import org.eclipse.swt.events.DisposeListener; 26 import org.eclipse.swt.events.ModifyEvent; 27 import org.eclipse.swt.events.ModifyListener; 28 import org.eclipse.swt.events.SelectionAdapter; 29 import org.eclipse.swt.events.SelectionEvent; 30 import org.eclipse.swt.layout.GridData; 31 import org.eclipse.swt.layout.GridLayout; 32 import org.eclipse.swt.widgets.Button; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.FileDialog; 35 36 import org.eclipse.jface.dialogs.IDialogSettings; 37 import org.eclipse.jface.wizard.IWizard; 38 39 44 public class RefactoringScriptLocationControl extends Composite { 45 46 47 protected static final String SETTING_CLIPBOARD= "org.eclipse.ltk.ui.refactoring.useClipboard"; 49 50 protected static final String SETTING_HISTORY= "org.eclipse.ltk.ui.refactoring.scriptHistory"; 52 53 protected Button fExternalBrowseButton= null; 54 55 56 protected RefactoringLocationControl fExternalLocationControl= null; 57 58 59 protected Button fFromClipboardButton= null; 60 61 62 protected Button fFromExternalLocationButton= null; 63 64 65 protected URI fScriptLocation= null; 66 67 68 protected final IWizard fWizard; 69 70 78 public RefactoringScriptLocationControl(final IWizard wizard, final Composite parent) { 79 super(parent, SWT.NONE); 80 Assert.isNotNull(wizard); 81 fWizard= wizard; 82 setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 6, 0)); 83 setLayout(new GridLayout(3, false)); 84 boolean clipboard= false; 85 final IDialogSettings settings= fWizard.getDialogSettings(); 86 if (settings != null) 87 clipboard= settings.getBoolean(SETTING_CLIPBOARD); 88 fFromClipboardButton= new Button(this, SWT.RADIO); 89 fFromClipboardButton.setText(ScriptingMessages.ScriptLocationControl_clipboard_label); 90 fFromClipboardButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_BEGINNING, 3, 0)); 91 fFromClipboardButton.setSelection(clipboard); 92 fFromClipboardButton.addSelectionListener(new SelectionAdapter() { 93 94 public final void widgetSelected(final SelectionEvent event) { 95 final boolean selection= fFromClipboardButton.getSelection(); 96 fExternalLocationControl.setEnabled(!selection); 97 fExternalBrowseButton.setEnabled(!selection); 98 handleClipboardScriptChanged(); 99 } 100 }); 101 fFromExternalLocationButton= new Button(this, SWT.RADIO); 102 fFromExternalLocationButton.setText(ScriptingMessages.ScriptLocationControl_location_label); 103 fFromExternalLocationButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_BEGINNING, 1, 0)); 104 fFromExternalLocationButton.setSelection(!clipboard); 105 fFromExternalLocationButton.addSelectionListener(new SelectionAdapter() { 106 107 public final void widgetSelected(final SelectionEvent event) { 108 handleExternalLocationChanged(); 109 } 110 }); 111 fExternalLocationControl= new RefactoringLocationControl(fWizard, this, SETTING_HISTORY); 112 fExternalLocationControl.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 1, 0)); 113 fExternalLocationControl.setEnabled(!clipboard); 114 fExternalLocationControl.getControl().addModifyListener(new ModifyListener() { 115 116 public final void modifyText(final ModifyEvent event) { 117 handleExternalLocationChanged(); 118 } 119 }); 120 fExternalLocationControl.getControl().addSelectionListener(new SelectionAdapter() { 121 122 public void widgetSelected(final SelectionEvent event) { 123 handleExternalLocationChanged(); 124 } 125 }); 126 if (!clipboard) 127 fExternalLocationControl.setFocus(); 128 fExternalBrowseButton= new Button(this, SWT.PUSH); 129 fExternalBrowseButton.setText(ScriptingMessages.ScriptLocationControl_browse_label); 130 fExternalBrowseButton.setEnabled(!clipboard); 131 fExternalBrowseButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 1, 0)); 132 SWTUtil.setButtonDimensionHint(fExternalBrowseButton); 133 fExternalBrowseButton.addSelectionListener(new SelectionAdapter() { 134 135 public final void widgetSelected(final SelectionEvent event) { 136 handleBrowseExternalLocation(); 137 } 138 }); 139 addDisposeListener(new DisposeListener() { 140 141 public final void widgetDisposed(final DisposeEvent event) { 142 if (settings != null) 143 settings.put(SETTING_CLIPBOARD, fFromClipboardButton.getSelection()); 144 } 145 }); 146 } 147 148 159 protected GridData createGridData(final int flag, final int hspan, final int indent) { 160 final GridData data= new GridData(flag); 161 data.horizontalIndent= indent; 162 data.horizontalSpan= hspan; 163 return data; 164 } 165 166 173 public URI getRefactoringScript() { 174 return fScriptLocation; 175 } 176 177 180 protected void handleBrowseExternalLocation() { 181 final FileDialog file= new FileDialog(getShell(), SWT.OPEN); 182 file.setText(ScriptingMessages.ScriptLocationControl_browse_caption); 183 file.setFilterNames(new String [] { ScriptingMessages.ScriptLocationControl_filter_name_script, ScriptingMessages.ScriptLocationControl_filter_name_wildcard}); 184 file.setFilterExtensions(new String [] { ScriptingMessages.ScriptLocationControl_filter_extension_script, ScriptingMessages.ScriptLocationControl_filter_extension_wildcard}); 185 final String path= file.open(); 186 if (path != null) 187 fExternalLocationControl.setText(path); 188 } 189 190 193 protected void handleClipboardScriptChanged() { 194 } 196 197 200 protected void handleExternalLocationChanged() { 201 final String text= fExternalLocationControl.getText(); 202 if (text != null && !"".equals(text)) fScriptLocation= new File (text).toURI(); 204 else 205 fScriptLocation= null; 206 } 207 208 211 public void loadHistory() { 212 fExternalLocationControl.loadHistory(); 213 } 214 215 218 public void saveHistory() { 219 fExternalLocationControl.saveHistory(); 220 } 221 222 230 public void setRefactoringScript(final URI uri) { 231 if (fExternalLocationControl != null) 232 fExternalLocationControl.setEnabled(true); 233 if (fExternalBrowseButton != null) 234 fExternalBrowseButton.setEnabled(true); 235 if (uri == null) 236 fExternalLocationControl.setText(""); else { 238 try { 239 final String path= new File (uri).getCanonicalPath(); 240 if (path != null && !"".equals(path)) fExternalLocationControl.setText(path); 242 handleExternalLocationChanged(); 243 } catch (IOException exception) { 244 RefactoringUIPlugin.log(exception); 245 } 246 } 247 } 248 } | Popular Tags |