Package de.t68.utils.keyboard

Source Code of de.t68.utils.keyboard.SpecialKey$SelectKeyboardLayout

/**
* Copyright (c) 2009, Till Woitendorf (t68)

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:<BR>

1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.<BR>
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.<BR>

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Contact me via http://jvkeyboard.sourceforge.net if you discover any bugs or if you have a suggestion
*/

package de.t68.utils.keyboard;

import de.t68.utils.keyboard.button.KeyboardButton;
import de.t68.utils.keyboard.button.ToggleKeyboardButton;
import de.t68.utils.keyboard.event.VirtualKeyListener;
import de.t68.utils.keyboard.icons.Backspace;
import de.t68.utils.keyboard.icons.Capslock;
import de.t68.utils.keyboard.icons.Enter;
import de.t68.utils.keyboard.icons.Left;
import de.t68.utils.keyboard.icons.Right;
import de.t68.utils.keyboard.icons.Shift;
import de.t68.utils.keyboard.icons.Tab;

import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.util.Locale;

import javax.swing.AbstractButton;
import javax.swing.JToggleButton;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;

import org.jdom2.Element;

public class SpecialKey extends KeyboardKey {

    public class SelectKeyboardLayout implements ItemListener {

        private final VKeyboard keyboard;

        public SelectKeyboardLayout(VKeyboard keyboard)
        {
            this.keyboard = keyboard;
        }

        public void itemStateChanged(ItemEvent e)
        {
            if (e.getStateChange() == ItemEvent.SELECTED)
                keyboard.selectKeyboardLayout(SpecialKey.this);
        }
    }

    public static final String DELETE = "delete";

    public static final String SPACE = "space";

    public static final String RIGHT = "right";

    public static final String LEFT = "left";

    public static final String ENTER = "enter";

    public static final String IME = "ime";

    public static final String SHIFT = "shift";

    public static final String ALTGR = "altgr";

    public static final String ALT = "alt";

    public static final String CAPSLOCK = "capslock";

    public static final String TAB = "tab";

    public static final String Attr_Special = "special";

    String textToShow;

    private final String key;

    public static final String BACKSPACE = "backspace";

    public String getKey()
    {
        return key;
    }

    public SpecialKey(String fontName, String key, String textToShow, int keyWidth)
    {
        super(fontName);
        if (key == null)
            throw new IllegalArgumentException("Parameter key is null!");
        this.key = key;
        this.textToShow = textToShow;
        this.keyWidth = keyWidth;
    }

    @Override
    public AbstractButton getComponent(VirtualKeyListener vkListener, final VKeyboard keyboard)
    {
        AbstractButton button;
        KeyboardButton kButton = null;
        ToggleKeyboardButton toggleButton = null;
        switch (key.toLowerCase())
        {
            case CAPSLOCK: // Caps Look
                toggleButton = new ToggleKeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                toggleButton.setIcon(new Capslock(32, 32));
                toggleButton.addVirtualKeyListener(vkListener);
                button = toggleButton;
                break;
            case ALT: // alt
                toggleButton = new ToggleKeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                toggleButton.setText(textToShow);
                toggleButton.addVirtualKeyListener(vkListener);
                button = toggleButton;
                if (keyboard.getAltModel() == null)
                    keyboard.setAltModel(button.getModel());
                else
                    button.setModel(keyboard.getAltModel());
                break;
            case ALTGR: // Alt-Gr
                toggleButton = new ToggleKeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                toggleButton.setText(textToShow);
                toggleButton.addVirtualKeyListener(vkListener);
                button = toggleButton;
                if (keyboard.getAltgrModel() == null)
                    keyboard.setAltgrModel(button.getModel());
                else
                    button.setModel(keyboard.getAltgrModel());
                break;
            case SHIFT: // left shift
                toggleButton = new ToggleKeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                toggleButton.setIcon(new Shift(32, 32));
                toggleButton.addVirtualKeyListener(vkListener);
                button = toggleButton;
                if (keyboard.getShiftModel() == null)
                    keyboard.setShiftModel(button.getModel());
                else
                    button.setModel(keyboard.getShiftModel());
                break;
            case IME: // only IME Mode
                JToggleButton imeButton = new JToggleButton();
                imeButton.setText(textToShow);
                imeButton.setFont(new Font(fontName, Font.PLAIN, keyFontSize));
                imeButton.addItemListener(new SelectKeyboardLayout(keyboard));
                button = imeButton;
                break;
            case BACKSPACE:
                kButton = new KeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                kButton.setIcon(new Backspace(32, 32));
                kButton.addVirtualKeyListener(vkListener);
                button = kButton;
                break;
            case ENTER: // Enter
                kButton = new KeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                kButton.setIcon(new Enter(32, 32));
                kButton.addVirtualKeyListener(vkListener);
                button = kButton;
                break;
            case LEFT: // left
                kButton = new KeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                kButton.setIcon(new Left(32, 32));
                kButton.addVirtualKeyListener(vkListener);
                button = kButton;
                break;
            case RIGHT: // right
                kButton = new KeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                kButton.setIcon(new Right(32, 32));
                kButton.addVirtualKeyListener(vkListener);
                button = kButton;
                break;
            case TAB:
                kButton = new KeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                kButton.setIcon(new Tab(32, 32));
                kButton.addVirtualKeyListener(vkListener);
                button = kButton;
                break;
            // All other special keys re
            default:
                kButton = new KeyboardButton(fontName, keyFontSize, getKeyCode(), getKeyChar());
                kButton.setText(textToShow);
                kButton.addVirtualKeyListener(vkListener);
                button = kButton;
                break;
        }
        return button;
    }

