Class DocumentView

All Implemented Interfaces:
Animation, Editable, StyleListener, TextInputClient, TextInputClient.CompositionCancellation, TextInputGeometry.Provider

Shows a document as pages and edits it, the way DataGrid shows and edits a Sheet.

DocumentModel model = new DocumentModel(new Document());
model.insertBlock(0, new Paragraph(model.newId('p'), RichText.of("Quarterly review")));
DocumentView view = new DocumentView(model);
Form form = new Form("Document", new BorderLayout());
form.add(BorderLayout.CENTER, view);
form.show();

The view subscribes to its model while it is on a form and lays the document out again on every change, so anything that edits the model -- the view itself, a toolbar, an undo button -- is shown at once. Pages are laid out by DocumentLayout with the view's Measurer (ScreenMeasurer by default, the fonts the view draws in). Set a PdfMeasurer to break pages exactly where the PDF export does, at the cost of spacing set by the PDF's fonts.

A tap places the caret, a drag or a double tap selects, and typing, Backspace, Delete and Enter edit through the model, which gives undo for free. Pinch or Control and the wheel zooms; by default a page is fitted to the view's width when it would not fit.

  • Constructor Details

    • DocumentView

      public DocumentView()
      A view of an empty document.
    • DocumentView

      public DocumentView(Document doc)

      A view of a document, in a model of its own.

      Parameters
      • doc: the document
    • DocumentView

      public DocumentView(DocumentModel model)

      A view of a model.

      Parameters
      • model: the model
  • Method Details

    • getModel

      public DocumentModel getModel()

      The model shown.

      Returns

      the model

    • setModel

      public final void setModel(DocumentModel value)

      Shows another model.

      Parameters
      • value: the model
    • getDocument

      public Document getDocument()

      The document shown; a shortcut for getModel().getDocument().

      Returns

      the document

    • setDocument

      public void setDocument(Document doc)

      Replaces the document, forgetting the undo history; a shortcut for getModel().reset(doc).

      Parameters
      • doc: the document
    • setMeasurer

      public void setMeasurer(Measurer value)

      Sets the measurer pages are laid out with, for instance a PdfMeasurer with TrueType fonts registered for a script the standard fonts lack.

      Parameters
      • value: the measurer
    • getMeasurer

      public Measurer getMeasurer()

      The measurer pages are laid out with.

      Returns

      the measurer

    • getLayout

      public DocumentLayout getLayout()

      The current layout.

      Returns

      the layout of the model's document

    • isEditable

      public boolean isEditable()

      Whether the user may edit the document.

      Returns

      true by default

      Specified by:
      isEditable in interface Editable
      Overrides:
      isEditable in class Component
    • setEditable

      public void setEditable(boolean value)

      Lets the user edit the document, or only read and select it.

      Parameters
      • value: true to allow editing
    • getZoom

      public float getZoom()

      The zoom factor on top of the fitted or natural size.

      Returns

      the factor, 1 by default

    • setZoom

      public void setZoom(float value)

      Sets the zoom factor on top of the fitted or natural size.

      Parameters
      • value: between 0.25 and 6
    • isFitWidth

      public boolean isFitWidth()

      Whether a page wider than the view is shrunk to fit its width.

      Returns

      true by default

    • setFitWidth

      public void setFitWidth(boolean value)

      Shrinks pages wider than the view to its width, or shows them at their natural size.

      Parameters
      • value: true to fit
    • getCaret

      public TextPosition getCaret()

      The caret, or null when there is none.

      Returns

      the caret position

    • setCaret

      public void setCaret(TextPosition value)

      Places the caret and clears the selection.

      Parameters
      • value: the position, or null to remove the caret
    • setSelection

      public void setSelection(TextPosition from, TextPosition to)

      Selects the text between two positions; equal positions place the caret.

      Parameters
      • from: where the selection is anchored

      • to: where the caret is

    • getSelection

      public TextPosition[] getSelection()

      The selection, earlier end first, or null when nothing is selected.

      Returns

      the two ends, or null

    • addCaretListener

      public void addCaretListener(ActionListener l)

      Adds a listener told when the caret or the selection moves, for a toolbar showing the style at the caret.

      Parameters
      • l: the listener
    • removeCaretListener

      public void removeCaretListener(ActionListener l)

      Removes a caret listener.

      Parameters
      • l: the listener
    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class ScrollSurface
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class ScrollSurface
    • getScale

      public float getScale()

      Pixels per point at the current size.

      Returns

      the scale

    • setChromeless

      public void setChromeless(boolean value)

      Shows the pages with nothing around them: no desk, no page edge and no white page, one page directly under the other with no gap. A view laid over a slide uses this to edit a text box in place, over the shape's own fill.

      Parameters
      • value: true for no chrome
    • isChromeless

      public boolean isChromeless()

      True when the view shows its pages without chrome.

      Returns

      true without chrome

    • setFixedScale

      public void setFixedScale(float value)

      Pins the scale, in pixels per point, overriding fit-to-width and zoom; zero or less returns to them. A view laid over another painting of the same text uses this so both put every word in the same place.

      Parameters
      • value: pixels per point, or 0
    • scrollChanged

      protected void scrollChanged()
      Description copied from class: ScrollSurface
      The scroll position changed.
      Overrides:
      scrollChanged in class ScrollSurface
    • itemBounds

      public int[] itemBounds(int page, LayoutItem item)

      Where a laid out item is, in the view's own coordinates at the current scroll.

      Parameters
      • page: the item's page

      • item: the item

      Returns

      {x, y, width, height} in pixels

    • layoutSurface

      protected void layoutSurface(int width, int height)
      Description copied from class: ScrollSurface
      Lays the surface out for a size: sets the header sizes and whatever the content's size depends on. Called before every paint and every pointer event, so it has to return at once when nothing has changed.
      Specified by:
      layoutSurface in class ScrollSurface
    • getContentWidth

      protected int getContentWidth()
      Description copied from class: ScrollSurface
      The width of what scrolls, in pixels, without the left header.
      Specified by:
      getContentWidth in class ScrollSurface
    • getContentHeight

      protected int getContentHeight()
      Description copied from class: ScrollSurface
      The height of what scrolls, in pixels, without the top header.
      Specified by:
      getContentHeight in class ScrollSurface
    • zoomBy

      protected boolean zoomBy(float factor, int x, int y)
      Description copied from class: ScrollSurface

      Zooms by factor around a point, for a pinch or Control/Command and the wheel.

      Returns

      false when the surface has no zoom, so the gesture is left alone

      Overrides:
      zoomBy in class ScrollSurface
    • setProofreader

      public void setProofreader(Proofreader p)

      Underlines spelling and grammar problems with a wavy line, red for spelling and blue for grammar.

      Parameters
      • p: the proofreader, or null to stop
    • getProofreader

      public Proofreader getProofreader()

      The proofreader underlining problems.

      Returns

      the proofreader, or null

    • proofIssueAt

      public ProofIssue proofIssueAt(TextPosition at, int max)

      The problem under a text position, with spelling suggestions, for a menu of fixes.

      Parameters
      • at: the position

      • max: the most suggestions

      Returns

      the issue, or null when there is none or no proofreader

    • applySuggestion

      public void applySuggestion(String paragraphId, ProofIssue issue, String replacement)

      Replaces the range of a problem with one of its suggestions, as one undoable edit.

      Parameters
      • paragraphId: the paragraph the issue was found in

      • issue: the issue

      • replacement: the text to put in its place

    • paintContent

      protected void paintContent(Graphics g, int x, int y)
      Description copied from class: ScrollSurface
      Paints what scrolls. x and y are where the content's own origin is on screen, which is off screen once it has scrolled. The graphics is clipped to the content region.
      Specified by:
      paintContent in class ScrollSurface
    • paintComponentBox

      protected void paintComponentBox(Graphics g, LayoutItem item, int x, int y, int w, int h)

      Paints an embedded component's box: its snapshot, or a frame naming its type. A subclass that hosts live components paints them here.

      Parameters
      • g: the graphics

      • item: the laid out component

      • x: the box's left edge in pixels

      • y: its top edge

      • w: its width

      • h: its height

    • getCommentsAtCaret

      public List<Comment> getCommentsAtCaret()

      The review comments over the caret.

      Returns

      the comments, empty when there is no caret

    • commentSelection

      public Comment commentSelection(String text)

      Adds a review comment over the selection, by the model's author.

      Parameters
      • text: the comment
      Returns

      the comment, or null when nothing is selected

    • font

      protected Font font(TextStyle style, int px)

      The platform font text of a style is drawn in, at a pixel size.

      Parameters
      • style: the style

      • px: the size in pixels

      Returns

      the font

    • positionAt

      public TextPosition positionAt(int x, int y)

      The text position under a point in the view.

      Parameters
      • x: in the component's coordinates

      • y: in the component's coordinates

      Returns

      the position, or null

    • tapped

      protected void tapped(int x, int y, boolean doubleTap)
      Description copied from class: ScrollSurface
      A tap that was not a drag or a long press.
      Overrides:
      tapped in class ScrollSurface
    • editStarted

      protected boolean editStarted(int x, int y, ScrollSurface.EditTrigger trigger)
      Description copied from class: ScrollSurface

      Asked when a drag or a long press begins, at the point where the pointer went down, in the component's coordinates.

      Parameters
      • trigger: what kind of gesture it is
      Returns

      true when an edit has begun and the drag belongs to it; false to pan

      Overrides:
      editStarted in class ScrollSurface
    • editMoved

      protected void editMoved(int x, int y)
      Description copied from class: ScrollSurface
      The pointer of an edit moved, or the surface scrolled under it.
      Overrides:
      editMoved in class ScrollSurface
    • editEnded

      protected void editEnded(boolean committed)
      Description copied from class: ScrollSurface
      The edit is over. committed is false when it was cancelled: by cancelEdit, a second finger, or the surface leaving the form.
      Overrides:
      editEnded in class ScrollSurface
    • animateSurface

      protected boolean animateSurface()
      Description copied from class: ScrollSurface

      Advances the view's own motion. Called every frame while the surface animates; call requestAnimation to start the frames.

      Returns

      true while the view still has something moving

      Overrides:
      animateSurface in class ScrollSurface
    • focusGained

      protected void focusGained()
      Description copied from class: Component
      Callback allowing a developer to track when the component gains focus
      Overrides:
      focusGained in class Component
    • focusLost

      protected void focusLost()
      Description copied from class: Component
      Callback allowing a developer to track wheh the component loses focus
      Overrides:
      focusLost in class Component
    • paint

      public void paint(Graphics g)

      Paints the view, then the touch selection handles and toolbar over it.

      Parameters
      • g: the graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class ScrollSurface
    • copySelection

      public void copySelection()
      Copies the selection to the clipboard as plain text, HTML, RTF and Markdown, so it pastes with its formatting into a word processor, a browser or a text editor alike.
    • cutSelection

      public void cutSelection()
      Copies the selection to the clipboard and deletes it.
    • pasteClipboard

      public void pasteClipboard()
      Pastes the clipboard over the selection, keeping formatting when the clipboard holds HTML, RTF or Markdown.
    • selectAll

      public void selectAll()
      Selects the whole document.
    • typeText

      public void typeText(String text)

      Types text at the caret, replacing the selection.

      Parameters
      • text: the text; a line break splits the paragraph
    • deleteBackward

      public void deleteBackward()
      Deletes the selection, or the character before the caret, joining paragraphs at a paragraph start.
    • deleteForward

      public void deleteForward()
      Deletes the selection, or the character after the caret.
    • moveCaret

      public void moveCaret(boolean forward, boolean extend)

      Moves the caret one character, or extends the selection when extend is true.

      Parameters
      • forward: true to move right

      • extend: true to keep the selection anchor

    • moveCaretLine

      public void moveCaretLine(boolean down, boolean extend)

      Moves the caret a line up or down, or extends the selection.

      Parameters
      • down: true to move down

      • extend: true to keep the selection anchor

    • keyPressed

      public void keyPressed(int keyCode)
      Description copied from class: Component

      If this Component is focused, the key pressed event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyPressed in class Component
    • commitText

      public void commitText(String text)

      Typed text from the platform's keyboard or input method.

      Parameters
      • text: the text
      Specified by:
      commitText in interface TextInputClient
    • setComposingText

      public void setComposingText(String text, int relativeCaret)

      Composition is committed whole: the composing text is typed as it is.

      Parameters
      • text: the composing text

      • relativeCaret: ignored

      Specified by:
      setComposingText in interface TextInputClient
    • finishComposing

      public void finishComposing()
      Nothing to do: composing text is never shown before it is committed.
      Specified by:
      finishComposing in interface TextInputClient
    • deleteSurroundingText

      public void deleteSurroundingText(int before, int after)

      Deletes around the caret, within its paragraph.

      Parameters
      • before: characters before the caret

      • after: characters after it

      Specified by:
      deleteSurroundingText in interface TextInputClient
    • onKeyCommand

      public void onKeyCommand(int command, int modifiers)

      A key command from the platform.

      Parameters
      • command: one of the TextInputClient commands

      • modifiers: modifier bits

      Specified by:
      onKeyCommand in interface TextInputClient
    • onEditorAction

      public void onEditorAction(int action)

      The Enter action splits the paragraph.

      Parameters
      • action: the action
      Specified by:
      onEditorAction in interface TextInputClient
    • getEditingState

      public TextInputState getEditingState()

      The caret's paragraph, as the platform sees the text being edited.

      Returns

      the state

      Specified by:
      getEditingState in interface TextInputClient
    • getCaretRect

      public int[] getCaretRect()

      The caret rectangle in screen coordinates.

      Returns

      {x, y, width, height}

      Specified by:
      getCaretRect in interface TextInputClient
    • getConfig

      public TextInputConfig getConfig()

      Multi-line text with autocorrect.

      Returns

      the configuration

      Specified by:
      getConfig in interface TextInputClient
    • inputFocusGained

      public void inputFocusGained()
      Nothing to do.
      Specified by:
      inputFocusGained in interface TextInputClient
    • inputFocusLost

      public void inputFocusLost()
      The platform let go of input.
      Specified by:
      inputFocusLost in interface TextInputClient
    • cancelComposing

      public void cancelComposing()
      Nothing to undo: composing text is never inserted before it is committed.
      Specified by:
      cancelComposing in interface TextInputClient.CompositionCancellation
    • getGeometrySnapshot

      public TextInputGeometry getGeometrySnapshot()

      The caret and selection rectangles the platform places its candidate window by.

      Returns

      the snapshot

      Specified by:
      getGeometrySnapshot in interface TextInputGeometry.Provider
    • getTextLength

      public int getTextLength()

      The length of the caret's paragraph.

      Returns

      the length

      Specified by:
      getTextLength in interface TextInputClient
    • getTextRange

      public String getTextRange(int start, int end)

      Part of the caret's paragraph.

      Parameters
      • start: from

      • end: to

      Returns

      the text

      Specified by:
      getTextRange in interface TextInputClient
    • rectForOffset

      public int[] rectForOffset(int offset)

      Where an offset of the caret's paragraph is on screen.

      Parameters
      • offset: the offset
      Returns

      {x, y, width, height} in screen coordinates

      Specified by:
      rectForOffset in interface TextInputClient
    • offsetAtPoint

      public int offsetAtPoint(int x, int y)

      The offset in the caret's paragraph nearest a screen point.

      Parameters
      • x: screen x

      • y: screen y

      Returns

      the offset

      Specified by:
      offsetAtPoint in interface TextInputClient
    • selectionRects

      public int[] selectionRects(int start, int end)

      The selection rectangles of a range of the caret's paragraph.

      Parameters
      • start: from

      • end: to

      Returns

      the rectangle from the start's caret to the end's, as {x, y, w, h}

      Specified by:
      selectionRects in interface TextInputClient
    • replaceRange

      public void replaceRange(int start, int end, String text)

      Replaces part of the caret's paragraph.

      Parameters
      • start: from

      • end: to

      • text: the replacement

      Specified by:
      replaceRange in interface TextInputClient
    • setSelectionRange

      public void setSelectionRange(int start, int end)

      Selects part of the caret's paragraph.

      Parameters
      • start: the anchor

      • end: the caret

      Specified by:
      setSelectionRange in interface TextInputClient