Class DocumentModel

java.lang.Object
com.codename1.wordprocessing.DocumentModel

public class DocumentModel extends Object

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 Details

    • DocumentModel

      public DocumentModel()
      A model holding a new empty document.
    • DocumentModel

      public DocumentModel(Document document)

      A model holding a document.

      Parameters
      • 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: 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

      public void reset(Document value)

      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 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: the step count, at least one
    • addDocumentListener

      public void addDocumentListener(DocumentListener l)

      Adds a listener.

      Parameters
      • l: the listener
    • removeDocumentListener

      public void removeDocumentListener(DocumentListener l)

      Removes a listener.

      Parameters
      • l: the listener
    • newId

      public String newId(char prefix)

      A fresh block id, reserved so the next call returns another.

      Parameters
      • prefix: 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: from zero
      Returns

      the block

    • setBlock

      public void setBlock(int index, Block block)

      Replaces a block, as one undoable edit.

      Parameters
      • index: the block to replace

      • block: its replacement

    • insertBlock

      public void insertBlock(int index, Block block)

      Inserts a block, as one undoable edit.

      Parameters
      • index: where, 0 to size()

      • block: the new block

    • removeBlock

      public void removeBlock(int index)

      Removes a block, as one undoable edit.

      Parameters
      • index: the block to remove
    • findParagraph

      public Paragraph findParagraph(String id)

      A paragraph anywhere in the document: the body, table cells or notes.

      Parameters
      • id: 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: 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: the new paragraph
    • insertText

      public TextPosition insertText(TextPosition at, String text, TextStyle style)

      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

      public TextPosition insertObject(TextPosition at, Inline object)

      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

      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: the paragraph

      • offset: 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: one end

      • to: 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: 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: 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: one end

      • to: the other end, in the same paragraph

      • mapper: computes each run's new style

    • setAuthor

      public void setAuthor(String value)

      The author recorded on tracked changes and new comments.

      Parameters
      • value: 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: true to track changes
    • acceptRevisions

      public int acceptRevisions(String revisionId)

      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

      public int rejectRevisions(String revisionId)

      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

      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: one end of the range

      • to: the other end

      • text: the comment

      Returns

      the new comment

    • removeComment

      public boolean removeComment(String id)

      Removes a review comment.

      Parameters
      • id: 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: the comment's id

      • resolved: 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: 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: one position

      • b: the other

      Returns

      the two, earlier first