Class DocumentModel
The editable state behind a document view: the current immutable Document, its listeners
and its undo history.
Every change replaces the whole document, so undo is a stack of earlier documents, and since
a new document shares its unchanged blocks with the old one the history stays small. Edits
that belong together (the letters of one typed word) are joined with beginGroup/endGroup
into one undo step.
DocumentModel model = new DocumentModel(new Document());
model.addDocumentListener(new DocumentListener() {
public void documentChanged(DocumentModel m, Document before, Document after) {
Log.p("now " + after.size() + " blocks");
}
});
model.insertBlock(model.size(), new Paragraph(model.newId('p'), RichText.of("The end")));
model.undo();
-
Constructor Summary
ConstructorsConstructorDescriptionA model holding a new empty document.DocumentModel(Document document) A model holding a document. -
Method Summary
Modifier and TypeMethodDescriptionintacceptRevisions(String revisionId) Accepts tracked changes: insertions become ordinary text and deleted text goes.addComment(TextPosition from, TextPosition to, String text) Adds a review comment over a range, by the current author.voidAdds a listener.voidStarts joining edits into one undo step.booleancanRedo()Whetherredowould do anything.booleancanUndo()Whetherundowould do anything.The comments whose range holds a position.delete(TextPosition from, TextPosition to) Deletes the text between two positions, which may lie in different paragraphs of the same block list; the paragraphs between them go, and the last one's remaining text joins the first.voidendGroup()Ends a group started bybeginGroup.findParagraph(String id) A paragraph anywhere in the document: the body, table cells or notes.The author recorded on tracked changes and new comments.getBlock(int index) One top-level block.The current document.The distinct tracked changes in the document, in document order.voidinsertBlock(int index, Block block) Inserts a block, as one undoable edit.insertObject(TextPosition at, Inline object) Inserts an inline object: a picture, a component, a field or a note.insertText(TextPosition at, String text, TextStyle style) Inserts text at a position, as typing does.booleanWhether table formulas are calculated again after every edit.joinWithPrevious(String paragraphId) Joins a paragraph to the one before it in its block list, as Backspace at its start does.newId(char prefix) A fresh block id, reserved so the next call returns another.order(TextPosition a, TextPosition b) Orders two positions by where they are in the document.booleanredo()Re-applies the last undone edit.intrejectRevisions(String revisionId) Rejects tracked changes: inserted text goes and deletions become ordinary text again.voidremoveBlock(int index) Removes a block, as one undoable edit.booleanremoveComment(String id) Removes a review comment.voidRemoves a listener.voidReplaces a paragraph, wherever it is, by one with the same id.voidReplaces the document and forgets the history, as when a different file is opened.voidrestyle(TextPosition from, TextPosition to, RichText.StyleMapper mapper) Changes the character style of the text between two positions in one paragraph.voidThe author recorded on tracked changes and new comments.voidsetAutoRecalculate(boolean value) Calculates table formulas again after every edit, so a formula field always shows the value of the cells it reads; or leaves them as they are untilTableSheets.recalculate.voidReplaces a block, as one undoable edit.booleansetCommentResolved(String id, boolean resolved) Marks a review comment resolved or open again.voidsetDocument(Document value) Replaces the document as one undoable edit and notifies the listeners.voidsetTrackChanges(boolean value) Turns change tracking on or off.voidsetUndoLimit(int limit) Sets how many undo steps are kept.intsize()The number of top-level blocks.Splits a paragraph in two at a position, as pressing Enter does.The id of the top-level table a paragraph is in, for opening aTableBindingon the table under the caret.static TextStyletypingStyle(Paragraph p, int offset) The style typing at a position continues: that of the character before it, without a link or a revision.booleanundo()Restores the document before the last edit.
-
Constructor Details
-
DocumentModel
public DocumentModel()A model holding a new empty document. -
DocumentModel
A model holding a document.
Parameters
document: the initial document
-
-
Method Details
-
getDocument
The current document.
Returns
the document
-
setDocument
Replaces the document as one undoable edit and notifies the listeners. Setting an equal document does nothing.
Parameters
value: the new document
-
isAutoRecalculate
public boolean isAutoRecalculate()Whether table formulas are calculated again after every edit.
Returns
true by default
-
setAutoRecalculate
public void setAutoRecalculate(boolean value) Calculates table formulas again after every edit, so a formula field always shows the value of the cells it reads; or leaves them as they are until
TableSheets.recalculate.Parameters
value: true to calculate after every edit
-
reset
Replaces the document and forgets the history, as when a different file is opened.
Parameters
value: the new document
-
beginGroup
public void beginGroup()Starts joining edits into one undo step. Groups nest; the step ends with the outermostendGroup. -
endGroup
public void endGroup()Ends a group started bybeginGroup. -
canUndo
public boolean canUndo()Whether
undowould do anything.Returns
true when there is history
-
canRedo
public boolean canRedo()Whether
redowould do anything.Returns
true after an undo with no edit since
-
undo
public boolean undo()Restores the document before the last edit.
Returns
false when there was nothing to undo
-
redo
public boolean redo()Re-applies the last undone edit.
Returns
false when there was nothing to redo
-
setUndoLimit
public void setUndoLimit(int limit) Sets how many undo steps are kept.
Parameters
limit: the step count, at least one
-
addDocumentListener
Adds a listener.
Parameters
l: the listener
-
removeDocumentListener
Removes a listener.
Parameters
l: the listener
-
newId
A fresh block id, reserved so the next call returns another.
Parameters
prefix:pfor a paragraph,tfor a table,ifor a picture
Returns
the id
-
size
public int size()The number of top-level blocks.
Returns
the block count
-
getBlock
One top-level block.
Parameters
index: from zero
Returns
the block
-
setBlock
Replaces a block, as one undoable edit.
Parameters
-
index: the block to replace -
block: its replacement
-
-
insertBlock
Inserts a block, as one undoable edit.
Parameters
-
index: where, 0 tosize() -
block: the new block
-
-
removeBlock
public void removeBlock(int index) Removes a block, as one undoable edit.
Parameters
index: the block to remove
-
findParagraph
-
tableOf
-
replaceParagraph
Replaces a paragraph, wherever it is, by one with the same id.
Parameters
p: the new paragraph
-
insertText
Inserts text at a position, as typing does.
Parameters
-
at: where -
text: the text; a line break inserts a soft break within the paragraph -
style: its style, or null to continue the style before the position
Returns
the position after the inserted text
-
-
insertObject
Inserts an inline object: a picture, a component, a field or a note.
Parameters
-
at: where -
object: the object
Returns
the position after it
-
-
typingStyle
-
delete
Deletes the text between two positions, which may lie in different paragraphs of the same block list; the paragraphs between them go, and the last one's remaining text joins the first.
Parameters
-
from: one end -
to: the other end
Returns
the position where the deleted text was
-
-
splitParagraph
Splits a paragraph in two at a position, as pressing Enter does. The new paragraph keeps the old one's formatting, except that a heading is followed by a normal paragraph.
Parameters
at: where
Returns
the position at the start of the new paragraph
-
joinWithPrevious
Joins a paragraph to the one before it in its block list, as Backspace at its start does.
Parameters
paragraphId: the paragraph
Returns
the position at the join, or null when there is no paragraph before it to join
-
restyle
Changes the character style of the text between two positions in one paragraph.
Parameters
-
from: one end -
to: the other end, in the same paragraph -
mapper: computes each run's new style
-
-
setAuthor
The author recorded on tracked changes and new comments.
Parameters
value: the author's name
-
getAuthor
The author recorded on tracked changes and new comments.
Returns
the name, possibly empty
-
setTrackChanges
public void setTrackChanges(boolean value) Turns change tracking on or off. While it is on, inserted text is marked as an insertion and deleted text stays in the document marked as a deletion, until the revisions are accepted or rejected.
Parameters
value: true to track changes
-
acceptRevisions
Accepts tracked changes: insertions become ordinary text and deleted text goes.
Parameters
revisionId: the revision to accept, or null for all of them
Returns
the number of characters changed
-
rejectRevisions
Rejects tracked changes: inserted text goes and deletions become ordinary text again.
Parameters
revisionId: the revision to reject, or null for all of them
Returns
the number of characters changed
-
getRevisions
-
addComment
Adds a review comment over a range, by the current author.
Parameters
-
from: one end of the range -
to: the other end -
text: the comment
Returns
the new comment
-
-
removeComment
Removes a review comment.
Parameters
id: the comment's id
Returns
true when there was one
-
setCommentResolved
Marks a review comment resolved or open again.
Parameters
-
id: the comment's id -
resolved: the new state
Returns
true when there was such a comment
-
-
commentsAt
The comments whose range holds a position.
Parameters
at: the position
Returns
the comments, in document order
-
order
Orders two positions by where they are in the document.
Parameters
-
a: one position -
b: the other
Returns
the two, earlier first
-
-