Returns a mnemonic of a masked string. - Java Swing

Java examples for Swing:Key Event

Description

Returns a mnemonic of a masked string.

Demo Code


import java.awt.Component;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class Main{
    private static final Map<Character, Integer> MNEMONIC_OF_CHAR = new HashMap<>();
    /**/*from w w  w  .j  av a2s.  co  m*/
     * Returns a mnemonic of a masked string.
     *
     * The mask is the first ampersand (&amp;), the mnemonic is the character
     * behind the ampersand.
     *
     * <em>The valid range of mnemonic characters is [A-Za-z0-9]</em>.
     *
     * @param   string masked string or string without an ampersand
     * @return         {@code MnemonicIndexString#index} ist -1 if the
     *                 string does not contain an ampersand or the mnemonic
     *                 character is invalid. {@code MnemonicIndexString#string} is
     *                 the string without the mask, or the string itself if
     *                 {@code MnemonicIndexString#index} is -1.
     * @throws NullPointerException if <code>string</code> is null
     */
    static MnemonicIndexString getMnemonic(String string) {
        if (string == null) {
            throw new NullPointerException("string == null");
        }

        MnemonicIndexString noMnemonicIndexString = new MnemonicIndexString(
                -1, string);

        if (string.length() < 2) {
            return noMnemonicIndexString;
        }

        int strlen = string.length();
        int ampersandIndex = string.indexOf('&');

        if (ampersandIndex < 0) {
            return noMnemonicIndexString;
        }

        if ((strlen < 2) || (ampersandIndex < 0)
                || (ampersandIndex > strlen - 2)) {
            return noMnemonicIndexString;
        }

        char mnemonicChar = string
                .substring(ampersandIndex + 1, ampersandIndex + 2)
                .toUpperCase().charAt(0);
        boolean isInRange = MnemonicUtil.isInRange(mnemonicChar);

        assert isInRange : "Not in Range: " + mnemonicChar + " of "
                + string;

        if (isInRange) {
            int mnemonic = MnemonicUtil.getMnemonicOf(mnemonicChar);
            String titlePrefix = (ampersandIndex == 0) ? "" : string
                    .substring(0, ampersandIndex);
            String titlePostfix = (ampersandIndex == strlen - 1) ? ""
                    : string.substring(ampersandIndex + 1);

            return new MnemonicIndexString(mnemonic, titlePrefix
                    + titlePostfix);
        }

        return noMnemonicIndexString;
    }
    public static boolean isInRange(char c) {
        return MNEMONIC_OF_CHAR.containsKey(c);
    }
    /**
     * Returns a mnemonic (index) of a specific character.
     *
     * @param  c character in range [A-Z,0-9]
     * @return   mnemonic
     * @throws   IllegalArgumentException if <code>c</code> is not in range
     */
    public static int getMnemonicOf(char c) {
        if (!isInRange(c)) {
            throw new IllegalArgumentException(
                    "Character is not in Range: " + c);
        }

        return MNEMONIC_OF_CHAR.get(c);
    }
}

Related Tutorials