Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class DocumentModel
- Object
- 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();Constructors
public DocumentModel() | A model holding a new empty document. |
public DocumentModel(Document document) | A model holding a document. |
Methods
Inherited methods
Constructor details
DocumentModel
public DocumentModel()A model holding a new empty document.
DocumentModel
public DocumentModel(Document document)A model holding a document.
Parameters
documentDocument- the initial document
Method details
getDocument
public Document getDocument()The current document.
Returns
the document
setDocument
public void setDocument(Document value)Replaces the document as one undoable edit and notifies the listeners. Setting an equal
document does nothing.
Parameters
valueDocument- 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
valueboolean- true to calculate after every edit
reset
public void reset(Document value)Replaces the document and forgets the history, as when a different file is opened.
Parameters
valueDocument- the new document
beginGroup
public void beginGroup()Starts joining edits into one undo step. Groups nest; the step ends with the outermost
endGroup.endGroup
public void endGroup()Ends a group started by
beginGroup.canUndo
public boolean canUndo()Whether
undo would do anything.Returns
true when there is history
canRedo
public boolean canRedo()Whether
redo would 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
limitint- the step count, at least one
addDocumentListener
public void addDocumentListener(DocumentListener l)Adds a listener.
Parameters
lDocumentListener- the listener
removeDocumentListener
public void removeDocumentListener(DocumentListener l)Removes a listener.
Parameters
lDocumentListener- the listener
newId
public String newId(char prefix)A fresh block id, reserved so the next call returns another.
Parameters
prefixcharpfor 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
public Block getBlock(int index)One top-level block.
Parameters
indexint- from zero
Returns
the block
setBlock
public void setBlock(int index, Block block)Replaces a block, as one undoable edit.
Parameters
indexint- the block to replace
blockBlock- its replacement
insertBlock
public void insertBlock(int index, Block block)Inserts a block, as one undoable edit.
Parameters
indexint- where, 0 to
size() blockBlock- the new block
removeBlock
public void removeBlock(int index)Removes a block, as one undoable edit.
Parameters
indexint- the block to remove
findParagraph
public Paragraph findParagraph(String id)A paragraph anywhere in the document: the body, table cells or notes.
Parameters
idString- the paragraph id
Returns
the paragraph, or null
tableOf
public String tableOf(String paragraphId)The id of the top-level table a paragraph is in, for opening a
TableBinding on the
table under the caret.Parameters
paragraphIdString- the paragraph
Returns
the table id, or null when the paragraph is not in a table
replaceParagraph
public void replaceParagraph(Paragraph p)Replaces a paragraph, wherever it is, by one with the same id.
Parameters
pParagraph- the new paragraph
insertText
public TextPosition insertText(TextPosition at, String text, TextStyle style)Inserts text at a position, as typing does.
Parameters
atTextPosition- where
textString- the text; a line break inserts a soft break within the paragraph
styleTextStyle- its style, or null to continue the style before the position
Returns
the position after the inserted text
insertObject
public TextPosition insertObject(TextPosition at, Inline object)Inserts an inline object: a picture, a component, a field or a note.
Parameters
atTextPosition- where
objectInline- the object
Returns
the position after it
typingStyle
public static TextStyle typingStyle(Paragraph p, int offset)The style typing at a position continues: that of the character before it, without a
link or a revision.
Parameters
pParagraph- the paragraph
offsetint- the position
Returns
the style
delete
public TextPosition 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.
Parameters
fromTextPosition- one end
toTextPosition- the other end
Returns
the position where the deleted text was
splitParagraph
public TextPosition splitParagraph(TextPosition at)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
atTextPosition- where
Returns
the position at the start of the new paragraph
joinWithPrevious
public TextPosition joinWithPrevious(String paragraphId)Joins a paragraph to the one before it in its block list, as Backspace at its start
does.
Parameters
paragraphIdString- the paragraph
Returns
the position at the join, or null when there is no paragraph before it to join
restyle
public void restyle(TextPosition from, TextPosition to, RichText.StyleMapper mapper)Changes the character style of the text between two positions in one paragraph.
Parameters
fromTextPosition- one end
toTextPosition- the other end, in the same paragraph
mapperRichText.StyleMapper- computes each run’s new style
setAuthor
public void setAuthor(String value)The author recorded on tracked changes and new comments.
Parameters
valueString- the author’s name
getAuthor
public String 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
valueboolean- true to track changes
acceptRevisions
public int acceptRevisions(String revisionId)Accepts tracked changes: insertions become ordinary text and deleted text goes.
Parameters
revisionIdString- the revision to accept, or null for all of them
Returns
the number of characters changed
rejectRevisions
public int rejectRevisions(String revisionId)Rejects tracked changes: inserted text goes and deletions become ordinary text again.
Parameters
revisionIdString- the revision to reject, or null for all of them
Returns
the number of characters changed
getRevisions
public List<Revision> getRevisions()The distinct tracked changes in the document, in document order.
Returns
the revisions
addComment
public Comment addComment(TextPosition from, TextPosition to, String text)Adds a review comment over a range, by the current author.
Parameters
fromTextPosition- one end of the range
toTextPosition- the other end
textString- the comment
Returns
the new comment
removeComment
public boolean removeComment(String id)Removes a review comment.
Parameters
idString- the comment’s id
Returns
true when there was one
setCommentResolved
public boolean setCommentResolved(String id, boolean resolved)Marks a review comment resolved or open again.
Parameters
idString- the comment’s id
resolvedboolean- the new state
Returns
true when there was such a comment
commentsAt
public List<Comment> commentsAt(TextPosition at)The comments whose range holds a position.
Parameters
atTextPosition- the position
Returns
the comments, in document order
order
public TextPosition[] order(TextPosition a, TextPosition b)Orders two positions by where they are in the document.
Parameters
aTextPosition- one position
bTextPosition- the other
Returns
the two, earlier first