Skip to content

Q: How to apply multiple styles at the same time ? #48

@Scoty

Description

@Scoty

Hello,
I am trying to make a simple XML editor. This is the part of the code where I am trying to style the main CodeArea of the app, to have code highlight:

public static void setCurCodeArea(CodeArea newCodeArea) {

        newCodeArea.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldText, String newText) {

                StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();

                //make the "<" and ">" blue
                Pattern arrows = Pattern.compile("\\<|\\>");
                addSpanStyle(spansBuilder, newText, arrows, "arrow");

                //make text between <>  brown
                Pattern tags = Pattern.compile("\\<(.*?)\\>");
                addSpanStyle(spansBuilder, newText, tags, "tag");

                newCodeArea.setStyleSpans(0, spansBuilder.create());
            }
        });

        curCodeArea = newCodeArea;
    }

    private static void addSpanStyle(StyleSpansBuilder<Collection<String>> spansBuilder, String newText, Pattern pattern, String spanClass) {
        Matcher matcher = pattern.matcher(newText);
        int lastKwEnd = 0;
        while (matcher.find()) {
            spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
            spansBuilder.add(Collections.singleton(spanClass), matcher.end() - matcher.start());
            lastKwEnd = matcher.end();
        }
        spansBuilder.add(Collections.emptyList(), newText.length() - lastKwEnd);
    }

And this is part of the exception StackTrace.

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -2968
    at java.lang.String.substring(String.java:1918)
    at org.fxmisc.richtext.StyledText.subSequence(StyledText.java:59)
    at org.fxmisc.richtext.Paragraph.subSequence(Paragraph.java:153)
    at org.fxmisc.richtext.Paragraph.restyle(Paragraph.java:180)
    at org.fxmisc.richtext.EditableStyledDocument.setStyleSpans(EditableStyledDocument.java:281)
    at org.fxmisc.richtext.StyledTextArea.setStyleSpans(StyledTextArea.java:444)
    at xmltool.UIBean$1.changed(UIBean.java:37)
    at xmltool.UIBean$1.changed(UIBean.java:23)
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:176)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at org.reactfx.inhibeans.binding.ObjectBinding.release(ObjectBinding.java:45)
    at org.reactfx.inhibeans.binding.ObjectBinding$$Lambda$69/1971275683.close(Unknown Source)
    at org.reactfx.Hold.lambda$multi$34(Hold.java:23)
    at org.reactfx.Hold$$Lambda$75/1047103664.close(Unknown Source)
    at org.fxmisc.richtext.StyledTextArea.replaceText(StyledTextArea.java:496)
    at org.fxmisc.richtext.EditActions.replaceText(EditActions.java:127)
    at xmltool.MainController.fileOpenByString(MainController.java:126)
    at xmltool.utils.UIHelperUtil.loadOpenFiles(UIHelperUtil.java:130)
    at xmltool.utils.UIHelperUtil.restoreState(UIHelperUtil.java:118)
    at xmltool.Main.initialization(Main.java:93)
    at xmltool.Main.start(Main.java:85)
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    ... 1 more

I guess I am not applying the multiple styles in a proper way...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions