All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsolutions.client.gwt.widget.IntegerKeyPressHandler Maven / Gradle / Ivy

There is a newer version: 5.8.4
Show newest version
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *              C E D A R
 *          S O L U T I O N S       "Software done right."
 *           S O F T W A R E
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2013 Kenneth J. Pronovici.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0.
 * See LICENSE for more information about the licensing terms.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Author   : Kenneth J. Pronovici 
 * Language : Java 6
 * Project  : Common Java Functionality
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.cedarsolutions.client.gwt.widget;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.ValueBox;

/**
 * Key press handler that accepts only legal integer characters.
 * @see StackOverflow
 * @see StackOverflow
 * @author Kenneth J. Pronovici , after a Stack Overflow example
 */
public class IntegerKeyPressHandler implements KeyPressHandler {

    @Override
    @SuppressWarnings("rawtypes")
    public void onKeyPress(KeyPressEvent event) {
        switch (event.getNativeEvent().getKeyCode()) {
        case KeyCodes.KEY_TAB:
        case KeyCodes.KEY_BACKSPACE:
        case KeyCodes.KEY_DELETE:
        case KeyCodes.KEY_LEFT:
        case KeyCodes.KEY_RIGHT:
        case KeyCodes.KEY_UP:
        case KeyCodes.KEY_DOWN:
        case KeyCodes.KEY_END:
        case KeyCodes.KEY_ENTER:
        case KeyCodes.KEY_ESCAPE:
        case KeyCodes.KEY_PAGEDOWN:
        case KeyCodes.KEY_PAGEUP:
        case KeyCodes.KEY_HOME:
        case KeyCodes.KEY_SHIFT:
        case KeyCodes.KEY_ALT:
        case KeyCodes.KEY_CTRL:
            break;
        default:
            if (event.isAltKeyDown() ||
                    (event.isControlKeyDown() &&
                            (event.getCharCode() != 'v' && event.getCharCode() != 'V'))) {
                break;
            }
            if (!Character.isDigit(event.getCharCode())) {
                ((ValueBox) event.getSource()).cancelKey();
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy