1 11 package org.eclipse.ui.editors.text; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.ResourceBundle ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.content.IContentDescription; 21 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.action.IMenuListener; 24 import org.eclipse.jface.action.IMenuManager; 25 import org.eclipse.jface.action.MenuManager; 26 import org.eclipse.jface.action.Separator; 27 import org.eclipse.jface.dialogs.IInputValidator; 28 import org.eclipse.jface.dialogs.InputDialog; 29 import org.eclipse.jface.window.Window; 30 31 import org.eclipse.ui.IActionBars; 32 import org.eclipse.ui.IEditorInput; 33 import org.eclipse.ui.IFileEditorInput; 34 import org.eclipse.ui.IWorkbenchActionConstants; 35 import org.eclipse.ui.actions.ActionGroup; 36 import org.eclipse.ui.internal.editors.text.NLSUtility; 37 import org.eclipse.ui.texteditor.ITextEditor; 38 import org.eclipse.ui.texteditor.IUpdate; 39 import org.eclipse.ui.texteditor.ResourceAction; 40 import org.eclipse.ui.texteditor.RetargetTextEditorAction; 41 import org.eclipse.ui.texteditor.TextEditorAction; 42 43 44 49 public class EncodingActionGroup extends ActionGroup { 50 51 private static final String FILE_CONTENT_ENCODING_FORMAT= TextEditorMessages.ResourceInfo_fileContentEncodingFormat; 52 private static final String FILE_CONTAINER_ENCODING_FORMAT= TextEditorMessages.ResourceInfo_fileContainerEncodingFormat; 53 54 55 59 static class PredefinedEncodingAction extends TextEditorAction { 60 61 62 private String fEncoding; 63 64 private String fLabel; 65 66 private boolean fIsDefault; 67 68 76 public PredefinedEncodingAction(ResourceBundle bundle, String prefix, String encoding, ITextEditor editor) { 77 super(bundle, prefix, editor); 78 fEncoding= encoding; 79 if (prefix == null) 80 setText(encoding); 81 fLabel= getText(); 82 } 83 84 91 public PredefinedEncodingAction(ResourceBundle bundle, String encoding, ITextEditor editor) { 92 super(bundle, null, editor); 93 fEncoding= encoding; 94 setText(encoding); 95 fLabel= getText(); 96 } 97 98 103 private IEncodingSupport getEncodingSupport() { 104 ITextEditor editor= getTextEditor(); 105 if (editor != null) 106 return (IEncodingSupport) editor.getAdapter(IEncodingSupport.class); 107 return null; 108 } 109 110 113 public void run() { 114 IEncodingSupport s= getEncodingSupport(); 115 if (s != null) 116 s.setEncoding(fIsDefault ? null : fEncoding); 117 } 118 119 125 private String getEncoding(ITextEditor editor) { 126 127 IEditorInput input= (editor.getEditorInput()); 128 if (input instanceof IFileEditorInput) { 129 IFile file= ((IFileEditorInput)input).getFile(); 130 try { 131 String explicitEncoding; 132 explicitEncoding= file.getCharset(false); 133 if (explicitEncoding == null) 134 return null; 135 } catch (CoreException e) { 136 } 138 } 139 140 IEncodingSupport s= getEncodingSupport(); 141 if (s != null) 142 return s.getEncoding(); 143 144 return null; 145 } 146 147 150 public void update() { 151 152 if (fEncoding == null) { 153 setEnabled(false); 154 return; 155 } 156 157 ITextEditor editor= getTextEditor(); 158 if (editor == null) { 159 setEnabled(false); 160 return; 161 } 162 163 fIsDefault= IEncodingActionsConstants.DEFAULT.equals(fEncoding); 165 if (fIsDefault) 166 setText(getDefaultEncodingText(editor, fLabel)); 167 else 168 setText(fLabel); 169 170 if (editor.isDirty()) 172 setEnabled(false); 173 else 174 setEnabled(true); 175 176 String current= getEncoding(editor); 178 if (fIsDefault) 179 setChecked(current == null); 180 else 181 setChecked(fEncoding.equals(current)); 182 } 183 184 } 185 186 189 private static String getDefaultEncodingText(ITextEditor editor, String defaultText) { 190 IEditorInput input= (editor.getEditorInput()); 191 if (!(input instanceof IFileEditorInput)) 192 return defaultText; 193 194 IFile file= ((IFileEditorInput)input).getFile(); 195 196 String format= FILE_CONTENT_ENCODING_FORMAT; 197 String encoding; 198 try { 199 encoding= getEncodingFromContent(file); 200 if (encoding == null) { 201 format= FILE_CONTAINER_ENCODING_FORMAT; 202 encoding= file.getParent().getDefaultCharset(); 203 } 204 } catch (CoreException ex) { 205 return defaultText; 206 } 207 208 return NLSUtility.format(format, encoding); 209 } 210 211 214 private static String getEncodingFromContent(IFile file) throws CoreException { 215 IContentDescription description = file.getContentDescription(); 216 if (description != null) { 217 byte[] bom= (byte[])description.getProperty(IContentDescription.BYTE_ORDER_MARK); 218 if (bom == null) 219 return (String )description.getProperty(IContentDescription.CHARSET); 220 if (bom == IContentDescription.BOM_UTF_8) 221 return TextEditorMessages.WorkbenchPreference_encoding_BOM_UTF_8; 222 if (bom == IContentDescription.BOM_UTF_16BE) 223 return TextEditorMessages.WorkbenchPreference_encoding_BOM_UTF_16BE; 224 if (bom == IContentDescription.BOM_UTF_16LE) 225 return TextEditorMessages.WorkbenchPreference_encoding_BOM_UTF_16LE; 226 } 227 228 return null; 229 } 230 231 234 static class CustomEncodingAction extends TextEditorAction { 235 236 237 240 protected CustomEncodingAction(ResourceBundle bundle, String prefix, ITextEditor editor) { 241 super(bundle, prefix, editor); 242 } 243 244 247 public void update() { 248 ITextEditor editor= getTextEditor(); 249 setEnabled(editor != null && !editor.isDirty()); 250 } 251 252 255 public void run() { 256 257 ITextEditor editor= getTextEditor(); 258 if (editor == null) 259 return; 260 261 IEncodingSupport encodingSupport= (IEncodingSupport) editor.getAdapter(IEncodingSupport.class); 262 if (encodingSupport == null) 263 return; 264 265 String title= TextEditorMessages.Editor_ConvertEncoding_Custom_dialog_title; 266 String message= TextEditorMessages.Editor_ConvertEncoding_Custom_dialog_message; 267 IInputValidator inputValidator = new IInputValidator() { 268 public String isValid(String newText) { 269 return (newText == null || newText.length() == 0) ? " " : null; } 271 }; 272 273 String initialValue= encodingSupport.getEncoding(); 274 if (initialValue == null) 275 initialValue= encodingSupport.getDefaultEncoding(); 276 if (initialValue == null) 277 initialValue= ""; 279 InputDialog d= new InputDialog(editor.getSite().getShell(), title, message, initialValue, inputValidator); 280 if (d.open() == Window.OK) 281 encodingSupport.setEncoding(d.getValue()); 282 } 283 } 284 285 286 287 private static final String [][] ENCODINGS; 288 289 290 private static final String SYSTEM_ENCODING; 291 292 295 static { 296 297 String [][] encodings= { 298 { IEncodingActionsConstants.DEFAULT, IEncodingActionsHelpContextIds.DEFAULT, IEncodingActionsDefinitionIds.DEFAULT }, 299 { IEncodingActionsConstants.US_ASCII, IEncodingActionsHelpContextIds.US_ASCII, IEncodingActionsDefinitionIds.US_ASCII }, 300 { IEncodingActionsConstants.ISO_8859_1, IEncodingActionsHelpContextIds.ISO_8859_1, IEncodingActionsDefinitionIds.ISO_8859_1 }, 301 { IEncodingActionsConstants.UTF_8, IEncodingActionsHelpContextIds.UTF_8, IEncodingActionsDefinitionIds.UTF_8 }, 302 { IEncodingActionsConstants.UTF_16BE, IEncodingActionsHelpContextIds.UTF_16BE, IEncodingActionsDefinitionIds.UTF_16BE }, 303 { IEncodingActionsConstants.UTF_16LE, IEncodingActionsHelpContextIds.UTF_16LE, IEncodingActionsDefinitionIds.UTF_16LE }, 304 { IEncodingActionsConstants.UTF_16, IEncodingActionsHelpContextIds.UTF_16, IEncodingActionsDefinitionIds.UTF_16 } 305 }; 306 307 String system= System.getProperty("file.encoding"); if (system != null) { 309 310 int i; 311 for (i= 0; i < encodings.length; i++) { 312 if (encodings[i][0].equals(system)) 313 break; 314 } 315 316 if (i != encodings.length) { 317 String [] s= encodings[i]; 319 encodings[i]= encodings[1]; 320 encodings[1]= s; 321 system= null; 323 } 324 } 325 326 SYSTEM_ENCODING= system; 327 ENCODINGS= encodings; 328 } 329 330 331 332 333 private List fRetargetActions= new ArrayList (); 334 335 338 public EncodingActionGroup() { 339 340 fRetargetActions.add(new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding." + ENCODINGS[0][0] + ".", ENCODINGS[0][0], IAction.AS_RADIO_BUTTON)); 342 if (SYSTEM_ENCODING != null) 343 fRetargetActions.add(new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding.System.", IEncodingActionsConstants.SYSTEM, IAction.AS_RADIO_BUTTON)); 345 for (int i= 1; i < ENCODINGS.length; i++) 346 fRetargetActions.add(new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding." + ENCODINGS[i][0] + ".", ENCODINGS[i][0], IAction.AS_RADIO_BUTTON)); 348 fRetargetActions.add(new RetargetTextEditorAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding.Custom.", IEncodingActionsConstants.CUSTOM, IAction.AS_PUSH_BUTTON)); } 350 351 354 public void fillActionBars(IActionBars actionBars) { 355 IMenuManager menuManager= actionBars.getMenuManager(); 356 IMenuManager editMenu= menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT); 357 if (editMenu != null && fRetargetActions.size() > 0) { 358 MenuManager subMenu= new MenuManager(TextEditorMessages.Editor_ConvertEncoding_submenu_label); 359 subMenu.addMenuListener(new IMenuListener() { 360 public void menuAboutToShow(IMenuManager manager) { 361 update(); 362 } 363 }); 364 365 Iterator e= fRetargetActions.iterator(); 366 subMenu.add((IAction) e.next()); 367 subMenu.add(new Separator()); 368 while (e.hasNext()) 369 subMenu.add((IAction) e.next()); 370 371 editMenu.add(subMenu); 372 } 373 } 374 375 380 public void retarget(ITextEditor editor) { 381 fTextEditor= editor; 382 Iterator e= fRetargetActions.iterator(); 383 while (e.hasNext()) { 384 RetargetTextEditorAction a= (RetargetTextEditorAction) e.next(); 385 a.setAction(editor == null ? null : editor.getAction(a.getId())); 386 } 387 } 388 389 390 392 393 394 private ITextEditor fTextEditor; 395 396 401 public EncodingActionGroup(ITextEditor editor) { 402 403 fTextEditor= editor; 404 405 ResourceAction a; 406 if (SYSTEM_ENCODING != null) { 407 a= new PredefinedEncodingAction(TextEditorMessages.getBundleForConstructedKeys(), SYSTEM_ENCODING, editor); 408 a.setHelpContextId(IEncodingActionsHelpContextIds.SYSTEM); 409 a.setActionDefinitionId(IEncodingActionsDefinitionIds.SYSTEM); 410 editor.setAction(IEncodingActionsConstants.SYSTEM, a); 411 } 412 413 for (int i= 0; i < ENCODINGS.length; i++) { 414 a= new PredefinedEncodingAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding." + ENCODINGS[i][0] + ".", ENCODINGS[i][0], editor); a.setHelpContextId( ENCODINGS[i][1]); 416 a.setActionDefinitionId( ENCODINGS[i][2]); 417 editor.setAction(ENCODINGS[i][0], a); 418 } 419 420 a= new CustomEncodingAction(TextEditorMessages.getBundleForConstructedKeys(), "Editor.ConvertEncoding." + IEncodingActionsConstants.CUSTOM + ".", editor); a.setHelpContextId(IEncodingActionsHelpContextIds.CUSTOM); 422 a.setActionDefinitionId(IEncodingActionsDefinitionIds.CUSTOM); 423 editor.setAction(IEncodingActionsConstants.CUSTOM, a); 424 } 425 426 429 public void update() { 430 if (fTextEditor == null) 431 return; 432 433 IAction a= fTextEditor.getAction(IEncodingActionsConstants.SYSTEM); 434 if (a instanceof IUpdate) 435 ((IUpdate) a).update(); 436 437 for (int i= 0; i < ENCODINGS.length; i++) { 438 a= fTextEditor.getAction(ENCODINGS[i][0]); 439 if (a instanceof IUpdate) 440 ((IUpdate) a).update(); 441 } 442 443 a= fTextEditor.getAction(IEncodingActionsConstants.CUSTOM); 444 if (a instanceof IUpdate) 445 ((IUpdate) a).update(); 446 } 447 448 451 public void dispose() { 452 if (fTextEditor != null) { 453 fTextEditor.setAction(IEncodingActionsConstants.SYSTEM, null); 454 for (int i= 0; i < ENCODINGS.length; i++) 455 fTextEditor.setAction(ENCODINGS[i][0], null); 456 fTextEditor.setAction(IEncodingActionsConstants.CUSTOM, null); 457 458 fTextEditor= null; 459 } 460 } 461 } 462 | Popular Tags |