    @Override
    public void doAction(JTextComponent textField, VKeyboard keyboard, int eventID)
    {
        int pos = textField.getCaretPosition();
        switch (key.toLowerCase())
        {
            case CAPSLOCK:
                if (eventID == KeyEvent.KEY_PRESSED)
                    keyboard.setCapsLock(!keyboard.isCapsLock());
                else
                    // Released
                    keyboard.setCapsLock(false);
                break;
            case BACKSPACE:
                try
                {
                    if (pos > 0)
                        textField.getDocument().remove(pos - 1, 1);
                } catch (BadLocationException e1)
                {
                    e1.printStackTrace();
                }
                break;
            case ALT:
                if (eventID == KeyEvent.KEY_PRESSED)
                    keyboard.setModifier(KeyboardModifier.alt);
                else
                    keyboard.setModifier(KeyboardModifier.normal);
                break;
            case ALTGR:
                if (eventID == KeyEvent.KEY_PRESSED)
                    keyboard.setModifier(KeyboardModifier.altshift);
                else
                    keyboard.setModifier(KeyboardModifier.normal);
                break;
            case SHIFT:
                if (eventID == KeyEvent.KEY_PRESSED)
                    keyboard.setModifier(KeyboardModifier.shift);
                else
                    keyboard.setModifier(KeyboardModifier.normal);
                break;
            case SPACE:
                insertString(textField, keyboard, " ");
                break;
            case LEFT:
                if (pos > 0)
                    textField.setCaretPosition(pos - 1);
                break;
            case RIGHT:
                if (pos < textField.getText().length())
                    textField.setCaretPosition(pos + 1);
                break;
            case DELETE:
                try
                {
                    if (pos >= 0 && pos < textField.getText().length())
                        textField.getDocument().remove(pos, 1);
                } catch (BadLocationException e1)
                {
                    e1.printStackTrace();
                }
                break;
            default:
                try
                {
                    String str = new String(new char[] { getKeyChar() });
                    textField.getDocument().insertString(pos, str, null);
                } catch (BadLocationException e)
                {
                    e.printStackTrace();
                }
                break;
        }
    }

    /**
     * Creates a config XML element for this class.
     */
    @Override
    public Element getAsElement()
    {
        Element keyEl = new Element(Element_Key);
        keyEl.setAttribute(Attr_Special, this.key.toString());
        keyEl.setAttribute("length", String.valueOf(this.keyWidth));
        return keyEl;
    }

    /**
     * This is not applicable for {@link SpecialKey}.
     */
    @Override
    public void setKeyboardModifier(KeyboardModifier m)
    {
        // Not applicable, these keys are special or define the modifiers by themself.
    }

