Skip to content

Commit

Permalink
Workaround for JDK-8130458: use ReactFX bindings.
Browse files Browse the repository at this point in the history
Resolves #159, resolves #176.
  • Loading branch information
TomasMikula committed Sep 5, 2015
1 parent 9408471 commit 869e0aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public String toString() {

public Property<Paint> highlightTextFillProperty() { return text.highlightTextFillProperty(); }

public Property<Number> caretPositionProperty() { return text.caretPositionProperty(); }
public Var<Integer> caretPositionProperty() { return text.caretPositionProperty(); }

public Property<IndexRange> selectionProperty() { return text.selectionProperty(); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
import java.util.Optional;
import java.util.function.BiConsumer;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Bounds;
Expand All @@ -26,6 +21,7 @@
import org.fxmisc.richtext.StyledText;
import org.fxmisc.richtext.StyledTextArea;
import org.reactfx.value.Val;
import org.reactfx.value.Var;

class ParagraphText<S> extends TextFlowExt {

Expand All @@ -37,10 +33,10 @@ public ObjectProperty<Paint> highlightTextFillProperty() {
return highlightTextFill;
}

private final IntegerProperty caretPosition = new SimpleIntegerProperty(0);
public IntegerProperty caretPositionProperty() { return caretPosition; }
public void setCaretPosition(int pos) { caretPosition.set(pos); }
private final NumberBinding clampedCaretPosition;
private final Var<Integer> caretPosition = Var.newSimpleVar(0);
public Var<Integer> caretPositionProperty() { return caretPosition; }
public void setCaretPosition(int pos) { caretPosition.setValue(pos); }
private final Val<Integer> clampedCaretPosition;

private final ObjectProperty<IndexRange> selection = new SimpleObjectProperty<>(StyledTextArea.EMPTY_RANGE);
public ObjectProperty<IndexRange> selectionProperty() { return selection; }
Expand All @@ -52,12 +48,21 @@ public ObjectProperty<Paint> highlightTextFillProperty() {
private final Path selectionShape = new Path();
private final List<Path> backgroundShapes = new ArrayList<>();

// proxy for caretShape.visibleProperty() that implements unbind() correctly.
// This is necessary due to a bug in BooleanPropertyBase#unbind().
// See https://bugs.openjdk.java.net/browse/JDK-8130458
private final Var<Boolean> caretVisible = Var.newSimpleVar(false);
{
caretShape.visibleProperty().bind(caretVisible);
}

public ParagraphText(Paragraph<S> par, BiConsumer<? super TextExt, S> applyStyle) {
this.paragraph = par;

getStyleClass().add("paragraph-text");

clampedCaretPosition = Bindings.min(caretPosition, paragraph.length());
int parLen = paragraph.length();
clampedCaretPosition = caretPosition.map(i -> Math.min(i, parLen));
clampedCaretPosition.addListener((obs, oldPos, newPos) -> requestLayout());

selection.addListener((obs, old, sel) -> requestLayout());
Expand Down Expand Up @@ -120,8 +125,8 @@ public Paragraph<S> getParagraph() {
return paragraph;
}

public BooleanProperty caretVisibleProperty() {
return caretShape.visibleProperty();
public Var<Boolean> caretVisibleProperty() {
return caretVisible;
}

public ObjectProperty<Paint> highlightFillProperty() {
Expand Down Expand Up @@ -156,11 +161,11 @@ public Optional<Bounds> getSelectionBoundsOnScreen() {
}

public int currentLineIndex() {
return getLineOfCharacter(clampedCaretPosition.intValue());
return getLineOfCharacter(clampedCaretPosition.getValue());
}

private void updateCaretShape() {
PathElement[] shape = getCaretShape(clampedCaretPosition.intValue(), true);
PathElement[] shape = getCaretShape(clampedCaretPosition.getValue(), true);
caretShape.getElements().setAll(shape);
}

Expand Down

0 comments on commit 869e0aa

Please sign in to comment.