E
E
Enflamer2019-08-09 21:05:50
Java
Enflamer, 2019-08-09 21:05:50

How to make a function for walking and highlighting words using the forward and backward buttons?

For the file manager, it is necessary to implement a function that will save words somewhere, after which the user will press the "Forward" and "Back" buttons to navigate through the found words, and the text box should display the highlighted word. I tried to write a function, but it doesn't work properly.
I will have a word counter, the buttons, when pressed, will either decrease or increase this counter, respectively.

private static int changeTextView(StyledText tv, String target, int selected) {
    if (selected < 0) {
        selected = 0;
    }
    String text = tv.getText().toString();
    int startIndex = 0, endIndex = 0;

    LinkedList<StyleRange> list = new LinkedList<StyleRange>();
    int currentIndex = 0;

    while (true) {
        startIndex = text.indexOf(target, endIndex);
        endIndex = startIndex + target.length();

        boolean isLast = text.indexOf(target, endIndex) < 0;
        if (startIndex < 0)
            break;

        if (currentIndex == selected || isLast && selected > currentIndex) {
            //list.add(setSelection(startSpan, endSpan));
            tv.setSelection(startIndex, endIndex);
            if (isLast && selected > currentIndex) {
                selected = currentIndex;
            }
        } else {
            tv.setSelection(startIndex, endIndex);
        }

        currentIndex++;
        //getHighlightStyle(startSpan, endSpan);
    }
    //tv.styles = (StyleRange[]) list.toArray(new StyleRange[list.size()]);

    if (currentIndex == 0) {
        // text not found
        return -1;
    } else {
        return selected;
    }
}

Button function
@Override
        public void handleEvent(Event event) {
            selected++;
            changeTextView(styledText, keyword, selected);
        }

Function for highlighting words
private static StyleRange getHighlightStyle(int startOffset, int length) {
    StyleRange styleRange = new StyleRange();
    styleRange.start = startOffset;
    styleRange.length = length;
    styleRange.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    return styleRange;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question