Skip to content

Commit

Permalink
Merge pull request #250 from JordanMartinez/minorCodeCleanup
Browse files Browse the repository at this point in the history
Minor code refactoring to get rid of some compiler suggestions:
  • Loading branch information
TomasMikula committed Jan 26, 2016
2 parents f1593b1 + 04f6987 commit f24792a
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 45 deletions.
6 changes: 3 additions & 3 deletions richtextfx/src/main/java/org/fxmisc/richtext/AreaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static <PS, S> StyledTextArea<PS, S> styledTextArea(
PS initialParagraphStyle, BiConsumer<TextFlow, PS> applyParagraphStyle,
S initialTextStyle, BiConsumer<? super TextExt, S> applyStyle
) {
return new StyledTextArea<PS, S>(
return new StyledTextArea<>(
initialParagraphStyle, applyParagraphStyle,
initialTextStyle, applyStyle,
true);
Expand All @@ -44,7 +44,7 @@ public static <PS, S> StyledTextArea<PS, S> styledTextArea(
S initialTextStyle, BiConsumer<? super TextExt, S> applyStyle,
boolean preserveStyle
) {
return new StyledTextArea<PS, S>(
return new StyledTextArea<>(
initialParagraphStyle, applyParagraphStyle,
initialTextStyle, applyStyle,
preserveStyle);
Expand All @@ -60,7 +60,7 @@ public static <PS, S>VirtualizedScrollPane<StyledTextArea<PS, S>> embeddedStyled

// Clones StyledTextArea
public static <PS, S> StyledTextArea<PS, S> cloneStyleTextArea(StyledTextArea<PS, S> area) {
return new StyledTextArea<PS, S>(area.getInitialParagraphStyle(), area.getApplyParagraphStyle(),
return new StyledTextArea<>(area.getInitialParagraphStyle(), area.getApplyParagraphStyle(),
area.getInitialTextStyle(), area.getApplyStyle(),
area.getModel().getContent(), area.isPreserveStyle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static class HighlightFillProperty extends StyleableObjectProperty<Paint> {
public HighlightFillProperty(Object bean, Paint initialValue) {
super(initialValue);
this.bean = bean;
cssMetaData = new PropertyCssMetaData<Styleable, Paint>(
cssMetaData = new PropertyCssMetaData<>(
this, "-fx-highlight-fill",
StyleConverter.getPaintConverter(), initialValue);
}
Expand All @@ -79,7 +79,7 @@ public String getName() {
public CssMetaData<? extends Styleable, Paint> getCssMetaData() {
return cssMetaData;
}
};
}

static class HighlightTextFillProperty extends StyleableObjectProperty<Paint> {
private final Object bean;
Expand All @@ -89,7 +89,7 @@ static class HighlightTextFillProperty extends StyleableObjectProperty<Paint> {
public HighlightTextFillProperty(Object bean, Paint initialValue) {
super(initialValue);
this.bean = bean;
cssMetaData = new PropertyCssMetaData<Styleable, Paint>(
cssMetaData = new PropertyCssMetaData<>(
this, "-fx-highlight-text-fill",
StyleConverter.getPaintConverter(), initialValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ public ReadOnlyStyledDocument<PS, S> snapshot() {
public EventStream<RichTextChange<PS, S>> richChanges() { return richChanges; }

{
EventStream<String> removedText = EventStreams.zip(textChangePosition, textRemovalEnd).map(t2 -> t2.map((a, b) -> getText(a, b)));
EventStream<String> removedText = EventStreams.zip(textChangePosition, textRemovalEnd).map(t2 -> t2.map(this::getText));
EventStream<Integer> changePosition = EventStreams.merge(textChangePosition, styleChangePosition);
EventStream<Integer> removalEnd = EventStreams.merge(textRemovalEnd, styleChangeEnd);
EventStream<StyledDocument<PS, S>> removedDocument = EventStreams.zip(changePosition, removalEnd).map(t2 -> t2.map((a, b) -> subSequence(a, b)));
EventStream<StyledDocument<PS, S>> removedDocument = EventStreams.zip(changePosition, removalEnd).map(t2 -> t2.map(this::subSequence));
EventStream<Integer> insertionEnd = styleChangeEnd.emitOn(styleChangeDone);
EventStream<StyledDocument<PS, S>> insertedDocument = EventStreams.merge(
this.insertedDocument,
changePosition.emitBothOnEach(insertionEnd).map(t2 -> t2.map((a, b) -> subSequence(a, b))));
changePosition.emitBothOnEach(insertionEnd).map(t2 -> t2.map(this::subSequence)));

plainTextChanges = EventStreams.zip(textChangePosition, removedText, insertedText)
.filter(t3 -> t3.map((pos, removed, inserted) -> !removed.equals(inserted)))
.map(t3 -> t3.map((pos, removed, inserted) -> new PlainTextChange(pos, removed, inserted)));
.map(t3 -> t3.map(PlainTextChange::new));

richChanges = EventStreams.zip(changePosition, removedDocument, insertedDocument)
.filter(t3 -> t3.map((pos, removed, inserted) -> !removed.equals(inserted)))
.map(t3 -> t3.map((pos, removed, inserted) -> new RichTextChange<>(pos, removed, inserted)));
.map(t3 -> t3.map(RichTextChange::new));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package org.fxmisc.richtext;


import javafx.scene.text.TextFlow;

/**
* Text area that uses inline css to define style of text segments and paragraph segments.
*/
public class InlineCssTextArea extends StyledTextArea<String, String> {

public InlineCssTextArea() {
this(new EditableStyledDocument<String, String>("", ""));
this(new EditableStyledDocument<>("", ""));
}

public InlineCssTextArea(EditableStyledDocument<String, String> document) {
super(
"", (paragraph, style) -> paragraph.setStyle(style),
"", (text, style) -> text.setStyle(style),
"", TextFlow::setStyle,
"", TextExt::setStyle,
document,
true
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ void layoutChildren() {

text.resizeRelocate(graphicWidth, 0, w - graphicWidth, h);

graphic.ifPresent(g -> {
g.resizeRelocate(graphicOffset.get(), 0, graphicWidth, h);
});
graphic.ifPresent(g -> g.resizeRelocate(graphicOffset.get(), 0, graphicWidth, h));
}

double getGraphicPrefWidth() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.IndexRange;
Expand All @@ -25,7 +26,7 @@ class ParagraphText<PS, S> extends TextFlowExt {
// FIXME: changing it currently has not effect, because
// Text.impl_selectionFillProperty().set(newFill) doesn't work
// properly for Text node inside a TextFlow (as of JDK8-b100).
private final ObjectProperty<Paint> highlightTextFill = new SimpleObjectProperty<Paint>(Color.WHITE);
private final ObjectProperty<Paint> highlightTextFill = new SimpleObjectProperty<>(Color.WHITE);
public ObjectProperty<Paint> highlightTextFillProperty() {
return highlightTextFill;
}
Expand Down Expand Up @@ -64,8 +65,8 @@ public ParagraphText(Paragraph<PS, S> par, BiConsumer<? super TextExt, S> applyS

selection.addListener((obs, old, sel) -> requestLayout());

Val<Double> leftInset = Val.map(insetsProperty(), ins -> ins.getLeft());
Val<Double> topInset = Val.map(insetsProperty(), ins -> ins.getTop());
Val<Double> leftInset = Val.map(insetsProperty(), Insets::getLeft);
Val<Double> topInset = Val.map(insetsProperty(), Insets::getTop);

// selection highlight
selectionShape.setManaged(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public StyleClassedTextArea(EditableStyledDocument<Collection<String>, Collectio
}
public StyleClassedTextArea(boolean preserveStyle) {
this(
new EditableStyledDocument<Collection<String>, Collection<String>>(
new EditableStyledDocument<>(
Collections.<String>emptyList(), Collections.<String>emptyList()
), preserveStyle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ default StyleSpans<S> overlay(StyleSpans<S> that, BiFunction<? super S, ? super
}

default Stream<S> styleStream() {
return stream().map(span -> span.getStyle());
return stream().map(StyleSpan::getStyle);
}

default Stream<StyleSpan<S>> stream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Iterator<StyleSpan<S>> iterator() {
@Override
public int length() {
if(length == -1) {
length = spans.stream().mapToInt(span -> span.getLength()).sum();
length = spans.stream().mapToInt(StyleSpan::getLength).sum();
}

return length;
Expand Down Expand Up @@ -174,7 +174,7 @@ private void ensureNotCreated() {

abstract class StyleSpansBase<S> implements StyleSpans<S> {
protected final TwoLevelNavigator navigator = new TwoLevelNavigator(
() -> getSpanCount(),
this::getSpanCount,
i -> getStyleSpan(i).getLength());

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class StyledDocumentBase<PS, S, L extends List<Paragraph<PS, S>>>
protected StyledDocumentBase(L paragraphs) {
this.paragraphs = paragraphs;
navigator = new TwoLevelNavigator(
() -> paragraphs.size(),
paragraphs::size,
i -> paragraphs.get(i).length() + (i == paragraphs.size() - 1 ? 0 : 1));
}

Expand Down Expand Up @@ -79,7 +79,7 @@ public StyledDocument<PS, S> subSequence(int start, int end) {
return sub(
start, end,
p -> p,
(p, a, b) -> p.subSequence(a, b),
Paragraph::subSequence,
(List<Paragraph<PS, S>> pars) -> new ReadOnlyStyledDocument<>(pars, ADOPT));
}

Expand Down Expand Up @@ -172,7 +172,7 @@ public StyleSpans<S> getStyleSpans(int from, int to) {
subSpans.add(endPar.getStyleSpans(0, end.getMinor()));
}

int n = subSpans.stream().mapToInt(sr -> sr.getSpanCount()).sum();
int n = subSpans.stream().mapToInt(StyleSpans::getSpanCount).sum();
StyleSpansBuilder<S> builder = new StyleSpansBuilder<>(n);
for(StyleSpans<S> spans: subSpans) {
for(StyleSpan<S> span: spans) {
Expand Down
8 changes: 4 additions & 4 deletions richtextfx/src/main/java/org/fxmisc/richtext/StyledText.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ public String getText() {
}

public StyledText<S> subSequence(int start, int end) {
return new StyledText<S>(text.substring(start, end), style);
return new StyledText<>(text.substring(start, end), style);
}

public StyledText<S> subSequence(int start) {
return new StyledText<S>(text.substring(start), style);
return new StyledText<>(text.substring(start), style);
}

public StyledText<S> append(String str) {
return new StyledText<S>(text + str, style);
return new StyledText<>(text + str, style);
}

public StyledText<S> spliced(int from, int to, CharSequence replacement) {
String left = text.substring(0, from);
String right = text.substring(to);
return new StyledText<S>(left + replacement + right, style);
return new StyledText<>(left + replacement + right, style);
}

public S getStyle() {
Expand Down
10 changes: 4 additions & 6 deletions richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public Optional<Tuple2<Codec<PS>, Codec<S>>> getStyleCodecs() {
@Override public final ObservableValue<String> textProperty() { return model.textProperty(); }

// rich text
@Override public final StyledDocument<PS, S> getDocument() { return model.getDocument(); };
@Override public final StyledDocument<PS, S> getDocument() { return model.getDocument(); }

// length
@Override public final int getLength() { return model.getLength(); }
Expand Down Expand Up @@ -475,12 +475,12 @@ public StyledTextArea(PS initialParagraphStyle, BiConsumer<TextFlow, PS> applyPa
this(initialParagraphStyle, applyParagraphStyle, initialTextStyle, applyStyle, true);
}

public <C> StyledTextArea(PS initialParagraphStyle, BiConsumer<TextFlow, PS> applyParagraphStyle,
public StyledTextArea(PS initialParagraphStyle, BiConsumer<TextFlow, PS> applyParagraphStyle,
S initialTextStyle, BiConsumer<? super TextExt, S> applyStyle,
boolean preserveStyle
) {
this(initialParagraphStyle, applyParagraphStyle, initialTextStyle, applyStyle,
new EditableStyledDocument<PS, S>(initialParagraphStyle, initialTextStyle), preserveStyle);
new EditableStyledDocument<>(initialParagraphStyle, initialTextStyle), preserveStyle);
}

/**
Expand All @@ -500,7 +500,7 @@ public StyledTextArea(PS initialParagraphStyle, BiConsumer<TextFlow, PS> applyPa
S initialTextStyle, BiConsumer<? super TextExt, S> applyStyle,
EditableStyledDocument<PS, S> document, boolean preserveStyle
) {
this.model = new StyledTextAreaModel<PS, S>(initialParagraphStyle, initialTextStyle, document, preserveStyle);
this.model = new StyledTextAreaModel<>(initialParagraphStyle, initialTextStyle, document, preserveStyle);
this.applyStyle = applyStyle;
this.applyParagraphStyle = applyParagraphStyle;

Expand All @@ -521,7 +521,6 @@ public StyledTextArea(PS initialParagraphStyle, BiConsumer<TextFlow, PS> applyPa
Cell<Paragraph<PS, S>, ParagraphBox<PS, S>> cell = createCell(
par,
applyStyle,
initialParagraphStyle,
applyParagraphStyle);
nonEmptyCells.add(cell.getNode());
return cell.beforeReset(() -> nonEmptyCells.remove(cell.getNode()))
Expand Down Expand Up @@ -1023,7 +1022,6 @@ protected void layoutChildren() {
private Cell<Paragraph<PS, S>, ParagraphBox<PS, S>> createCell(
Paragraph<PS, S> paragraph,
BiConsumer<? super TextExt, S> applyStyle,
PS initialParagraphStyle,
BiConsumer<TextFlow, PS> applyParagraphStyle) {

ParagraphBox<PS, S> box = new ParagraphBox<>(paragraph, applyParagraphStyle, applyStyle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static int clamp(int min, int val, int max) {
@Override public final ObservableValue<String> textProperty() { return text; }

// rich text
@Override public final StyledDocument<PS, S> getDocument() { return content.snapshot(); };
@Override public final StyledDocument<PS, S> getDocument() { return content.snapshot(); }

// length
private final SuspendableVal<Integer> length;
Expand Down Expand Up @@ -212,8 +212,6 @@ private static int clamp(int min, int val, int max) {
private final boolean preserveStyle;
protected final boolean isPreserveStyle() { return preserveStyle; }

private final Suspendable omniSuspendable;


/* ********************************************************************** *
* *
Expand All @@ -233,10 +231,10 @@ public StyledTextAreaModel(PS initialParagraphStyle, S initialTextStyle) {
this(initialParagraphStyle, initialTextStyle, true);
}

public <C> StyledTextAreaModel(PS initialParagraphStyle, S initialTextStyle, boolean preserveStyle
public StyledTextAreaModel(PS initialParagraphStyle, S initialTextStyle, boolean preserveStyle
) {
this(initialParagraphStyle, initialTextStyle,
new EditableStyledDocument<PS, S>(initialParagraphStyle, initialTextStyle), preserveStyle);
new EditableStyledDocument<>(initialParagraphStyle, initialTextStyle), preserveStyle);
}

/**
Expand Down Expand Up @@ -340,7 +338,7 @@ public StyledTextAreaModel(PS initialParagraphStyle, S initialTextStyle,
() -> content.getText(internalSelection.getValue()),
internalSelection, content.getParagraphs()).suspendable();

omniSuspendable = Suspendable.combine(
final Suspendable omniSuspendable = Suspendable.combine(
beingUpdated, // must be first, to be the last one to release
text,
length,
Expand Down Expand Up @@ -709,13 +707,13 @@ private void manageSubscription(Subscription subscription) {

private UndoManager createPlainUndoManager(UndoManagerFactory factory) {
Consumer<PlainTextChange> apply = change -> replaceText(change.getPosition(), change.getPosition() + change.getRemoved().length(), change.getInserted());
BiFunction<PlainTextChange, PlainTextChange, Optional<PlainTextChange>> merge = (change1, change2) -> change1.mergeWith(change2);
BiFunction<PlainTextChange, PlainTextChange, Optional<PlainTextChange>> merge = PlainTextChange::mergeWith;
return factory.create(plainTextChanges(), PlainTextChange::invert, apply, merge);
}

private UndoManager createRichUndoManager(UndoManagerFactory factory) {
Consumer<RichTextChange<PS, S>> apply = change -> replace(change.getPosition(), change.getPosition() + change.getRemoved().length(), change.getInserted());
BiFunction<RichTextChange<PS, S>, RichTextChange<PS, S>, Optional<RichTextChange<PS, S>>> merge = (change1, change2) -> change1.mergeWith(change2);
BiFunction<RichTextChange<PS, S>, RichTextChange<PS, S>, Optional<RichTextChange<PS, S>>> merge = RichTextChange<PS, S>::mergeWith;
return factory.create(richChanges(), RichTextChange::invert, apply, merge);
}

Expand Down

0 comments on commit f24792a

Please sign in to comment.