Java Swing Text Format findMatchingBracket(Element el, int offset)

Here you can find the source of findMatchingBracket(Element el, int offset)

Description

find Matching Bracket

License

Open Source License

Declaration

public static int findMatchingBracket(Element el, int offset) throws BadLocationException 

Method Source Code


//package com.java2s;
/*//from   w  w  w .ja  va  2 s . co  m
 * This file is part of the Scriba source distribution. This is free, open-source 
 * software. For full licensing information, please see the LicensingInformation file
 * at the root level of the distribution.
 *
 * Copyright (c) 2006-2007 Kobrix Software, Inc.
 */

import javax.swing.text.*;

public class Main {
    public static int findMatchingBracket(Element el, int offset) throws BadLocationException {
        Document doc = el.getDocument();
        if (doc.getLength() == 0 || el.getEndOffset() - el.getStartOffset() == 0)
            return -1;
        char c = doc.getText(offset, 1).charAt(0);
        char cprime; // c` - corresponding character
        boolean direction; // true = back, false = forward

        switch (c) {
        case '(':
            cprime = ')';
            direction = false;
            break;
        case ')':
            cprime = '(';
            direction = true;
            break;
        case '[':
            cprime = ']';
            direction = false;
            break;
        case ']':
            cprime = '[';
            direction = true;
            break;
        case '{':
            cprime = '}';
            direction = false;
            break;
        case '}':
            cprime = '{';
            direction = true;
            break;
        default:
            return -1;
        }

        int count;

        // How to merge these two cases is left as an exercise
        // for the reader.

        // Go back or forward
        if (direction) {
            // Count is 1 initially because we have already
            // `found' one closing bracket
            count = 1;

            // Get text[0,offset-1];
            String text = doc.getText(el.getStartOffset(), offset);

            // Scan backwards
            for (int i = offset - 1; i >= 0; i--) {
                // If text[i] == c, we have found another
                // closing bracket, therefore we will need
                // two opening brackets to complete the
                // match.
                char x = text.charAt(i);
                if (x == c)
                    count++;

                // If text[i] == cprime, we have found a
                // opening bracket, so we return i if
                // --count == 0
                else if (x == cprime) {
                    if (--count == 0)
                        return i;
                }
            }
        } else {
            // Count is 1 initially because we have already
            // `found' one opening bracket
            count = 1;

            // So we don't have to + 1 in every loop
            offset++;

            // Number of characters to check
            int len = el.getEndOffset() - offset;

            // Get text[offset+1,len];
            String text = doc.getText(offset, el.getEndOffset());

            // Scan forwards
            for (int i = 0; i < len; i++) {
                // If text[i] == c, we have found another
                // opening bracket, therefore we will need
                // two closing brackets to complete the
                // match.
                char x = text.charAt(i);

                if (x == c)
                    count++;

                // If text[i] == cprime, we have found an
                // closing bracket, so we return i if
                // --count == 0
                else if (x == cprime) {
                    if (--count == 0)
                        return i + offset;
                }
            }
        }

        // Nothing found
        return -1;
    }
}

Related

  1. createFormatterMask(final String mask)
  2. createIntegerFormatter(int minimum, int maximum)
  3. createMaskFormatter(final String s)
  4. dataFormatada(Calendar cal)
  5. findElementUp(String name, Element start)
  6. findNext(boolean reverse, int pos)
  7. formartCpf(String cpf)
  8. format(String pattern, Object value)
  9. formata(String s, String mascara)