Interface for a generic styled document.
@author
Timothy Prinzing
@version
1.21 12/19/03
The property name for the description of the stream used to initialize the document. This should be used if the document was initialized from a stream and anything is known about the stream.
The property name for the title of the document, if there is one.
Registers the given observer to begin receiving notifications when changes are made to the document.
Parameters
listenerthe observer to register
Adds a new style into the logical style hierarchy. Style attributes resolve from bottom up so an attribute specified in a child will override an attribute specified in the parent.
Parameters
nmthe name of the style (must be unique within the collection of named styles). The name may be null if the style is unnamed, but the caller is responsible for managing the reference returned as an unnamed style can't be fetched by name. An unnamed style may be useful for things like character attribute overrides such as found in a style run.
parentthe parent style. This may be null if unspecified attributes need not be resolved in some other style.
Return
the style
Registers the given observer to begin receiving notifications when undoable edits are made to the document.
Parameters
listenerthe observer to register
This method allows an application to mark a place in a sequence of character content. This mark can then be used to tracks change as insertions and removals are made in the content. The policy is that insertions always occur prior to the current position (the most common case) unless the insertion location is zero, in which case the insertion is forced to a position that follows the original position.
Parameters
offsthe offset from the start of the document >= 0
Return
the position
Throws
BadLocationExceptionif the given position does not represent a valid location in the associated document
Takes a set of attributes and turn it into a background color specification. This might be used to specify things like brighter, more hue, etc.
Parameters
attrthe set of attributes
Return
the color
Gets the element that represents the character that is at the given offset within the document.
Parameters
posthe offset >= 0
Return
the element
Returns the root element that views should be based upon, unless some other mechanism for assigning views to element structures is provided.
Return
the root element
Returns a position that represents the end of the document. The position returned can be counted on to track change and stay located at the end of the document.
Return
the position
Takes a set of attributes and turn it into a font specification. This can be used to turn things like family, style, size, etc into a font that is available on the system the document is currently being used on.
Parameters
attrthe set of attributes
Return
the font
Takes a set of attributes and turn it into a foreground color specification. This might be used to specify things like brighter, more hue, etc.
Parameters
attrthe set of attributes
Return
the color
Returns number of characters of content currently in the document.
Return
number of characters >= 0
Gets a logical style for a given position in a paragraph.
Parameters
pthe position >= 0
Return
the style
Gets the element that represents the paragraph that encloses the given offset within the document.
Parameters
posthe offset >= 0
Return
the element
Gets the properties associated with the document.
Parameters
keya non-null property key
Return
the properties
Returns all of the root elements that are defined.

Typically there will be only one document structure, but the interface supports building an arbitrary number of structural projections over the text data. The document can have multiple root elements to support multiple document structures. Some examples might be:

  • Text direction.
  • Lexical token streams.
  • Parse trees.
  • Conversions to formats other than the native format.
  • Modification specifications.
  • Annotations.
Return
the root element
Returns a position that represents the start of the document. The position returned can be counted on to track change and stay located at the beginning of the document.
Return
the position
Fetches a named style previously added.
Parameters
nmthe name of the style
Return
the style
Fetches the text contained within the given portion of the document.
Parameters
offsetthe offset into the document representing the desired start of the text >= 0
lengththe length of the desired string >= 0
Return
the text, in a String of length >= 0
Throws
BadLocationExceptionsome portion of the given range was not a valid part of the document. The location in the exception is the first bad position encountered.
Fetches the text contained within the given portion of the document.

If the partialReturn property on the txt parameter is false, the data returned in the Segment will be the entire length requested and may or may not be a copy depending upon how the data was stored. If the partialReturn property is true, only the amount of text that can be returned without creating a copy is returned. Using partial returns will give better performance for situations where large parts of the document are being scanned. The following is an example of using the partial return to access the entire document:



   int nleft = doc.getDocumentLength();
   Segment text = new Segment();
   int offs = 0;
   text.setPartialReturn(true);   
   while (nleft > 0) {
       doc.getText(offs, nleft, text);
       // do someting with text
       nleft -= text.count;
       offs += text.count;
   }

 
