Fix NpIntTextBox to prevent non-numeric characters
Handle both KeyPressEvent and KeyDownEvent. Browsers may fire KeyDownEvent when sending characters like '#', which the box does not want to accept. Change-Id: If605f8e472733a728be1cca5d27c0fef9ed6fae9
This commit is contained in:
		| @@ -15,7 +15,13 @@ | ||||
| package com.google.gerrit.client.ui; | ||||
|  | ||||
| import com.google.gwt.dom.client.Element; | ||||
| import com.google.gwt.dom.client.NativeEvent; | ||||
| import com.google.gwt.event.dom.client.DomEvent; | ||||
| import com.google.gwt.event.dom.client.KeyCodeEvent; | ||||
| import com.google.gwt.event.dom.client.KeyCodes; | ||||
| import com.google.gwt.event.dom.client.KeyDownEvent; | ||||
| import com.google.gwt.event.dom.client.KeyDownHandler; | ||||
| import com.google.gwt.event.dom.client.KeyEvent; | ||||
| import com.google.gwt.event.dom.client.KeyPressEvent; | ||||
| import com.google.gwt.event.dom.client.KeyPressHandler; | ||||
| import com.google.gwtexpui.globalkey.client.NpTextBox; | ||||
| @@ -34,13 +40,30 @@ public class NpIntTextBox extends NpTextBox { | ||||
|   } | ||||
|  | ||||
|   private void init() { | ||||
|     addKeyDownHandler(new KeyDownHandler() { | ||||
|       @Override | ||||
|       public void onKeyDown(KeyDownEvent event) { | ||||
|         int code = event.getNativeKeyCode(); | ||||
|         onKey(event, code, code); | ||||
|       } | ||||
|     }); | ||||
|     addKeyPressHandler(new KeyPressHandler() { | ||||
|       @Override | ||||
|       public void onKeyPress(KeyPressEvent event) { | ||||
|         char c = event.getCharCode(); | ||||
|         if (c < '0' || '9' < c) { | ||||
|           final int nativeCode = event.getNativeEvent().getKeyCode(); | ||||
|           switch (nativeCode) { | ||||
|         int charCode = event.getCharCode(); | ||||
|         int keyCode = event.getNativeEvent().getKeyCode(); | ||||
|         onKey(event, charCode, keyCode); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   private void onKey(KeyEvent<?> event, int charCode, int keyCode) { | ||||
|     if ('0' <= charCode && charCode <= '9') { | ||||
|       if (event.isAnyModifierKeyDown()) { | ||||
|         event.preventDefault(); | ||||
|       } | ||||
|     } else { | ||||
|       switch (keyCode) { | ||||
|         case KeyCodes.KEY_BACKSPACE: | ||||
|         case KeyCodes.KEY_LEFT: | ||||
|         case KeyCodes.KEY_RIGHT: | ||||
| @@ -51,15 +74,17 @@ public class NpIntTextBox extends NpTextBox { | ||||
|           break; | ||||
|  | ||||
|         default: | ||||
|               if (!event.isAnyModifierKeyDown()) { | ||||
|           // Allow copy and paste using ctl-c/ctrl-v, | ||||
|           // or whatever the platform's convention is. | ||||
|           if (!(event.isControlKeyDown() | ||||
|               || event.isMetaKeyDown() | ||||
|               || event.isAltKeyDown())) { | ||||
|             event.preventDefault(); | ||||
|           } | ||||
|           break; | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   public int getIntValue() { | ||||
|     String txt = getText().trim(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Shawn Pearce
					Shawn Pearce