All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fxmisc.richtext.model.GenericEditableStyledDocumentBase Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.fxmisc.richtext.model;

import static org.fxmisc.richtext.model.TwoDimensional.Bias.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.reactfx.EventSource;
import org.reactfx.EventStream;
import org.reactfx.Subscription;
import org.reactfx.Suspendable;
import org.reactfx.SuspendableEventStream;
import org.reactfx.SuspendableNo;
import org.reactfx.collection.LiveList;
import org.reactfx.collection.LiveListBase;
import org.reactfx.collection.MaterializedListModification;
import org.reactfx.collection.QuasiListModification;
import org.reactfx.collection.SuspendableList;
import org.reactfx.collection.UnmodifiableByDefaultLiveList;
import org.reactfx.util.BiIndex;
import org.reactfx.util.Lists;
import org.reactfx.value.SuspendableVal;
import org.reactfx.value.Val;

class GenericEditableStyledDocumentBase implements EditableStyledDocument {

    private class ParagraphList
    extends LiveListBase>
    implements UnmodifiableByDefaultLiveList> {

        @Override
        public Paragraph get(int index) {
            return doc.getParagraph(index);
        }

        @Override
        public int size() {
            return doc.getParagraphCount();
        }

        @Override
        protected Subscription observeInputs() {
            return parChanges.subscribe(mod -> {
                mod = mod.trim();
                QuasiListModification> qmod =
                        QuasiListModification.create(mod.getFrom(), mod.getRemoved(), mod.getAddedSize());
                notifyObservers(qmod.asListChange());
            });
        }
    }

    private ReadOnlyStyledDocument doc;

    private final EventSource> internalRichChanges = new EventSource<>();
    private final SuspendableEventStream> richChanges = internalRichChanges.pausable();
    @Override public EventStream> richChanges() { return richChanges; }

    private final Val internalText = Val.create(() -> doc.getText(), internalRichChanges);
    private final SuspendableVal text = internalText.suspendable();
    @Override public String getText() { return text.getValue(); }
    @Override public Val textProperty() { return text; }


    private final Val internalLength = Val.create(() -> doc.length(), internalRichChanges);
    private final SuspendableVal length = internalLength.suspendable();
    @Override public int getLength() { return length.getValue(); }
    @Override public Val lengthProperty() { return length; }
    @Override public int length() { return length.getValue(); }

    private final EventSource>> parChanges =
            new EventSource<>();

    private final SuspendableList> paragraphs = new ParagraphList().suspendable();
    @Override
    public LiveList> getParagraphs() {
        return paragraphs;
    }

    @Override
    public ReadOnlyStyledDocument snapshot() {
        return doc;
    }

    private final SuspendableNo beingUpdated = new SuspendableNo();
    @Override public final SuspendableNo beingUpdatedProperty() { return beingUpdated; }
    @Override public final boolean isBeingUpdated() { return beingUpdated.get(); }

    GenericEditableStyledDocumentBase(Paragraph initialParagraph) {
        this.doc = new ReadOnlyStyledDocument<>(Collections.singletonList(initialParagraph));

        final Suspendable omniSuspendable = Suspendable.combine(
                text,
                length,

                // add streams after properties, to be released before them
                richChanges,

                // paragraphs to be released first
                paragraphs);
        omniSuspendable.suspendWhen(beingUpdated);
    }

    /**
     * Creates an empty {@link EditableStyledDocument}
     */
    public GenericEditableStyledDocumentBase(PS initialParagraphStyle, S initialStyle, SegmentOps segmentOps) {
        this(new Paragraph<>(initialParagraphStyle, segmentOps, segmentOps.createEmptySeg(), initialStyle));
    }


    @Override
    public Position position(int major, int minor) {
        return doc.position(major, minor);
    }

    @Override
    public Position offsetToPosition(int offset, Bias bias) {
        return doc.offsetToPosition(offset, bias);
    }

    @Override
    public void replace(int start, int end, StyledDocument replacement) {
        ensureValidRange(start, end);
        doc.replace(start, end, ReadOnlyStyledDocument.from(replacement)).exec(this::update);
    }

    @Override
    public void setStyle(int from, int to, S style) {
        ensureValidRange(from, to);
        doc.replace(from, to, removed -> removed.mapParagraphs(par -> par.restyle(style))).exec(this::update);
    }

    @Override
    public void setStyle(int paragraphIndex, S style) {
        ensureValidParagraphIndex(paragraphIndex);
        doc.replaceParagraph(paragraphIndex, p -> p.restyle(style)).exec(this::update);
    }

    @Override
    public void setStyle(int paragraphIndex, int fromCol, int toCol, S style) {
        ensureValidParagraphRange(paragraphIndex, fromCol, toCol);
        doc.replace(
            new BiIndex(paragraphIndex, fromCol),
            new BiIndex(paragraphIndex, toCol),
            d -> d.mapParagraphs(p -> p.restyle(style))
        ).exec(this::update);
    }

    @Override
    public void setStyleSpans(int from, StyleSpans styleSpans) {
        int len = styleSpans.length();
        ensureValidRange(from, from + len);
        doc.replace(from, from + len, d -> {
            Position i = styleSpans.position(0, 0);
            List> pars = new ArrayList<>(d.getParagraphs().size());
            for(Paragraph p: d.getParagraphs()) {
                Position j = i.offsetBy(p.length(), Backward);
                StyleSpans spans = styleSpans.subView(i, j);
                pars.add(p.restyle(0, spans));
                i = j.offsetBy(1, Forward); // skip the newline
            }
            return new ReadOnlyStyledDocument<>(pars);
        }).exec(this::update);
    }

    @Override
    public void setStyleSpans(int paragraphIndex, int from, StyleSpans styleSpans) {
        setStyleSpans(doc.position(paragraphIndex, from).toOffset(), styleSpans);
    }

    @Override
    public void setParagraphStyle(int paragraphIndex, PS style) {
        ensureValidParagraphIndex(paragraphIndex);
        doc.replaceParagraph(paragraphIndex, p -> p.setParagraphStyle(style)).exec(this::update);
    }

    @Override
    public StyledDocument concat(StyledDocument that) {
        return doc.concat(that);
    }

    @Override
    public StyledDocument subSequence(int start, int end) {
        return doc.subSequence(start, end);
    }


    /* ********************************************************************** *
     *                                                                        *
     * Private and package private methods                                    *
     *                                                                        *
     * ********************************************************************** */

    private void ensureValidParagraphIndex(int parIdx) {
        Lists.checkIndex(parIdx, doc.getParagraphCount());
    }

    private void ensureValidRange(int start, int end) {
        Lists.checkRange(start, end, length());
    }

    private void ensureValidParagraphRange(int par, int start, int end) {
        ensureValidParagraphIndex(par);
        Lists.checkRange(start, end, fullLength(par));
    }

    private int fullLength(int par) {
        int n = doc.getParagraphCount();
        return doc.getParagraph(par).length() + (par == n-1 ? 0 : 1);
    }

    private void update(
            ReadOnlyStyledDocument newValue,
            RichTextChange change,
            MaterializedListModification> parChange) {
        this.doc = newValue;
        beingUpdated.suspendWhile(() -> {
            internalRichChanges.push(change);
            parChanges.push(parChange);
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy