TextEditor相关操作

来源:互联网 发布:彩票电视走势图软件 编辑:程序博客网 时间:2024/05/16 05:10

1)取得当前编辑器(getEditor())。

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();

2)得到文件的编辑器的方法:
    public static IEditorPart findEditor(IFile file){
        IEditorReference[] editors = getActivePage().getEditorReferences();;
        for (int i = 0; i < editors.length; i++) {
            IEditorPart part = (IEditorPart)editors[i].getPart(false);
            if (part != null ){
                IEditorInput input = part.getEditorInput(); 
                if(input instanceof FileEditorInput && ((FileEditorInput)input).getFile().equals(file))
                    return part;
            }                
        }
        return null;
    }


3)取得选中文本。

(ITextSelection) getEditor().getEditorSite().getSelectionProvider().getSelection();


4)替换选中文本。

AbstractTextEditor editor = (AbstractTextEditor) getEditor(); 
editor.getDocumentProvider().getDocument(editor.getEditorInput()); 
document.replace(offset, length, replaceText);


5)选中替换后的文本。

ITextSelection tsNew = new TextSelection(document, offset, length); 
getEditor().getEditorSite().getSelectionProvider().setSelection(tsNew);