From 934352ae2da027546b3822f2e23bc9cbbefcce6a Mon Sep 17 00:00:00 2001 From: Jurgen Date: Sun, 5 Jan 2020 10:20:48 +0200 Subject: [PATCH] Revert "Fixed memory leak in ParagraphText (#886)" (#892) This reverts commit a227e06531d438fa726dbfcc82e7db679cb0e492. --- .../org/fxmisc/richtext/ParagraphText.java | 49 +++++++------------ 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java index 926b6eaca..755b5e364 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java @@ -14,7 +14,6 @@ import java.util.function.Function; import java.util.function.Supplier; import java.util.function.UnaryOperator; -import java.util.stream.Collectors; import org.fxmisc.richtext.model.Paragraph; import org.fxmisc.richtext.model.StyledSegment; @@ -71,8 +70,7 @@ public ObjectProperty highlightTextFillProperty() { return highlightTextFill; } - private Paragraph paragraph; - private Function, Node> nodeMaker; + private final Paragraph paragraph; private final CustomCssShapeHelper backgroundShapeHelper; private final CustomCssShapeHelper borderShapeHelper; @@ -90,6 +88,7 @@ public ObjectProperty highlightTextFillProperty() { private int selectionShapeStartIndex = 0; ParagraphText(Paragraph par, Function, Node> nodeFactory) { + this.paragraph = par; getStyleClass().add("paragraph-text"); @@ -150,6 +149,17 @@ public ObjectProperty highlightTextFillProperty() { // } // }); + // populate with text nodes + par.getStyledSegments().stream().map(nodeFactory).forEach(n -> { + if (n instanceof TextExt) { + TextExt t = (TextExt) n; + // XXX: binding selectionFill to textFill, + // see the note at highlightTextFill + JavaFXCompatibility.Text_selectionFillProperty(t).bind(t.fillProperty()); + } + getChildren().add(n); + }); + // set up custom css shape helpers UnaryOperator configurePath = shape -> { shape.setManaged(false); @@ -208,45 +218,20 @@ public ObjectProperty highlightTextFillProperty() { addToForeground, clearUnusedShapes ); - - // populate with text nodes - nodeMaker = nodeFactory; - setParagraph( par ); } void dispose() { - carets.clear(); - selections.clear(); // this removes listeners (in selections and carets listeners) and avoids memory leaks - selections.removeListener( selectionPathListener ); - carets.removeListener( caretNodeListener ); - setParagraph( null ); + selections.removeListener( selectionPathListener ); + carets.removeListener( caretNodeListener ); + selections.clear(); + carets.clear(); } public Paragraph getParagraph() { return paragraph; } - public void setParagraph(Paragraph par) { - getChildren().stream().filter( n -> n instanceof TextExt ).map( n -> (TextExt) n ) - .forEach( t -> JavaFXCompatibility.Text_selectionFillProperty(t).unbind() ); - - getChildren().setAll( selections.values() ); - - if ( par != null ) getChildren().addAll( par.getStyledSegments().stream().map(nodeMaker) - .peek( n -> { - if (n instanceof TextExt) { - TextExt t = (TextExt) n; - // XXX: binding selectionFill to textFill, - // see the note at highlightTextFill - JavaFXCompatibility.Text_selectionFillProperty(t).bind(t.fillProperty()); - } - }).collect( Collectors.toList() ) ); - - getChildren().addAll( carets ); - paragraph = par; - } - public double getCaretOffsetX(T caret) { layout(); // ensure layout, is a no-op if not dirty checkWithinParagraph(caret);