    /**
     * Returns the character for this sprecial key. Backspace, Enter, Space and Tab have a character, the
     * other return {@link KeyEvent#CHAR_UNDEFINED}.
     *
     * */
    public char getKeyChar()
    {
        switch (key.toLowerCase())
        {
            case BACKSPACE: // Backspace
                return '\b';
            case ENTER: // Enter
                return '\n';
            case SPACE:// Space
                return ' ';
            case TAB: // Tab
                return '\t';
                // All other special keys do not have an asigned character.
            default:
                return KeyEvent.CHAR_UNDEFINED;
        }
    }

    public int getKeyCode()
    {
        switch (key.toLowerCase())
        {
            case CAPSLOCK:
                return KeyEvent.VK_CAPS_LOCK;
            case BACKSPACE:
                return KeyEvent.VK_BACK_SPACE;
            case ALT:
                return KeyEvent.VK_ALT;
            case ENTER:
                return KeyEvent.VK_ENTER;
            case ALTGR:
                return KeyEvent.VK_ALT_GRAPH;
            case SHIFT:
                return KeyEvent.VK_SHIFT;
            case IME: // IME Modus
                return 0;
            case SPACE:
                return KeyEvent.VK_SPACE;
            case LEFT:
                return KeyEvent.VK_LEFT;
            case RIGHT:
                return KeyEvent.VK_RIGHT;
            case DELETE:
                return KeyEvent.VK_DELETE;
            case TAB:
                return KeyEvent.VK_TAB;
            default:
                return 0;
        }
    }

    /**
     * Returns the text for the key code. The language is derived from the system not from
     * {@link Locale#getDefault()}.
     *
     * @return returns a system dependend text for a key.
     */
    public String getStringKey()
    {
        switch (key.toLowerCase())
        {
            case CAPSLOCK:
                return KeyEvent.getKeyText(KeyEvent.VK_CAPS_LOCK);
            case BACKSPACE:
                return KeyEvent.getKeyText(KeyEvent.VK_BACK_SPACE);
            case ALT:
                return KeyEvent.getKeyText(KeyEvent.VK_ALT);
            case ENTER:
                return KeyEvent.getKeyText(KeyEvent.VK_ENTER);
            case SHIFT:
                return KeyEvent.getKeyText(KeyEvent.VK_SHIFT);
            case IME: // IME Modus
                return "IME";
            case SPACE:
                return KeyEvent.getKeyText(KeyEvent.VK_SPACE);
            case LEFT:
                return KeyEvent.getKeyText(KeyEvent.VK_LEFT);
            case RIGHT:
                return KeyEvent.getKeyText(KeyEvent.VK_RIGHT);
            case DELETE:
                return KeyEvent.getKeyText(KeyEvent.VK_DELETE);
            case TAB:
                return KeyEvent.getKeyText(KeyEvent.VK_TAB);
            default:
                return key;

        }
    }

    /**
     * Creates a special key for the given key name.
     *
     * @param fontName
     *            the font
     * @param key
     *            the key name
     * @return the special key
     */
    public static KeyboardKey getKeyboardKey(String fontName, String key)
    {
        if (key == null)
            throw new IllegalArgumentException("Parameter key is null!");
        switch (key.toLowerCase())
        {
            case CAPSLOCK:
                return new SpecialKey(fontName, key, "Caps Lock", 2);
            case BACKSPACE:
                return new SpecialKey(fontName, key, "Back Space", 2);
            case ALT:
                return new SpecialKey(fontName, key, "Alt", 2);
            case ENTER:
                return new SpecialKey(fontName, key, "Enter", 2);
            case ALTGR:
                return new SpecialKey(fontName, key, "Alt Gr", 2);
            case SHIFT:
                return new SpecialKey(fontName, key, "Shift", 2);
            case IME: // IME Modus
                return new SpecialKey(fontName, key, "IME", 2);
            case TAB:
                return new SpecialKey(fontName, key, "Tab", 2);
            case SPACE:
                return new SpecialKey(fontName, key, " ", 8);
            case LEFT:
                return new SpecialKey(fontName, key, "&#x2190;", 1);
            case RIGHT:
                return new SpecialKey(fontName, key, "&#x2192;", 1);
            case DELETE:
                return new SpecialKey(fontName, key, KeyEvent.getKeyText(KeyEvent.VK_DELETE), 1);
            default:
                return new SpecialKey(fontName, key, key, 1);
        }
    }
}
TOP

Related Classes of de.t68.utils.keyboard.SpecialKey$SelectKeyboardLayout

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.