Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] how to get the IDocument of an ITextFileBuffer created from an IFile?

Hi

I try to replace text in a file and for that I would like to use a IDocument interface. Before I got it from (1.) a page, a (2.) IFileEditorInput and a (3.) IEditorDescriptor
My function looked like underneath but then it always brought the window up front.. and I don't want that.

public void replaceText(IFile file, int offset, int length, String text) {
    IWorkbench wb = PlatformUI.getWorkbench();
    IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
    IWorkbenchPage page = win.getActivePage();
    IEditorDescriptor desc = wb.getEditorRegistry().getDefaultEditor(file.getName());
    if (desc == null) {
        System.err.println("Error: Couldn't replace text, IEditorDescriptor is null");
        return;
    }
    try    {
        FileEditorInput fileEditorInput = new FileEditorInput(file);
        ITextEditor editor = (ITextEditor)page.openEditor(fileEditorInput, desc.getId());
        IDocumentProvider dp = editor.getDocumentProvider();
        IDocument doc = dp.getDocument(fileEditorInput);
        if (doc == null) {
            System.err.println("Error: Couldn't replace text, IDocument is null");
            return;
        }
        doc.replace(offset, length, text);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

Now I would like to get the IDocument interface from the ITextFileBuffer instead but it keeps returning a length of zero...
Anyone who know why the ITextFileBuffer can't get hold of a working IDocument?

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;

public void replaceText(IFile iFile, int offset, int length, String text) {
    ITextFileBufferManager iTextFileBufferManager = FileBuffers.getTextFileBufferManager();
    ITextFileBuffer iTextFileBuffer = null;
    try    {
        iTextFileBufferManager.connect(iFile.getFullPath(), LocationKind.IFILE, new NullProgressMonitor());
        iTextFileBuffer = iTextFileBufferManager.getTextFileBuffer(iFile.getFullPath(), LocationKind.IFILE);
        IDocument doc = iTextFileBuffer.getDocument();
        System.out.println("Document length: " + doc.getLength());
        doc.replace(offset, length, text);
        iTextFileBufferManager.disconnect(iFile.getFullPath(), LocationKind.IFILE, new NullProgressMonitor());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
// OUTPUT
Document length: 0
// Exception thrown
org.eclipse.jface.text.BadLocationException ... because the length of the IDocument is zero..
    at org.eclipse.jface.text.AbstractDocument.replace(AbstractDocument.java:1145)
    at org.eclipse.core.internal.filebuffers.SynchronizableDocument.replace(SynchronizableDocument.java:147)
    at org.eclipse.jface.text.AbstractDocument.replace(AbstractDocument.java:1176)
    at org.eclipse.core.internal.filebuffers.SynchronizableDocument.replace(SynchronizableDocument.java:133)
    at modules.CodeNav.replaceText(CodeNav.java:204)



Back to the top