<commit_before>/**
 * This file is part of DBNavigation.
 * 
 * RiPhone is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * 
 * RiPhone is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RiPhone. If not, see <http://www.gnu.org/licenses/>
 * 
 * 
 * Copyright (c) 2011-2012 <NAME>
 * 
 */
package de.knightsoft.dbnavigationbar.client.ui;

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.TextBox;

/**
 * The <code>NumericKeyPressHandler</code> class is KeyPress Handler
 * Class to limit input to numeric fields.
 * 
 * @author <NAME>
 * @version $Rev$, $Date$
 */
public class NumericKeyPressHandler implements KeyPressHandler
{

  @Override
  public final void onKeyPress(final KeyPressEvent event)
  {
    int keyCode = 0;
    if (event.getNativeEvent() != null)
    {
      keyCode = event.getNativeEvent().getKeyCode();
    }
    final char charCode = event.getCharCode();

    switch (keyCode)
    {
      case KeyCodes.KEY_BACKSPACE:
      case KeyCodes.KEY_DELETE:
      case KeyCodes.KEY_LEFT:
      case KeyCodes.KEY_RIGHT:
      case KeyCodes.KEY_SHIFT:
      case KeyCodes.KEY_TAB:
      case KeyCodes.KEY_ENTER:
      case KeyCodes.KEY_HOME:
      case KeyCodes.KEY_END:
      case KeyCodes.KEY_UP:
      case KeyCodes.KEY_DOWN:
        break;
      default:
        // Copy, Cut or Paste or numeric input
        if (!(event.isControlKeyDown()
            && (charCode == 'c'
            || charCode == 'x'
            || charCode == 'v'))
            && !Character.isDigit(charCode))
        {
          ((TextBox) event.getSource()).cancelKey();
        }
        break;
    }
  }
}<commit_msg>handler can now be also attached to all Input Fields, not only TextBox<commit_after>/**
 * This file is part of DBNavigation.
 * 
 * RiPhone is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * 
 * RiPhone is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RiPhone. If not, see <http://www.gnu.org/licenses/>
 * 
 * 
 * Copyright (c) 2011-2012 <NAME>
 * 
 */
package de.knightsoft.dbnavigationbar.client.ui;

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.ValueBoxBase;

/**
 * The <code>NumericKeyPressHandler</code> class is KeyPress Handler
 * Class to limit input to numeric fields.
 * 
 * @author <NAME>
 * @version $Rev$, $Date$
 */
public class NumericKeyPressHandler implements KeyPressHandler
{

  @Override
  public final void onKeyPress(final KeyPressEvent event)
  {
    int keyCode = 0;
    if (event.getNativeEvent() != null)
    {
      keyCode = event.getNativeEvent().getKeyCode();
    }
    final char charCode = event.getCharCode();

    switch (keyCode)
    {
      case KeyCodes.KEY_BACKSPACE:
      case KeyCodes.KEY_DELETE:
      case KeyCodes.KEY_LEFT:
      case KeyCodes.KEY_RIGHT:
      case KeyCodes.KEY_SHIFT:
      case KeyCodes.KEY_TAB:
      case KeyCodes.KEY_ENTER:
      case KeyCodes.KEY_HOME:
      case KeyCodes.KEY_END:
      case KeyCodes.KEY_UP:
      case KeyCodes.KEY_DOWN:
        break;
      default:
        // Copy, Cut or Paste or numeric input
        if (!(event.isControlKeyDown()
            && (charCode == 'c'
            || charCode == 'x'
            || charCode == 'v'))
            && !Character.isDigit(charCode))
        {
          ((ValueBoxBase<?>) event.getSource()).cancelKey();
        }
        break;
    }
  }
}
{'fraction_non_alphanumeric': 0.0994671403197158, 'fraction_numerical': 0.004440497335701598, 'mean_word_length': 2.8405797101449277, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6962470', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1340, 'n_words': 422}