Trying to understand why my Jfx CodeArea works so slowly I wrote a test 
application (see below).
And I got interesting results. If you start the application you will see that 
about 10 paragraphs
are visible.

1. If after start you click on the report button you will see that about 113 
paragraphs were created (remember 10 visible).
2. If you click event button (as I understand to update only 15 paragraphs - I 
can be wrong here) in the report you will see 112 paragraphs.
3. If you scroll to line 100, click report to clear, add one letter via 
keyboard and in the new report you will see 210 paragraphs were updated.
So, with one paragraph modified and 10 paragraphs visible CodeArea updates 210 
paragraphs.

I see two solutions here:
1) If Jfx CodeArea wants to update paragraphs itself it should be optimized
2) A better solution is to remove final modifier from 
CodeTextModel.getParagraph(int index) method so that we could override it.

public class JfxTextArea extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        CodeArea codeArea = new CodeArea();
        codeArea.setLineNumbersEnabled(true);
        final List<Integer> requestedIndexes = new ArrayList<>();
        codeArea.setSyntaxDecorator(new SyntaxDecorator() {
            @Override
            public RichParagraph createRichParagraph(CodeTextModel ctm, int i) {
                requestedIndexes.add(i);
                RichParagraph.Builder b = RichParagraph.builder();
                b.addSegment(ctm.getPlainText(i));
                return b.build();
            }

            @Override
            public void handleChange(CodeTextModel ctm, TextPos tp, TextPos 
tp1, int i, int i1, int i2) {

            }
        });
        StringBuilder sb = new StringBuilder();
        for (var i = 0; i < 1000; i++) {
            sb.append(i);
            sb.append("\n");
        }
        codeArea.setText(sb.toString());

        var reportButton = new Button("Report & Clear");
        reportButton.setOnAction(e -> {
            Collections.sort(requestedIndexes);
            System.out.println("Created/Updated " + requestedIndexes.size() + " 
paragraphs: " + requestedIndexes);
            requestedIndexes.clear();
        });
        var topButton = new Button("To Top");
        topButton.setOnAction(e -> codeArea.select(new TextPos(0, 0, 0, true)));
        var bottomButton = new Button("To Bottom");
        bottomButton.setOnAction(e -> codeArea.select(new TextPos(999, 0, 0, 
true)));
        var eventButton = new Button("Fire Event");
        eventButton.setOnAction(e -> codeArea.getModel()
                .fireStyleChangeEvent(new TextPos(0, 0, 0, true), new 
TextPos(15, 0, 0, true)));
        HBox buttonBox = new HBox(reportButton, topButton, bottomButton, 
eventButton);

        VBox.setVgrow(codeArea, Priority.ALWAYS);
        VBox root = new VBox(codeArea, buttonBox);
        Scene scene = new Scene(root, 600, 200);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

Best regards, Pavel

Reply via email to