Example usage for javax.swing.text ChangedCharSetException keyEqualsCharSet

List of usage examples for javax.swing.text ChangedCharSetException keyEqualsCharSet

Introduction

In this page you can find the example usage for javax.swing.text ChangedCharSetException keyEqualsCharSet.

Prototype

public boolean keyEqualsCharSet() 

Source Link

Document

Returns the char set key.

Usage

From source file:EditorPaneExample16.java

protected String getNewCharSet(ChangedCharSetException e) {
    String spec = e.getCharSetSpec();
    if (e.keyEqualsCharSet()) {
        // The event contains the new CharSet
        return spec;
    }/*from  w  ww . j  a  va  2 s  . com*/

    // The event contains the content type
    // plus ";" plus qualifiers which may
    // contain a "charset" directive. First
    // remove the content type.
    int index = spec.indexOf(";");
    if (index != -1) {
        spec = spec.substring(index + 1);
    }

    // Force the string to lower case
    spec = spec.toLowerCase();

    StringTokenizer st = new StringTokenizer(spec, " \t=", true);
    boolean foundCharSet = false;
    boolean foundEquals = false;
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (token.equals(" ") || token.equals("\t")) {
            continue;
        }
        if (foundCharSet == false && foundEquals == false && token.equals("charset")) {
            foundCharSet = true;
            continue;
        } else if (foundEquals == false && token.equals("=")) {
            foundEquals = true;
            continue;
        } else if (foundEquals == true && foundCharSet == true) {
            return token;
        }

        // Not recognized
        foundCharSet = false;
        foundEquals = false;
    }

    // No charset found - return a guess
    return "8859_1";
}