Class Buffer

  • All Implemented Interfaces:
    java.lang.AutoCloseable, IBuffer, ISnapshotProvider, IReferenceCountable
    Direct Known Subclasses:
    ChildBuffer

    public class Buffer
    extends java.lang.Object
    implements IBuffer
    A simple IBuffer implementation. This implementation is not backed by an underlying resource, so saving the buffer only modifies its "dirty" state; it does not really save its contents.

    An instance of this class is safe for use by multiple threads. Clients can use this class as it stands or subclass it as circumstances warrant.

    • Constructor Summary

      Constructors 
      Constructor Description
      Buffer()
      Creates a new buffer instance that is initially empty.
      Buffer​(java.lang.String contents)
      Creates a new buffer instance and initializes it with the given contents.
    • Constructor Detail

      • Buffer

        public Buffer()
        Creates a new buffer instance that is initially empty.

        It is the client responsibility to release the created buffer after it is no longer needed.

      • Buffer

        public Buffer​(java.lang.String contents)
        Creates a new buffer instance and initializes it with the given contents.

        It is the client responsibility to release the created buffer after it is no longer needed.

        Parameters:
        contents - initial contents
    • Method Detail

      • getDocument

        public org.eclipse.jface.text.IDocument getDocument()
        Description copied from interface: IBuffer
        Returns the underlying document of this buffer. The relationship between a buffer and its document does not change over the lifetime of the buffer.
        Specified by:
        getDocument in interface IBuffer
        Returns:
        the buffer's underlying document (never null)
      • getSnapshot

        public ISnapshot getSnapshot()
        Description copied from interface: IBuffer
        Returns the current snapshot of this buffer. The returned snapshot may immediately become stale or expire.

        Note that it is possible to obtain a non-expiring snapshot from the buffer, although protractedly holding on non-expiring snapshots is not recommended as they may potentially consume large amount of space.

        Specified by:
        getSnapshot in interface IBuffer
        Specified by:
        getSnapshot in interface ISnapshotProvider
        Returns:
        the buffer's current snapshot (never null)
      • applyChange

        public IBufferChange applyChange​(IBufferChange change,
                                         org.eclipse.core.runtime.IProgressMonitor monitor)
                                  throws org.eclipse.core.runtime.CoreException
        Description copied from interface: IBuffer
        Applies the given change to this buffer.

        Note that an update conflict may occur if the buffer's contents have changed since the inception of the snapshot on which the change is based. In that case, a StaleSnapshotException is thrown.

        Specified by:
        applyChange in interface IBuffer
        Parameters:
        change - a buffer change (not null)
        monitor - a progress monitor, or null if progress reporting is not desired. The caller must not rely on IProgressMonitor.done() having been called by the receiver
        Returns:
        undo change, if requested by the change. Otherwise, null
        Throws:
        org.eclipse.core.runtime.CoreException - if the change's edit tree is not in a valid state, or if one of the edits in the tree could not be executed, or if save is requested by the change but the buffer could not be saved
      • save

        public void save​(IContext context,
                         org.eclipse.core.runtime.IProgressMonitor monitor)
                  throws org.eclipse.core.runtime.CoreException
        Description copied from interface: IBuffer
        Saves this buffer. It is up to the implementors of this method to decide what saving means. Typically, the contents of the underlying resource is changed to the contents of the buffer.
        Specified by:
        save in interface IBuffer
        Parameters:
        context - the operation context (not null)
        monitor - a progress monitor, or null if progress reporting is not desired. The caller must not rely on IProgressMonitor.done() having been called by the receiver
        Throws:
        org.eclipse.core.runtime.CoreException - if the buffer could not be saved
      • isDirty

        public boolean isDirty()
        Description copied from interface: IBuffer
        Returns whether this buffer has been modified since the last time it was opened or saved.
        Specified by:
        isDirty in interface IBuffer
        Returns:
        true if the buffer has unsaved changes, false otherwise
      • getSupportedListenerMethods

        public int getSupportedListenerMethods()
        Description copied from interface: IBuffer
        Returns a bit-mask describing the listener methods supported by this buffer. The buffer will never invoke a listener method it does not support.
        Specified by:
        getSupportedListenerMethods in interface IBuffer
        Returns:
        a bit-mask describing the supported listener methods
        See Also:
        IBufferListener
      • addListener

        public void addListener​(IBufferListener listener)
        Description copied from interface: IBuffer
        Adds the given listener to this buffer. Has no effect if the same listener is already registered.
        Specified by:
        addListener in interface IBuffer
        Parameters:
        listener - not null
      • removeListener

        public void removeListener​(IBufferListener listener)
        Description copied from interface: IBuffer
        Removes the given listener from this buffer. Has no effect if the same listener was not already registered.
        Specified by:
        removeListener in interface IBuffer
        Parameters:
        listener - not null
      • createEmptyDocument

        protected org.eclipse.jface.text.IDocument createEmptyDocument()
      • doSave

        protected void doSave​(IContext context,
                              org.eclipse.core.runtime.IProgressMonitor monitor)
                       throws org.eclipse.core.runtime.CoreException
        Throws:
        org.eclipse.core.runtime.CoreException
      • getListeners

        protected java.lang.Iterable<IBufferListener> getListeners()
        Since:
        1.4