diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/InlineCssTextArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/InlineCssTextArea.java index 480f5eafa..4f3edaad2 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/InlineCssTextArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/InlineCssTextArea.java @@ -9,7 +9,7 @@ public class InlineCssTextArea extends StyledTextArea { public InlineCssTextArea() { - this(new EditableStyledDocumentImpl<>("", "")); + this(new SimpleEditableStyledDocument<>("", "")); } public InlineCssTextArea(EditableStyledDocument document) { diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/EditableStyledDocumentImpl.java b/richtextfx/src/main/java/org/fxmisc/richtext/SimpleEditableStyledDocument.java similarity index 92% rename from richtextfx/src/main/java/org/fxmisc/richtext/EditableStyledDocumentImpl.java rename to richtextfx/src/main/java/org/fxmisc/richtext/SimpleEditableStyledDocument.java index 992583a32..71554f63d 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/EditableStyledDocumentImpl.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/SimpleEditableStyledDocument.java @@ -22,7 +22,7 @@ /** * Provides an implementation of {@link EditableStyledDocument} */ -final class EditableStyledDocumentImpl implements EditableStyledDocument { +public final class SimpleEditableStyledDocument implements EditableStyledDocument { private class ParagraphList extends LiveListBase> @@ -84,11 +84,14 @@ public ReadOnlyStyledDocument snapshot() { @Override public final boolean isBeingUpdated() { return beingUpdated.get(); } - EditableStyledDocumentImpl(Paragraph initialParagraph) { + SimpleEditableStyledDocument(Paragraph initialParagraph) { this.doc = new ReadOnlyStyledDocument<>(Collections.singletonList(initialParagraph)); } - EditableStyledDocumentImpl(PS initialParagraphStyle, S initialStyle) { + /** + * Creates an empty {@link EditableStyledDocument} + */ + public SimpleEditableStyledDocument(PS initialParagraphStyle, S initialStyle) { this(new Paragraph<>(initialParagraphStyle, "", initialStyle)); } @@ -103,16 +106,6 @@ public Position offsetToPosition(int offset, Bias bias) { return doc.offsetToPosition(offset, bias); } - /** - * The style of the inserted text will be the style at position - * {@code start} in the current document. - */ - public void replaceText(int start, int end, String text) { - StyledDocument doc = ReadOnlyStyledDocument.fromString( - text, getParagraphStyleAtPosition(start), getStyleAtPosition(start)); - replace(start, end, doc); - } - @Override public void replace(int start, int end, StyledDocument replacement) { ensureValidRange(start, end); diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/StyleClassedTextArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/StyleClassedTextArea.java index 514473f0a..d23fcec02 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/StyleClassedTextArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/StyleClassedTextArea.java @@ -23,7 +23,7 @@ public StyleClassedTextArea(EditableStyledDocument, Collectio } public StyleClassedTextArea(boolean preserveStyle) { this( - new EditableStyledDocumentImpl<>( + new SimpleEditableStyledDocument<>( Collections.emptyList(), Collections.emptyList() ), preserveStyle); } diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java index 7210fa473..bb4a7ce1b 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java @@ -507,7 +507,7 @@ public StyledTextArea(PS initialParagraphStyle, BiConsumer applyPa boolean preserveStyle ) { this(initialParagraphStyle, applyParagraphStyle, initialTextStyle, applyStyle, - new EditableStyledDocumentImpl<>(initialParagraphStyle, initialTextStyle), preserveStyle); + new SimpleEditableStyledDocument<>(initialParagraphStyle, initialTextStyle), preserveStyle); } /** diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaModel.java b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaModel.java index 900ded381..0bd429fd2 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaModel.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaModel.java @@ -233,7 +233,7 @@ public StyledTextAreaModel(PS initialParagraphStyle, S initialTextStyle) { public StyledTextAreaModel(PS initialParagraphStyle, S initialTextStyle, boolean preserveStyle ) { this(initialParagraphStyle, initialTextStyle, - new EditableStyledDocumentImpl<>(initialParagraphStyle, initialTextStyle), preserveStyle); + new SimpleEditableStyledDocument<>(initialParagraphStyle, initialTextStyle), preserveStyle); } /** diff --git a/richtextfx/src/test/java/org/fxmisc/richtext/EditableStyledDocumentImplTest.java b/richtextfx/src/test/java/org/fxmisc/richtext/SimpleEditableStyledDocumentTest.java similarity index 52% rename from richtextfx/src/test/java/org/fxmisc/richtext/EditableStyledDocumentImplTest.java rename to richtextfx/src/test/java/org/fxmisc/richtext/SimpleEditableStyledDocumentTest.java index d52de8fe5..fb9247c1a 100644 --- a/richtextfx/src/test/java/org/fxmisc/richtext/EditableStyledDocumentImplTest.java +++ b/richtextfx/src/test/java/org/fxmisc/richtext/SimpleEditableStyledDocumentTest.java @@ -4,11 +4,21 @@ import org.junit.Test; -public class EditableStyledDocumentImplTest { +public class SimpleEditableStyledDocumentTest { + + /** + * The style of the inserted text will be the style at position + * {@code start} in the current document. + */ + private void replaceText(EditableStyledDocument doc, int start, int end, String text) { + StyledDocument styledDoc = ReadOnlyStyledDocument.fromString( + text, doc.getParagraphStyleAtPosition(start), doc.getStyleAtPosition(start)); + doc.replace(start, end, styledDoc); + } @Test public void testConsistencyOfTextWithLength() { - EditableStyledDocumentImpl document = new EditableStyledDocumentImpl<>("", ""); + SimpleEditableStyledDocument document = new SimpleEditableStyledDocument<>("", ""); document.getText(); // enforce evaluation of text property document.getLength(); // enforce evaluation of length property @@ -18,12 +28,12 @@ public void testConsistencyOfTextWithLength() { assertEquals(length, textLength); }); - document.replaceText(0, 0, "A"); + replaceText(document, 0, 0, "A"); } @Test public void testConsistencyOfLengthWithText() { - EditableStyledDocumentImpl document = new EditableStyledDocumentImpl<>("", ""); + SimpleEditableStyledDocument document = new SimpleEditableStyledDocument<>("", ""); document.getText(); // enforce evaluation of text property document.getLength(); // enforce evaluation of length property @@ -33,54 +43,54 @@ public void testConsistencyOfLengthWithText() { assertEquals(textLength, length); }); - document.replaceText(0, 0, "A"); + replaceText(document, 0, 0, "A"); } @Test public void testUnixParagraphCount() { - EditableStyledDocumentImpl document = new EditableStyledDocumentImpl<>("", ""); + SimpleEditableStyledDocument document = new SimpleEditableStyledDocument<>("", ""); String text = "X\nY"; - document.replaceText(0, 0, text); + replaceText(document, 0, 0, text); assertEquals(2, document.getParagraphs().size()); } @Test public void testMacParagraphCount() { - EditableStyledDocumentImpl document = new EditableStyledDocumentImpl<>("", ""); + SimpleEditableStyledDocument document = new SimpleEditableStyledDocument<>("", ""); String text = "X\rY"; - document.replaceText(0, 0, text); + replaceText(document, 0, 0, text); assertEquals(2, document.getParagraphs().size()); } @Test public void testWinParagraphCount() { - EditableStyledDocumentImpl document = new EditableStyledDocumentImpl<>("", ""); + SimpleEditableStyledDocument document = new SimpleEditableStyledDocument<>("", ""); String text = "X\r\nY"; - document.replaceText(0, 0, text); + replaceText(document, 0, 0, text); assertEquals(2, document.getParagraphs().size()); } @Test public void testGetTextWithEndAfterNewline() { - EditableStyledDocumentImpl doc = new EditableStyledDocumentImpl<>(true, ""); + SimpleEditableStyledDocument doc = new SimpleEditableStyledDocument<>(true, ""); - doc.replaceText(0, 0, "123\n"); + replaceText(doc, 0, 0, "123\n"); String txt1 = doc.getText(0, 4); assertEquals(4, txt1.length()); - doc.replaceText(4, 4, "567"); + replaceText(doc, 4, 4, "567"); String txt2 = doc.getText(2, 4); assertEquals(2, txt2.length()); - doc.replaceText(4, 4, "\n"); + replaceText(doc, 4, 4, "\n"); String txt3 = doc.getText(2, 4); assertEquals(2, txt3.length()); } @Test public void testWinDocumentLength() { - EditableStyledDocumentImpl document = new EditableStyledDocumentImpl<>("", ""); - document.replaceText(0, 0, "X\r\nY"); + SimpleEditableStyledDocument document = new SimpleEditableStyledDocument<>("", ""); + replaceText(document, 0, 0, "X\r\nY"); assertEquals(document.getText().length(), document.getLength()); } } diff --git a/richtextfx/src/test/java/org/fxmisc/richtext/StyledTextAreaModelTest.java b/richtextfx/src/test/java/org/fxmisc/richtext/StyledTextAreaModelTest.java index e627d2e38..835086464 100644 --- a/richtextfx/src/test/java/org/fxmisc/richtext/StyledTextAreaModelTest.java +++ b/richtextfx/src/test/java/org/fxmisc/richtext/StyledTextAreaModelTest.java @@ -32,7 +32,7 @@ public void testForBug216() { // set up area with some styled text content boolean initialStyle = false; StyledTextAreaModel model = new StyledTextAreaModel<>( - "", initialStyle, new EditableStyledDocumentImpl<>("", initialStyle), true); + "", initialStyle, new SimpleEditableStyledDocument<>("", initialStyle), true); model.replaceText("testtest"); model.setStyle(0, 8, true);