Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple esd #309

Merged
merged 3 commits into from
May 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class InlineCssTextArea extends StyledTextArea<String, String> {

public InlineCssTextArea() {
this(new EditableStyledDocumentImpl<>("", ""));
this(new SimpleEditableStyledDocument<>("", ""));
}

public InlineCssTextArea(EditableStyledDocument<String, String> document) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* Provides an implementation of {@link EditableStyledDocument}
*/
final class EditableStyledDocumentImpl<PS, S> implements EditableStyledDocument<PS, S> {
public final class SimpleEditableStyledDocument<PS, S> implements EditableStyledDocument<PS, S> {

private class ParagraphList
extends LiveListBase<Paragraph<PS, S>>
Expand Down Expand Up @@ -84,11 +84,14 @@ public ReadOnlyStyledDocument<PS, S> snapshot() {
@Override public final boolean isBeingUpdated() { return beingUpdated.get(); }


EditableStyledDocumentImpl(Paragraph<PS, S> initialParagraph) {
SimpleEditableStyledDocument(Paragraph<PS, S> 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));
}

Expand All @@ -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<PS, S> doc = ReadOnlyStyledDocument.fromString(
text, getParagraphStyleAtPosition(start), getStyleAtPosition(start));
replace(start, end, doc);
}

@Override
public void replace(int start, int end, StyledDocument<PS, S> replacement) {
ensureValidRange(start, end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public StyleClassedTextArea(EditableStyledDocument<Collection<String>, Collectio
}
public StyleClassedTextArea(boolean preserveStyle) {
this(
new EditableStyledDocumentImpl<>(
new SimpleEditableStyledDocument<>(
Collections.<String>emptyList(), Collections.<String>emptyList()
), preserveStyle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public StyledTextArea(PS initialParagraphStyle, BiConsumer<TextFlow, PS> applyPa
boolean preserveStyle
) {
this(initialParagraphStyle, applyParagraphStyle, initialTextStyle, applyStyle,
new EditableStyledDocumentImpl<>(initialParagraphStyle, initialTextStyle), preserveStyle);
new SimpleEditableStyledDocument<>(initialParagraphStyle, initialTextStyle), preserveStyle);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PS, S> void replaceText(EditableStyledDocument<PS, S> doc, int start, int end, String text) {
StyledDocument<PS, S> styledDoc = ReadOnlyStyledDocument.fromString(
text, doc.getParagraphStyleAtPosition(start), doc.getStyleAtPosition(start));
doc.replace(start, end, styledDoc);
}

@Test
public void testConsistencyOfTextWithLength() {
EditableStyledDocumentImpl<String, String> document = new EditableStyledDocumentImpl<>("", "");
SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
document.getText(); // enforce evaluation of text property
document.getLength(); // enforce evaluation of length property

Expand All @@ -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<String, String> document = new EditableStyledDocumentImpl<>("", "");
SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
document.getText(); // enforce evaluation of text property
document.getLength(); // enforce evaluation of length property

Expand All @@ -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<String, String> document = new EditableStyledDocumentImpl<>("", "");
SimpleEditableStyledDocument<String, String> 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<String, String> document = new EditableStyledDocumentImpl<>("", "");
SimpleEditableStyledDocument<String, String> 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<String, String> document = new EditableStyledDocumentImpl<>("", "");
SimpleEditableStyledDocument<String, String> 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<Boolean, String> doc = new EditableStyledDocumentImpl<>(true, "");
SimpleEditableStyledDocument<Boolean, String> 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<String, String> document = new EditableStyledDocumentImpl<>("", "");
document.replaceText(0, 0, "X\r\nY");
SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
replaceText(document, 0, 0, "X\r\nY");
assertEquals(document.getText().length(), document.getLength());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void testForBug216() {
// set up area with some styled text content
boolean initialStyle = false;
StyledTextAreaModel<String, Boolean> model = new StyledTextAreaModel<>(
"", initialStyle, new EditableStyledDocumentImpl<>("", initialStyle), true);
"", initialStyle, new SimpleEditableStyledDocument<>("", initialStyle), true);
model.replaceText("testtest");
model.setStyle(0, 8, true);

Expand Down