1 11 package org.eclipse.ui.editors.text; 12 13 import java.io.CharConversionException ; 14 import java.io.UnsupportedEncodingException ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.custom.BusyIndicator; 18 import org.eclipse.swt.events.SelectionAdapter; 19 import org.eclipse.swt.events.SelectionEvent; 20 import org.eclipse.swt.graphics.Color; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.widgets.Button; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Display; 25 import org.eclipse.swt.widgets.Label; 26 import org.eclipse.swt.widgets.Shell; 27 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.Preferences; 30 31 import org.eclipse.core.resources.ResourcesPlugin; 32 33 import org.eclipse.jface.action.IAction; 34 35 import org.eclipse.ui.IEditorInput; 36 import org.eclipse.ui.IWorkbenchWindow; 37 import org.eclipse.ui.PlatformUI; 38 import org.eclipse.ui.internal.editors.text.NLSUtility; 39 import org.eclipse.ui.texteditor.IDocumentProvider; 40 import org.eclipse.ui.texteditor.ITextEditorActionConstants; 41 import org.eclipse.ui.texteditor.StatusTextEditor; 42 import org.eclipse.ui.texteditor.TextEditorAction; 43 44 45 49 public class DefaultEncodingSupport implements IEncodingSupport { 50 51 52 private Preferences.IPropertyChangeListener fPropertyChangeListener; 53 54 private StatusTextEditor fTextEditor; 55 56 59 public DefaultEncodingSupport() { 60 super(); 61 } 62 63 68 public void initialize(StatusTextEditor textEditor) { 69 70 fTextEditor= textEditor; 71 72 fPropertyChangeListener= new Preferences.IPropertyChangeListener() { 73 public void propertyChange(Preferences.PropertyChangeEvent e) { 74 if (ResourcesPlugin.PREF_ENCODING.equals(e.getProperty())) { 75 Runnable runnable= new Runnable () { 76 public void run() { 77 setEncoding(null, false); } 79 }; 80 if (Display.getCurrent() != null) 81 runnable.run(); 82 else { 83 Shell shell; 85 if (fTextEditor != null) 86 shell= fTextEditor.getSite().getShell(); 87 else 88 shell= getActiveWorkbenchShell(); 89 Display display; 90 if (shell != null) 91 display= shell.getDisplay(); 92 else 93 display= Display.getDefault(); 94 display.asyncExec(runnable); 95 } 96 } 97 } 98 }; 99 100 Preferences p= ResourcesPlugin.getPlugin().getPluginPreferences(); 101 p.addPropertyChangeListener(fPropertyChangeListener); 102 } 103 104 107 public void dispose() { 108 Preferences p= ResourcesPlugin.getPlugin().getPluginPreferences(); 109 p.removePropertyChangeListener(fPropertyChangeListener); 110 111 fTextEditor= null; 112 } 113 114 118 public void reset() { 119 } 120 121 128 protected void setEncoding(String encoding, boolean overwrite) { 129 IDocumentProvider p= fTextEditor.getDocumentProvider(); 130 if (p instanceof IStorageDocumentProvider) { 131 final IEditorInput input= fTextEditor.getEditorInput(); 132 IStorageDocumentProvider provider= (IStorageDocumentProvider)p; 133 String current= provider.getEncoding(input); 134 if (!fTextEditor.isDirty()) { 135 String internal= encoding == null ? "" : encoding; boolean apply= (overwrite || current == null) && !internal.equals(current); 137 if (apply) { 138 provider.setEncoding(input, encoding); 139 Runnable encodingSetter= 140 new Runnable () { 141 public void run() { 142 fTextEditor.doRevertToSaved(); 143 } 144 }; 145 Display display= fTextEditor.getSite().getShell().getDisplay(); 146 if (display != null && !display.isDisposed()) 147 BusyIndicator.showWhile(display, encodingSetter); 148 else 149 encodingSetter.run(); 150 } 151 } 152 } 153 } 154 155 158 public void setEncoding(String encoding) { 159 setEncoding(encoding, true); 160 } 161 162 165 public String getEncoding() { 166 IDocumentProvider p= fTextEditor.getDocumentProvider(); 167 if (p instanceof IStorageDocumentProvider) { 168 IStorageDocumentProvider provider= (IStorageDocumentProvider) p; 169 return provider.getEncoding(fTextEditor.getEditorInput()); 170 } 171 return null; 172 } 173 174 177 public String getDefaultEncoding() { 178 IDocumentProvider p= fTextEditor.getDocumentProvider(); 179 if (p instanceof IStorageDocumentProvider) { 180 IStorageDocumentProvider provider= (IStorageDocumentProvider) p; 181 return provider.getDefaultEncoding(); 182 } 183 return null; 184 } 185 186 192 public String getStatusHeader(IStatus status) { 193 Throwable t= status.getException(); 194 195 if (t instanceof CharConversionException ) 196 return TextEditorMessages.Editor_error_unreadable_encoding_header; 197 198 if (t instanceof UnsupportedEncodingException ) 199 return TextEditorMessages.Editor_error_unsupported_encoding_header; 200 201 return null; 202 } 203 204 210 public String getStatusBanner(IStatus status) { 211 Throwable t= status.getException(); 212 213 if (t instanceof CharConversionException ) 214 return TextEditorMessages.Editor_error_unreadable_encoding_banner; 215 216 if (t instanceof UnsupportedEncodingException ) 217 return TextEditorMessages.Editor_error_unsupported_encoding_banner; 218 219 return null; 220 221 } 222 223 229 public String getStatusMessage(IStatus status) { 230 Throwable t= status.getException(); 231 if (t instanceof CharConversionException || t instanceof UnsupportedEncodingException ) { 232 233 String encoding= getEncoding(); 234 if (encoding == null) 235 encoding= getDefaultEncoding(); 236 237 if (t instanceof CharConversionException ) { 238 if (encoding != null) 239 return NLSUtility.format(TextEditorMessages.Editor_error_unreadable_encoding_message_arg, encoding); 240 return TextEditorMessages.Editor_error_unreadable_encoding_message; 241 } 242 243 if (t instanceof UnsupportedEncodingException ) { 244 if (encoding != null) 245 return NLSUtility.format(TextEditorMessages.Editor_error_unsupported_encoding_message_arg, encoding); 246 return TextEditorMessages.Editor_error_unsupported_encoding_message; 247 } 248 } 249 250 return null; 251 } 252 253 261 public boolean isEncodingError(IStatus status) { 262 if (status == null || status.getSeverity() != IStatus.ERROR) 263 return false; 264 265 Throwable t= status.getException(); 266 return t instanceof CharConversionException || t instanceof UnsupportedEncodingException ; 267 } 268 269 278 public void createStatusEncodingChangeControl(Composite parent, final IStatus status) { 279 final IAction action= fTextEditor.getAction(ITextEditorActionConstants.CHANGE_ENCODING); 280 if (action instanceof TextEditorAction) 281 ((TextEditorAction)action).update(); 282 283 if (action == null || !action.isEnabled()) 284 return; 285 286 Shell shell= parent.getShell(); 287 Display display= shell.getDisplay(); 288 Color bgColor= display.getSystemColor(SWT.COLOR_LIST_BACKGROUND); 289 290 Button button= new Button(parent, SWT.PUSH | SWT.FLAT); 291 button.setText(action.getText()); 292 button.addSelectionListener(new SelectionAdapter() { 293 296 public void widgetSelected(SelectionEvent e) { 297 action.run(); 298 } 299 }); 300 301 Label filler= new Label(parent, SWT.NONE); 302 filler.setLayoutData(new GridData(GridData.FILL_BOTH)); 303 filler.setBackground(bgColor); 304 } 305 306 312 private static Shell getActiveWorkbenchShell() { 313 IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 314 if (window != null) 315 return window.getShell(); 316 317 return null; 318 } 319 320 } 321 | Popular Tags |