Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class DocumentModel

  1. Object
  2. 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

public Document getDocument()The current document.
public void setDocument(Document value)Replaces the document as one undoable edit and notifies the listeners.
public boolean isAutoRecalculate()Whether table formulas are calculated again after every edit.
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.
public void reset(Document value)Replaces the document and forgets the history, as when a different file is opened.
public void beginGroup()Starts joining edits into one undo step.
public void endGroup()Ends a group started by beginGroup.
public boolean canUndo()Whether undo would do anything.
public boolean canRedo()Whether redo would do anything.
public boolean undo()Restores the document before the last edit.
public boolean redo()Re-applies the last undone edit.
public void setUndoLimit(int limit)Sets how many undo steps are kept.
public void addDocumentListener(DocumentListener l)Adds a listener.
public void removeDocumentListener(DocumentListener l)Removes a listener.
public String newId(char prefix)A fresh block id, reserved so the next call returns another.
public int size()The number of top-level blocks.
public Block getBlock(int index)One top-level block.
public void setBlock(int index, Block block)Replaces a block, as one undoable edit.
public void insertBlock(int index, Block block)Inserts a block, as one undoable edit.
public void removeBlock(int index)Removes a block, as one undoable edit.
public Paragraph findParagraph(String id)A paragraph anywhere in the document: the body, table cells or notes.
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.
public void replaceParagraph(Paragraph p)Replaces a paragraph, wherever it is, by one with the same id.
public TextPosition insertText(TextPosition at, String text, TextStyle style)Inserts text at a position, as typing does.
public TextPosition insertObject(TextPosition at, Inline object)Inserts an inline object: a picture, a component, a field or a note.
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.
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.
public TextPosition splitParagraph(TextPosition at)Splits a paragraph in two at a position, as pressing Enter does.
public TextPosition joinWithPrevious(String paragraphId)Joins a paragraph to the one before it in its block list, as Backspace at its start does.
public void restyle(TextPosition from, TextPosition to, RichText.StyleMapper mapper)Changes the character style of the text between two positions in one paragraph.
public void setAuthor(String value)The author recorded on tracked changes and new comments.
public String getAuthor()The author recorded on tracked changes and new comments.
public void setTrackChanges(boolean value)Turns change tracking on or off.
public int acceptRevisions(String revisionId)Accepts tracked changes: insertions become ordinary text and deleted text goes.
public int rejectRevisions(String revisionId)Rejects tracked changes: inserted text goes and deletions become ordinary text again.
public List<Revision> getRevisions()The distinct tracked changes in the document, in document order.
public Comment addComment(TextPosition from, TextPosition to, String text)Adds a review comment over a range, by the current author.
public boolean removeComment(String id)Removes a review comment.
public boolean setCommentResolved(String id, boolean resolved)Marks a review comment resolved or open again.
public List<Comment> commentsAt(TextPosition at)The comments whose range holds a position.
public TextPosition[] order(TextPosition a, TextPosition b)Orders two positions by where they are in the document.

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

document Document
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

value Document
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 boolean
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

value Document
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

limit int
the step count, at least one

addDocumentListener

public void addDocumentListener(DocumentListener l)
Adds a listener.

Parameters

l DocumentListener
the listener

removeDocumentListener

public void removeDocumentListener(DocumentListener l)
Removes a listener.

Parameters

l DocumentListener
the listener

newId

public String newId(char prefix)
A fresh block id, reserved so the next call returns another.

Parameters

prefix char
p for a paragraph, t for a table, i for 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

index int
from zero

Returns

the block

setBlock

public void setBlock(int index, Block block)
Replaces a block, as one undoable edit.

Parameters

index int
the block to replace
block Block
its replacement

insertBlock

public void insertBlock(int index, Block block)
Inserts a block, as one undoable edit.

Parameters

index int
where, 0 to size()
block Block
the new block

removeBlock

public void removeBlock(int index)
Removes a block, as one undoable edit.

Parameters

index int
the block to remove

findParagraph

public Paragraph findParagraph(String id)
A paragraph anywhere in the document: the body, table cells or notes.

Parameters

id String
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

paragraphId String
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

p Paragraph
the new paragraph

insertText

public TextPosition insertText(TextPosition at, String text, TextStyle style)
Inserts text at a position, as typing does.

Parameters

at TextPosition
where
text String
the text; a line break inserts a soft break within the paragraph
style TextStyle
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

at TextPosition
where
object Inline
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

p Paragraph
the paragraph
offset int
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

from TextPosition
one end
to TextPosition
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

at TextPosition
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

paragraphId String
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

from TextPosition
one end
to TextPosition
the other end, in the same paragraph
mapper RichText.StyleMapper
computes each run’s new style

setAuthor

public void setAuthor(String value)
The author recorded on tracked changes and new comments.

Parameters

value String
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

value boolean
true to track changes

acceptRevisions

public int acceptRevisions(String revisionId)
Accepts tracked changes: insertions become ordinary text and deleted text goes.

Parameters

revisionId String
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

revisionId String
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

from TextPosition
one end of the range
to TextPosition
the other end
text String
the comment

Returns

the new comment

removeComment

public boolean removeComment(String id)
Removes a review comment.

Parameters

id String
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

id String
the comment’s id
resolved boolean
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

at TextPosition
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

a TextPosition
one position
b TextPosition
the other

Returns

the two, earlier first