Example usage for org.eclipse.jdt.internal.core BufferManager getBuffer

List of usage examples for org.eclipse.jdt.internal.core BufferManager getBuffer

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core BufferManager getBuffer.

Prototype

public IBuffer getBuffer(IOpenable owner) 

Source Link

Document

Returns the open buffer associated with the given owner, or null if the owner does not have an open buffer associated with it.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see Openable#openBuffer(org.eclipse.core.runtime.IProgressMonitor, Object)
 *//*  w  ww  . ja  v a  2 s .c  o  m*/
protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException {

    // create buffer
    BufferManager bufManager = getBufferManager();
    boolean isWorkingCopy = isWorkingCopy();
    IBuffer buffer = isWorkingCopy ? this.owner.createBuffer(this) : BufferManager.createBuffer(this);
    if (buffer == null)
        return null;

    ICompilationUnit original = null;
    boolean mustSetToOriginalContent = false;
    if (isWorkingCopy) {
        // ensure that isOpen() is called outside the bufManager synchronized block
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=237772
        mustSetToOriginalContent = !isPrimary()
                && (original = new CompilationUnit((PackageFragment) getParent(), manager, getElementName(),
                        DefaultWorkingCopyOwner.PRIMARY)).isOpen();
    }

    // synchronize to ensure that 2 threads are not putting 2 different buffers at the same time
    // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=146331
    synchronized (bufManager) {
        IBuffer existingBuffer = bufManager.getBuffer(this);
        if (existingBuffer != null)
            return existingBuffer;

        // set the buffer source
        if (buffer.getCharacters() == null) {
            if (isWorkingCopy) {
                if (mustSetToOriginalContent) {
                    buffer.setContents(original.getSource());
                } else {
                    File file = resource();
                    if (file == null || !file.exists()) {
                        // initialize buffer with empty contents
                        buffer.setContents(CharOperation.NO_CHAR);
                    } else {
                        buffer.setContents(Util.getResourceContentsAsCharArray(file));
                    }
                }
            } else {
                File file = resource();
                if (file == null || !file.exists())
                    throw newNotPresentException();
                buffer.setContents(Util.getResourceContentsAsCharArray(file));
            }
        }

        // add buffer to buffer cache
        // note this may cause existing buffers to be removed from the buffer cache, but only primary compilation unit's buffer
        // can be closed, thus no call to a client's IBuffer#close() can be done in this synchronized block.
        bufManager.addBuffer(buffer);

        // listen to buffer changes
        buffer.addBufferChangedListener(this);
    }
    return buffer;
}

From source file:org.eclipse.jdt.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see Openable#openBuffer(IProgressMonitor, Object)
 */// www . j  av  a 2s. com
protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException {

    // create buffer
    BufferManager bufManager = getBufferManager();
    boolean isWorkingCopy = isWorkingCopy();
    IBuffer buffer = isWorkingCopy ? this.owner.createBuffer(this) : BufferManager.createBuffer(this);
    if (buffer == null)
        return null;

    ICompilationUnit original = null;
    boolean mustSetToOriginalContent = false;
    if (isWorkingCopy) {
        // ensure that isOpen() is called outside the bufManager synchronized block
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=237772
        // GROOVY start
        /* old {
        mustSetToOriginalContent = !isPrimary() && (original = new CompilationUnit((PackageFragment)getParent(), getElementName(), DefaultWorkingCopyOwner.PRIMARY)).isOpen() ;
        } new */
        mustSetToOriginalContent = !isPrimary()
                && (original = LanguageSupportFactory.newCompilationUnit((PackageFragment) getParent(),
                        getElementName(), DefaultWorkingCopyOwner.PRIMARY)).isOpen();
        // GROOVY end
    }

    // synchronize to ensure that 2 threads are not putting 2 different buffers at the same time
    // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=146331
    synchronized (bufManager) {
        IBuffer existingBuffer = bufManager.getBuffer(this);
        if (existingBuffer != null)
            return existingBuffer;

        // set the buffer source
        if (buffer.getCharacters() == null) {
            if (isWorkingCopy) {
                if (mustSetToOriginalContent) {
                    buffer.setContents(original.getSource());
                } else {
                    IFile file = (IFile) getResource();
                    if (file == null || !file.exists()) {
                        // initialize buffer with empty contents
                        buffer.setContents(CharOperation.NO_CHAR);
                    } else {
                        buffer.setContents(Util.getResourceContentsAsCharArray(file));
                    }
                }
            } else {
                IFile file = (IFile) getResource();
                if (file == null || !file.exists())
                    throw newNotPresentException();
                buffer.setContents(Util.getResourceContentsAsCharArray(file));
            }
        }

        // add buffer to buffer cache
        // note this may cause existing buffers to be removed from the buffer cache, but only primary compilation unit's buffer
        // can be closed, thus no call to a client's IBuffer#close() can be done in this synchronized block.
        bufManager.addBuffer(buffer);

        // listen to buffer changes
        buffer.addBufferChangedListener(this);
    }
    return buffer;
}