Package net.helipilot50.stocktrade.displayproject.actions

Source Code of net.helipilot50.stocktrade.displayproject.actions.ExitOnTab

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

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

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o 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.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "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 COPYRIGHT OWNER 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.


*/
package net.helipilot50.stocktrade.displayproject.actions;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.KeyStroke;
import javax.swing.text.JTextComponent;

import net.helipilot50.stocktrade.framework.ForteKeyboardFocusManager;


/**
* Determines whether the passed jTextComponet tabs to the next control upon
* receiving the TAB keystroke, or if it uses this as part of it's input.
*
* @param pTextArea
*            The text area on which to effect the change
* @param pDoExit
*            True if the field should change the focus to the next control
*            on receiving a TAB, or false if it should consume the tab
*            itself and treat it as the tab character
*/
public class ExitOnTab extends PendingAction {
    private boolean exitOnTab;
    private final static String KEY_ADAPTER_PROPERTY = "qq_TabKeyAdapter";


    public ExitOnTab(JTextComponent pComponent, boolean pExitOnTab) {
        super(pComponent);
        this.exitOnTab = pExitOnTab;
    }

    @Override
    public void performAction() {
        final JTextComponent textArea = (JTextComponent)this.getComponent();
        KeyAdapter existingKeyAdapter = (KeyAdapter)textArea.getClientProperty(KEY_ADAPTER_PROPERTY);
       
        if (exitOnTab) {
            // Only do this if we're not already doing it, ie we don't have a key adapter set
            if (existingKeyAdapter == null) {
                textArea.getInputMap().put(KeyStroke.getKeyStroke("TAB"), "cycle-forwards");
                KeyAdapter newKeyAdapter = new KeyAdapter() {
                    public void keyPressed(KeyEvent event) {
                        if (event.getKeyCode() == KeyEvent.VK_TAB && event.getModifiersEx() == 0) {
                            ForteKeyboardFocusManager.setTabTraversal(true);
                            textArea.transferFocus();
                        }
                    }
                };
                textArea.addKeyListener(newKeyAdapter);
                textArea.putClientProperty(KEY_ADAPTER_PROPERTY, newKeyAdapter);
            }
        }
        else {
            // we need to remove the existing key listener if there is one
            if (existingKeyAdapter != null) {
                textArea.removeKeyListener(existingKeyAdapter);
                textArea.getInputMap().put(KeyStroke.getKeyStroke("TAB"), "insert-tab");
                textArea.putClientProperty(KEY_ADAPTER_PROPERTY, null);
            }
        }

    }

    /**
     * This method sets the behaviour of the text component to either accept tabs
     * or use the tabs to move the focus to the next widget.
     */
    public static void set(JTextComponent comp, boolean value) {
        ActionMgr.addAction(new ExitOnTab(comp, value));
    }

    public static boolean get(JTextComponent comp){
        ExitOnTab action = ActionMgr.getAction(comp, ExitOnTab.class);
        if (action != null) {
            return action.exitOnTab;
        }
        else {
            KeyAdapter existingKeyAdapter = (KeyAdapter)comp.getClientProperty(KEY_ADAPTER_PROPERTY);
            return existingKeyAdapter != null;
        }
    }
}
TOP

Related Classes of net.helipilot50.stocktrade.displayproject.actions.ExitOnTab

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.