Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Style Match #49

Closed
ghost opened this issue Apr 28, 2014 · 6 comments
Closed

Enhancement: Style Match #49

ghost opened this issue Apr 28, 2014 · 6 comments

Comments

@ghost
Copy link

ghost commented Apr 28, 2014

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)?

@TomasMikula
Copy link
Member

Hi Maher,

the problems I see with your code:

  1. you are not checking whether foundSpanCount == givenSpanCount
  2. you are not comparing the lengths of individual spans (whether foundSpans.getStyleSpan(i).getLength() == givenSpans.getStyleSpan(i).getLength())
  3. you should use equals instead of == to compare style objects, i.e. !foundSpans.getStyleSpan(i).getStyle().equals(givenSpans.getStyleSpan(i).getStyle()) instead of foundSpans.getStyleSpan(i).getStyle() != givenSpans.getStyleSpan(i).getStyle()

@TomasMikula
Copy link
Member

I just implemented equals on StyleSpans, so now you can use

boolean match = foundSpans.equals(givenSpans);

@ghost
Copy link
Author

ghost commented Apr 28, 2014

I knew you were going to do it :)
Thanks!

@ghost
Copy link
Author

ghost commented Apr 28, 2014

download URL seems dead

On Mon, Apr 28, 2014 at 2:38 PM, TomasMikula notifications@github.comwrote:

I just implemented equals on StyleSpans, so now you can use

boolean match = foundSpans.equals(givenSpans);


Reply to this email directly or view it on GitHubhttps://github.com//issues/49#issuecomment-41616985
.

@TomasMikula
Copy link
Member

fixed now

@ghost
Copy link
Author

ghost commented Apr 28, 2014

got it working! Thanks :)

On Mon, Apr 28, 2014 at 2:48 PM, TomasMikula notifications@github.comwrote:

fixed now


Reply to this email directly or view it on GitHubhttps://github.com//issues/49#issuecomment-41617987
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant