Package de.t68.utils.keyboard

Source Code of de.t68.utils.keyboard.KeyboardKey

/**
* 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.event.VirtualKeyEvent;
import de.t68.utils.keyboard.event.VirtualKeyListener;

import java.awt.event.KeyEvent;

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

import org.jdom2.Element;

/**
* This class represents a key for our virtual keyboard. The key can manage four Strings as content:
* <ol>
* <li>normal (without any modifier): {@link #normalKey}</li>
* <li>shift: {@link #shiftKey}</li>
* <li>alt: {@link #altKey}</li>
* <li>alt+shift: {@link #altshiftKey}</li>
* </ol>
*
* @author t68
*
*/
public class KeyboardKey {

    /**
     *
     */
    public static final String Element_Key = "Key";

    /**
     *
     */
    public static final String Attr_Normal = "normal";

    /**
     *
     */
    public static final String Attr_Shift = "shift";

    /**
     *
     */
    public static final String Attr_Alt = "alt";

    /**
     *
     */
    public static final String Attr_AltGr = "altgr";

    protected String normalKey;

    protected String shiftKey;

    protected String altKey;

    protected String altshiftKey;

    protected String fontName;

    protected KeyboardButton keyboardButton;

    protected int keyFontSize = 18;

    protected int keyWidth;

    /**
     * @param fontName
     */
    protected KeyboardKey(String fontName)
    {// konstruktor f�r ableitungen
        super();
        this.fontName = fontName;
    }

    /**
     * @param fontName
     * @param keyEl
     */
    public KeyboardKey(String fontName, Element keyEl)
    {
        this(fontName);
        String normal = keyEl.getAttributeValue(Attr_Normal);
        String shift = keyEl.getAttributeValue(Attr_Shift);
        String alt = keyEl.getAttributeValue(Attr_Alt);
        String altshift = keyEl.getAttributeValue(Attr_AltGr);
        this.shiftKey = normal.equalsIgnoreCase(shift) ? shift.toUpperCase() : shift;
        this.normalKey = normal.equalsIgnoreCase(shift) ? normal.toLowerCase() : normal;
        this.altKey = alt;
        this.altshiftKey = altshift;
        keyWidth = 1;

    }

    public KeyboardKey(String fontName, String normalKey, String shiftKey, String altKey, String altshiftKey)
    {
        super();
        this.normalKey = normalKey;
        this.shiftKey = shiftKey;
        this.altKey = altKey;
        this.altshiftKey = altshiftKey;
        this.fontName = fontName;
        keyWidth = 1;
    }

    public KeyboardKey(String fontName, String normalKey, String shiftKey, String altKey, String altshiftKey,
            int keyWidth)
    {
        super();
        this.normalKey = normalKey;
        this.shiftKey = shiftKey;
        this.altKey = altKey;
        this.altshiftKey = altshiftKey;
        this.fontName = fontName;
        this.keyWidth = keyWidth;
    }

    /**
     * Retuirns the key content for the modifier.
     *
     * @param modifier
     *            the {@link KeyboardModifier}
     * @return the key content
     */
    private String getContent(KeyboardModifier modifier)
    {
        switch (modifier)
        {
            case alt:
                return altKey;
            case altshift:
                return altshiftKey;
            case normal:
                return normalKey;
            case shift:
                return shiftKey;
            default:
                break;
        }
        return normalKey;
    }

    /**
     * Creates a new {@link KeyboardButton}. This instance is set as member {@link #keyboardButton}.
     *
     * @param vkListener
     *            the {@link VirtualKeyListener}
     * @param keyboard
     *            the {@link VKeyboard}, manages some necessary data for the button creation
     * @return a new {@link KeyboardButton}
     */
    public AbstractButton getComponent(VirtualKeyListener vkListener, VKeyboard keyboard)
    {
        keyboardButton = new KeyboardButton(fontName, keyFontSize, normalKey, shiftKey, altKey, altshiftKey,
                VirtualKeyEvent.stringToKeyCode(normalKey), normalKey.toCharArray()[0]);
        keyboardButton.addVirtualKeyListener(vkListener);
        return keyboardButton;
    }

    /**
     * @return the keyFontSize
     */
    public int getKeyFontSize()
    {
        return keyFontSize;
    }

    /**
     * @return the keyWidth
     */
    public int getKeyWidth()
    {
        return keyWidth;
    }

    protected void insertString(JTextComponent textField, VKeyboard keyboard, String s)
    {
        try
        {
            int pos = textField.getCaretPosition();
            textField.getDocument().insertString(pos, s, null);
            keyboard.setModifier(keyboard.isCapsLock() ? KeyboardModifier.shift : KeyboardModifier.normal);
        } catch (BadLocationException e)
        {
            e.printStackTrace();
        }
    }

    public void doAction(JTextComponent textField, VKeyboard keyboard, int eventID)
    {
        if (eventID == KeyEvent.KEY_TYPED)
            insertString(textField, keyboard, getContent(keyboard.getModifier()));
    }

    public Element getAsElement()
    {
        Element key = new Element(Element_Key);
        if (normalKey != null)
            key.setAttribute(Attr_Normal, normalKey);
        if (shiftKey != null)
            key.setAttribute(Attr_Shift, shiftKey);
        if (altKey != null)
            key.setAttribute(Attr_Alt, altKey);
        if (altshiftKey != null)
            key.setAttribute(Attr_AltGr, altshiftKey);
        return key;
    }

    public void setKeyboardModifier(KeyboardModifier m)
    {
        if (keyboardButton != null)
            keyboardButton.setKeyboardModifier(m);
    }

    /**
     * @param keyFontSize
     *            the keyFontSize to set
     */
    public void setKeyFontSize(int keyFontSize)
    {
        this.keyFontSize = keyFontSize;
    }

    /**
     * @param keyWidth
     *            the keyWidth to set
     */
    public void setKeyWidth(int keyWidth)
    {
        this.keyWidth = keyWidth;
    }

}
TOP

Related Classes of de.t68.utils.keyboard.KeyboardKey

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.