1 12 package org.eclipse.ui.texteditor; 13 14 import java.util.ResourceBundle ; 15 16 import org.eclipse.swt.custom.StyledText; 17 import org.eclipse.swt.graphics.Point; 18 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.jface.text.source.ISourceViewer; 22 23 27 public class CaseAction extends TextEditorAction implements IUpdate { 28 29 30 private boolean fToUpper; 31 32 45 public CaseAction(ResourceBundle bundle, String prefix, AbstractTextEditor editor, boolean toUpper) { 46 super(bundle, prefix, editor); 47 fToUpper= toUpper; 48 update(); 49 } 50 51 54 public void run() { 55 ITextEditor editor= getTextEditor(); 56 if (editor == null) 57 return; 58 59 if (!validateEditorInputState()) 60 return; 61 62 ISourceViewer viewer= ((AbstractTextEditor) editor).getSourceViewer(); 63 if (viewer == null) 64 return; 65 66 IDocument document= viewer.getDocument(); 67 if (document == null) 68 return; 69 70 StyledText st= viewer.getTextWidget(); 71 if (st == null) 72 return; 73 74 Point sel= viewer.getSelectedRange(); 75 if (sel == null) 76 return; 77 78 try { 79 if (sel.y == 0) { 82 83 85 93 if (sel.y == 0) 94 return; } 96 97 String target= document.get(sel.x, sel.y); 98 String replacement= (fToUpper ? target.toUpperCase() : target.toLowerCase()); 99 if (!target.equals(replacement)) { 100 document.replace(sel.x, target.length(), replacement); 101 int adjustment= replacement.length() - target.length(); 103 if (adjustment > 0) 104 sel.y += adjustment; 105 } 106 } catch (BadLocationException x) { 107 return; 109 } 110 111 viewer.setSelectedRange(sel.x, sel.y); 113 st.showSelection(); 115 } 116 117 } 118 | Popular Tags |