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)?
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:
But I can't seem to get it. How do you suggest I find an exact match (string+ style)?