Parameters
offsetthe offset into the document representing the desired start of the text >= 0
lengththe length of the desired string >= 0
txtthe Segment object to return the text in
Throws
BadLocationExceptionSome portion of the given range was not a valid part of the document. The location in the exception is the first bad position encountered.
Inserts a string of content. This will cause a DocumentEvent of type DocumentEvent.EventType.INSERT to be sent to the registered DocumentListers, unless an exception is thrown. The DocumentEvent will be delivered by calling the insertUpdate method on the DocumentListener. The offset and length of the generated DocumentEvent will indicate what change was actually made to the Document.

Diagram shows insertion of 'quick' in 'The quick brown fox'

If the Document structure changed as result of the insertion, the details of what Elements were inserted and removed in response to the change will also be contained in the generated DocumentEvent. It is up to the implementation of a Document to decide how the structure should change in response to an insertion.

If the Document supports undo/redo, an UndoableEditEvent will also be generated.

Parameters
offsetthe offset into the document to insert the content >= 0. All positions that track change at or after the given location will move.
strthe string to insert
athe attributes to associate with the inserted content. This may be null if there are no attributes.
Throws
BadLocationExceptionthe given insert position is not a valid position within the document
Associates a property with the document. Two standard property keys provided are: StreamDescriptionProperty and TitleProperty. Other properties, such as author, may also be defined.
Parameters
keythe non-null property key
valuethe property value
Removes a portion of the content of the document. This will cause a DocumentEvent of type DocumentEvent.EventType.REMOVE to be sent to the registered DocumentListeners, unless an exception is thrown. The notification will be sent to the listeners by calling the removeUpdate method on the DocumentListeners.

To ensure reasonable behavior in the face of concurrency, the event is dispatched after the mutation has occurred. This means that by the time a notification of removal is dispatched, the document has already been updated and any marks created by createPosition have already changed. For a removal, the end of the removal range is collapsed down to the start of the range, and any marks in the removal range are collapsed down to the start of the range.

Diagram shows removal of 'quick' from 'The quick brown fox.'

If the Document structure changed as result of the removal, the details of what Elements were inserted and removed in response to the change will also be contained in the generated DocumentEvent. It is up to the implementation of a Document to decide how the structure should change in response to a remove.

If the Document supports undo/redo, an UndoableEditEvent will also be generated.

Parameters
offsthe offset from the beginning >= 0
lenthe number of characters to remove >= 0
Throws
BadLocationExceptionsome portion of the removal range was not a valid part of the document. The location in the exception is the first bad position encountered.
Unregisters the given observer from the notification list so it will no longer receive change updates.
Parameters
listenerthe observer to register
Removes a named style previously added to the document.
Parameters
nmthe name of the style to remove
Unregisters the given observer from the notification list so it will no longer receive updates.
Parameters
listenerthe observer to register
Allows the model to be safely rendered in the presence of concurrency, if the model supports being updated asynchronously. The given runnable will be executed in a way that allows it to safely read the model with no changes while the runnable is being executed. The runnable itself may not make any mutations.
Parameters
ra Runnable used to render the model
Changes the content element attributes used for the given range of existing content in the document. All of the attributes defined in the given Attributes argument are applied to the given range. This method can be used to completely remove all content level attributes for the given range by giving an Attributes argument that has no attributes defined and setting replace to true.
Parameters
offsetthe start of the change >= 0
lengththe length of the change >= 0
sthe non-null attributes to change to. Any attributes defined will be applied to the text for the given range.
replaceindicates whether or not the previous attributes should be cleared before the new attributes as set. If true, the operation will replace the previous attributes entirely. If false, the new attributes will be merged with the previous attributes.
Sets the logical style to use for the paragraph at the given position. If attributes aren't explicitly set for character and paragraph attributes they will resolve through the logical style assigned to the paragraph, which in turn may resolve through some hierarchy completely independent of the element hierarchy in the document.
Parameters
posthe starting position >= 0
sthe style to set
Sets paragraph attributes.
Parameters
offsetthe start of the change >= 0
lengththe length of the change >= 0
sthe non-null attributes to change to. Any attributes defined will be applied to the text for the given range.
replaceindicates whether or not the previous attributes should be cleared before the new attributes are set. If true, the operation will replace the previous attributes entirely. If false, the new attributes will be merged with the previous attributes.