Java Swing Text Format findNext(boolean reverse, int pos)

Here you can find the source of findNext(boolean reverse, int pos)

Description

Find and select the next searchable matching text.

License

Apache License

Parameter

Parameter Description
reverse look forwards or backwards
pos the starting index to start finding from

Return

the location of the next selected, or -1 if not found

Declaration

private static int findNext(boolean reverse, int pos) 

Method Source Code

//package com.java2s;
/*/*from www  .  jav  a 2s.c  om*/
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

import javax.swing.JCheckBox;
import javax.swing.JComboBox;

import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.Segment;

public class Main {
    private static final JComboBox FIND_FIELD = new JComboBox();
    private static final JCheckBox MATCH_CASE_CHECKBOX = new JCheckBox("Match Case      ");
    private static final JCheckBox IS_BACKWARDS_CHECKBOX = new JCheckBox("Search Backwards");
    private static final JCheckBox WRAP_SEARCH_CHECKBOX = new JCheckBox("Wrap Search     ");
    private static JTextComponent textComponent;
    private static final Segment SEGMENT = new Segment();

    /**
     * Find and select the next searchable matching text.
     *
     * @param reverse look forwards or backwards
     * @param pos     the starting index to start finding from
     * @return the location of the next selected, or -1 if not found
     */
    private static int findNext(boolean reverse, int pos) {
        boolean backwards = IS_BACKWARDS_CHECKBOX.isSelected();
        backwards = backwards ? !reverse : reverse;

        String pattern = (String) FIND_FIELD.getSelectedItem();
        if (pattern != null && pattern.length() > 0) {
            try {
                Document doc = textComponent.getDocument();
                doc.getText(0, doc.getLength(), SEGMENT);
            } catch (Exception e) {
                // should NEVER reach here
                e.printStackTrace();
            }

            pos += textComponent.getSelectedText() == null ? (backwards ? -1 : 1) : 0;

            char first = backwards ? pattern.charAt(pattern.length() - 1) : pattern.charAt(0);
            char oppFirst = Character.isUpperCase(first) ? Character.toLowerCase(first)
                    : Character.toUpperCase(first);
            int start = pos;
            boolean wrapped = WRAP_SEARCH_CHECKBOX.isSelected();
            int end = backwards ? 0 : SEGMENT.getEndIndex();
            pos += backwards ? -1 : 1;

            int length = textComponent.getDocument().getLength();
            if (pos > length) {
                pos = wrapped ? 0 : length;
            }

            boolean found = false;
            while (!found && (backwards ? pos > end : pos < end)) {
                found = !MATCH_CASE_CHECKBOX.isSelected() && SEGMENT.array[pos] == oppFirst;
                found = found ? found : SEGMENT.array[pos] == first;

                if (found) {
                    pos += backwards ? -(pattern.length() - 1) : 0;
                    for (int i = 0; found && i < pattern.length(); i++) {
                        char c = pattern.charAt(i);
                        found = SEGMENT.array[pos + i] == c;
                        if (!MATCH_CASE_CHECKBOX.isSelected() && !found) {
                            c = Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c);
                            found = SEGMENT.array[pos + i] == c;
                        }
                    }
                }

                if (!found) {
                    pos += backwards ? -1 : 1;

                    if (pos == end && wrapped) {
                        pos = backwards ? SEGMENT.getEndIndex() : 0;
                        end = start;
                        wrapped = false;
                    }
                }
            }
            pos = found ? pos : -1;
        }

        return pos;
    }
}

Related

  1. createIntegerFormatter(int minimum, int maximum)
  2. createMaskFormatter(final String s)
  3. dataFormatada(Calendar cal)
  4. findElementUp(String name, Element start)
  5. findMatchingBracket(Element el, int offset)
  6. formartCpf(String cpf)
  7. format(String pattern, Object value)
  8. formata(String s, String mascara)
  9. formatAreaImperial(BigDecimal areaDec)