Skip to content

Enhancement: Style Match #49

@ghost

Description

Hi Tomas, I am trying to implement a search for similar text.
I can find similar strings, but I am trying to find a way to match the style. I am doing ths:

public static ArrayList<IndexRange> getIndexRangesOfStyledDocInTextArea(StyledDocument styledDocToFind, InlineCssTextArea richTextArea) {
    ArrayList<IndexRange> ranges = new ArrayList<>();

    String textBody = richTextArea.getText();

    int start = 0;
    while (true) {
        int found = textBody.indexOf(styledDocToFind.getText(), start);
        if (found != -1) {
            // Found a similar string, now lets match the style -- do whatever here

            IndexRange range = new IndexRange(found, found + styledDocToFind.length());

            StyledDocument foundDoc = richTextArea.getDocument().subSequence(range);
            StyleSpans foundSpans = foundDoc.getStyleSpans(0);
            StyleSpans givenSpans = styledDocToFind.getStyleSpans(0);

            int foundSpanCount = foundSpans.getSpanCount();
            int givenSpanCount = givenSpans.getSpanCount();

            boolean match = true;//assume we found it
            for (int i = 0; i < foundSpanCount; i++) {
                if (foundSpans.getStyleSpan(i).getStyle() != givenSpans.getStyleSpan(i).getStyle()) {
                    match = false;
                    break;
                }
            }

            //if assumptions hold true then..
            if (match) {
                ranges.add(range);
            }

        }
        if (found == -1) {
            break;
        }
        start = found + 2;  // move start up for next iteration
    }

    return ranges;
}

But I can't seem to get it. How do you suggest I find an exact match (string+ style)?

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