I create eclipse plugin (button), which insert some text into active open file. I inspirate http://wiki.eclipse.org/FAQ_How_do_I_insert_text_in_the_active_text_editor%3F
public class PasteTextAction implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
public void run(IAction aAction) {
IWorkbenchPage page = window.getActivePage();
IEditorPart part = page.getActiveEditor();
if (!(part instanceof AbstractTextEditor))
return;
ITextEditor editor = (ITextEditor) part;
IDocumentProvider dp = editor.getDocumentProvider();
IDocument doc = dp.getDocument(editor.getEditorInput());
try {
int offset = doc.getLineOffset(doc.getNumberOfLines() - 4);
doc.replace(offset, 0, "pasteText\n");
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void selectionChanged(IAction aAction, ISelection aSelection) {
}
public void dispose() {
}
public void init(IWorkbenchWindow aWindow) {
window = aWindow;
}
}
It works great.
But I have problem with readonly file or file under ClearCase version control system. It paste text into editor, but it doesn't try set file writable or doesn't paste it if file can't set writable. (It doesn't open CheckOut window for ClearCase file).
How can I set file writable programmatically?