public class Buffer extends java.lang.Object implements IBuffer
IBufferIBuffer.ITextEditCapability| Modifier and Type | Field and Description |
|---|---|
protected <any> |
changeListeners |
protected char[] |
contents |
protected static int |
F_HAS_UNSAVED_CHANGES |
protected static int |
F_IS_CLOSED |
protected static int |
F_IS_READ_ONLY |
protected IFile |
file |
protected int |
flags |
protected int |
gapEnd |
protected int |
gapStart |
protected java.lang.Object |
lock |
protected IOpenable |
owner |
| Modifier | Constructor and Description |
|---|---|
protected |
Buffer(IFile file,
IOpenable owner,
boolean readOnly)
Creates a new buffer on an underlying resource.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addBufferChangedListener(IBufferChangedListener listener)
Adds the given listener for changes to this buffer.
|
void |
append(char[] text)
Append the
text to the actual content, the gap is moved
to the end of the text. |
void |
append(java.lang.String text)
Append the
text to the actual content, the gap is moved
to the end of the text. |
void |
close()
Closes the buffer.
|
char |
getChar(int position)
Returns the character at the given position in this buffer.
|
char[] |
getCharacters()
Returns the contents of this buffer as a character array, or
null if
the buffer has not been initialized. |
java.lang.String |
getContents()
Returns the contents of this buffer as a
String. |
int |
getLength()
Returns number of characters stored in this buffer.
|
IOpenable |
getOwner()
Returns the Java openable element owning of this buffer.
|
java.lang.String |
getText(int offset,
int length)
Returns the given range of text in this buffer.
|
IResource |
getUnderlyingResource()
Returns the underlying resource for which this buffer was opened,
or
null if this buffer was not opened on a resource. |
boolean |
hasUnsavedChanges()
Returns whether this buffer has been modified since it
was opened or since it was last saved.
|
boolean |
isClosed()
Returns whether this buffer has been closed.
|
boolean |
isReadOnly()
Returns whether this buffer is read-only.
|
protected void |
moveAndResizeGap(int position,
int size)
Moves the gap to location and adjust its size to the
anticipated change size.
|
protected void |
notifyChanged(BufferChangedEvent event)
Notify the listeners that this buffer has changed.
|
void |
removeBufferChangedListener(IBufferChangedListener listener)
Removes the given listener from this buffer.
|
void |
replace(int position,
int length,
char[] text)
Replaces
length characters starting from position with text |
void |
replace(int position,
int length,
java.lang.String text)
Replaces
length characters starting from position with text |
void |
save(IProgressMonitor progress,
boolean force)
Saves the contents of this buffer to its underlying resource.
|
void |
setContents(char[] newContents)
Sets the contents of this buffer to the given character array.
|
void |
setContents(java.lang.String newContents)
Sets the contents of this buffer to the given
String. |
protected void |
setReadOnly(boolean readOnly)
Sets this
Buffer to be read only. |
java.lang.String |
toString() |
protected IFile file
protected int flags
protected char[] contents
protected <any> changeListeners
protected IOpenable owner
protected int gapStart
protected int gapEnd
protected java.lang.Object lock
protected static final int F_HAS_UNSAVED_CHANGES
protected static final int F_IS_READ_ONLY
protected static final int F_IS_CLOSED
protected Buffer(IFile file,
IOpenable owner,
boolean readOnly)
public void addBufferChangedListener(IBufferChangedListener listener)
IBufferaddBufferChangedListener in interface IBufferlistener - the listener of buffer changesIBufferpublic void append(char[] text)
text to the actual content, the gap is moved
to the end of the text.public void append(java.lang.String text)
text to the actual content, the gap is moved
to the end of the text.public void close()
IBufferFurther operations on the buffer are not allowed, except for close. If an attempt is made to close an already closed buffer, the second attempt has no effect.
public char getChar(int position)
IBufferThe returned value is undefined if the buffer is closed.
public char[] getCharacters()
IBuffernull if
the buffer has not been initialized.
Callers should make no assumption about whether the returned character array is or is not the genuine article or a copy. In other words, if the client wishes to change this array, they should make a copy. Likewise, if the client wishes to hang on to the array in its current state, they should make a copy.
The returned value is undefined if the buffer is closed.
getCharacters in interface IBufferIBufferpublic java.lang.String getContents()
IBufferString. Like all strings,
the result is an immutable value object., It can also answer null if
the buffer has not been initialized.
The returned value is undefined if the buffer is closed.
getContents in interface IBufferStringIBufferpublic int getLength()
IBufferThe returned value is undefined if the buffer is closed.
public IOpenable getOwner()
IBufferpublic java.lang.String getText(int offset,
int length)
IBufferThe returned value is undefined if the buffer is closed.
public IResource getUnderlyingResource()
IBuffernull if this buffer was not opened on a resource.getUnderlyingResource in interface IBuffernull
if none.IBufferpublic boolean hasUnsavedChanges()
IBuffertrue.
NOTE: when a buffer does not have unsaved changes, the model may decide to close it to claim some memory back. If the associated element needs to be reopened later on, its buffer factory will be requested to create a new buffer.
hasUnsavedChanges in interface IBufferboolean indicating presence of unsaved changes (in
the absence of any underlying resource, it will always return true).IBufferpublic boolean isClosed()
IBufferpublic boolean isReadOnly()
IBufferisReadOnly in interface IBufferboolean indicating whether this buffer is read-onlyIBufferprotected void moveAndResizeGap(int position,
int size)
protected void notifyChanged(BufferChangedEvent event)
public void removeBufferChangedListener(IBufferChangedListener listener)
IBufferremoveBufferChangedListener in interface IBufferlistener - the listenerIBufferpublic void replace(int position,
int length,
char[] text)
length characters starting from position with text.
After that operation, the gap is placed at the end of the
inserted text.public void replace(int position,
int length,
java.lang.String text)
length characters starting from position with text.
After that operation, the gap is placed at the end of the
inserted text.public void save(IProgressMonitor progress,
boolean force)
throws JavaModelException
IBuffer
The force parameter controls how this method deals with
cases where the workbench is not completely in sync with the local file system.
If false is specified, this method will only attempt
to overwrite a corresponding file in the local file system provided
it is in sync with the workbench. This option ensures there is no
unintended data loss; it is the recommended setting.
However, if true is specified, an attempt will be made
to write a corresponding file in the local file system,
overwriting any existing one if need be.
In either case, if this method succeeds, the resource will be marked
as being local (even if it wasn't before).
Has no effect if this buffer is read-only or if the buffer is closed.
save in interface IBufferprogress - the progress monitor to notifyforce - a boolean flag indicating how to deal with resource
inconsistencies.JavaModelException - if an error occurs writing the buffer
to the underlying resourceIBufferpublic void setContents(char[] newContents)
IBuffer
Equivalent to replace(0,getLength(),contents).
Has no effect if this buffer is read-only or if the buffer is closed.
setContents in interface IBuffernewContents - the new contents of this buffer as a character arrayIBufferpublic void setContents(java.lang.String newContents)
IBufferString.
This buffer will now have unsaved changes.
Any client can set the contents of the buffer, not just the owner of the buffer.
Reports a buffer changed event.
Equivalent to replace(0,getLength(),contents).
Has no effect if this buffer is read-only or if the buffer is closed.
setContents in interface IBuffernewContents - the new contents of this buffer as a StringIBufferprotected void setReadOnly(boolean readOnly)
Buffer to be read only.public java.lang.String toString()
toString in class java.lang.Object