use of com.google.gwt.event.dom.client.KeyPressHandler in project kie-wb-common by kiegroup.
the class AbstractValidatingTextBox method setup.
protected void setup() {
final TextBox me = this;
// Validate value as it is entered
this.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(final KeyPressEvent event) {
// Permit navigation
int keyCode = getKeyCodeFromKeyPressEvent(event);
if (event.isControlKeyDown()) {
return;
}
if (!event.isShiftKeyDown()) {
if (keyCode == KeyCodes.KEY_BACKSPACE || keyCode == KeyCodes.KEY_DELETE || keyCode == KeyCodes.KEY_LEFT || keyCode == KeyCodes.KEY_RIGHT || keyCode == KeyCodes.KEY_ENTER || keyCode == KeyCodes.KEY_TAB || keyCode == KeyCodes.KEY_HOME || keyCode == KeyCodes.KEY_END) {
return;
}
}
// Get new value and validate
int charCode = event.getCharCode();
String oldValue = me.getValue();
String newValue = oldValue.substring(0, me.getCursorPos());
newValue = newValue + ((char) charCode);
newValue = newValue + oldValue.substring(me.getCursorPos() + me.getSelectionLength());
String validationError = isValidValue(newValue, false);
if (validationError != null) {
event.preventDefault();
fireValidationError(validationError);
}
}
});
// Add validation when loses focus (for when values are pasted in by user)
this.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(final BlurEvent event) {
String value = me.getText();
String validationError = isValidValue(value, true);
if (validationError != null) {
fireValidationError(validationError);
String validValue = makeValidValue(value);
me.setValue(validValue);
ValueChangeEvent.fire(AbstractValidatingTextBox.this, validValue);
}
}
});
}
use of com.google.gwt.event.dom.client.KeyPressHandler in project kie-wb-common by kiegroup.
the class AbstractRestrictedEntryTextBox method setup.
protected void setup() {
final TextBox me = this;
// Validate value as it is entered
this.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
// Permit navigation
int keyCode = event.getNativeEvent().getKeyCode();
if (event.isControlKeyDown() || keyCode == KeyCodes.KEY_BACKSPACE || keyCode == KeyCodes.KEY_DELETE || keyCode == KeyCodes.KEY_LEFT || keyCode == KeyCodes.KEY_RIGHT || keyCode == KeyCodes.KEY_TAB || keyCode == KeyCodes.KEY_HOME || keyCode == KeyCodes.KEY_END) {
return;
}
// Get new value and validate
int charCode = event.getCharCode();
String oldValue = me.getValue();
String newValue = oldValue.substring(0, me.getCursorPos());
newValue = newValue + ((char) charCode);
newValue = newValue + oldValue.substring(me.getCursorPos() + me.getSelectionLength());
if (!isValidValue(newValue, false)) {
event.preventDefault();
}
}
});
// Add validation when looses focus (for when values are pasted in by users')
this.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent event) {
final String value = me.getText();
if (!isValidValue(value, true)) {
final String validValue = makeValidValue(value);
me.setText(validValue);
ValueChangeEvent.fire(AbstractRestrictedEntryTextBox.this, validValue);
}
}
});
}
use of com.google.gwt.event.dom.client.KeyPressHandler in project kie-wb-common by kiegroup.
the class CustomDataTypeTextBoxTest method testKeyEnter.
@Test
public void testKeyEnter() {
when(textBox.getKeyCodeFromKeyPressEvent(any(KeyPressEvent.class))).thenReturn(KeyCodes.KEY_ENTER);
when(keyPressEvent.isControlKeyDown()).thenReturn(false);
when(keyPressEvent.isShiftKeyDown()).thenReturn(false);
when(keyPressEvent.getCharCode()).thenReturn((char) 13);
when(textBox.getCursorPos()).thenReturn(4);
when(textBox.getSelectionLength()).thenReturn(0);
when(textBox.getValue()).thenReturn("ab12");
when(textBox.getText()).thenReturn("ab12" + (char) 13);
textBox.setup();
verify(textBox, times(1)).addBlurHandler(blurCaptor.capture());
verify(textBox, times(1)).addKeyPressHandler(keyPressCaptor.capture());
BlurHandler blurHandler = blurCaptor.getValue();
blurHandler.onBlur(blurEvent);
verify(textBox, times(1)).isValidValue("ab12" + (char) 13, true);
verify(textBox, times(1)).makeValidValue("ab12" + (char) 13);
verify(textBox, times(1)).setValue("ab12");
KeyPressHandler keyPressHandler = keyPressCaptor.getValue();
keyPressHandler.onKeyPress(keyPressEvent);
}
use of com.google.gwt.event.dom.client.KeyPressHandler in project kie-wb-common by kiegroup.
the class VariableNameTextBoxTest method testSetup.
@Test
public void testSetup() {
when(textBox.getKeyCodeFromKeyPressEvent(any(KeyPressEvent.class))).thenReturn(64);
when(keyPressEvent.isControlKeyDown()).thenReturn(false);
when(keyPressEvent.isShiftKeyDown()).thenReturn(true);
when(keyPressEvent.getCharCode()).thenReturn('@');
when(textBox.getCursorPos()).thenReturn(4);
when(textBox.getSelectionLength()).thenReturn(0);
when(textBox.getValue()).thenReturn("ab12");
when(textBox.getText()).thenReturn("ab12@");
textBox.setup();
verify(textBox, times(1)).addBlurHandler(blurCaptor.capture());
verify(textBox, times(1)).addKeyPressHandler(keyPressCaptor.capture());
BlurHandler blurHandler = blurCaptor.getValue();
blurHandler.onBlur(blurEvent);
verify(textBox, times(1)).isValidValue("ab12@", true);
verify(textBox, times(1)).makeValidValue("ab12@");
verify(textBox, times(1)).setValue("ab12");
KeyPressHandler keyPressHandler = keyPressCaptor.getValue();
keyPressHandler.onKeyPress(keyPressEvent);
verify(keyPressEvent, times(1)).preventDefault();
verify(textBox, times(1)).isValidValue("ab12@", false);
verify(textBox, times(1)).fireValidationError(ERROR_REMOVED + ": @");
verify(textBox, times(1)).fireValidationError(ERROR_TYPED + ": @");
}
use of com.google.gwt.event.dom.client.KeyPressHandler in project kie-wb-common by kiegroup.
the class ScrollableLienzoPanelViewTest method testSetPresenter.
@Test
@SuppressWarnings("unchecked")
public void testSetPresenter() {
when(tested.isRemoteCommunicationEnabled()).thenReturn(true);
tested.setPresenter(presenter);
verify(presenter, times(3)).register(any(HandlerRegistration.class));
ArgumentCaptor<KeyDownHandler> downCaptor = ArgumentCaptor.forClass(KeyDownHandler.class);
verify(tested, times(1)).addKeyDownHandler(downCaptor.capture());
KeyDownHandler downHandler = downCaptor.getValue();
KeyDownEvent keyDownEvent = mock(KeyDownEvent.class);
when(keyDownEvent.getNativeKeyCode()).thenReturn(11);
downHandler.onKeyDown(keyDownEvent);
verify(presenter, times(1)).onKeyDown(eq(11));
ArgumentCaptor<KeyPressHandler> pressCaptor = ArgumentCaptor.forClass(KeyPressHandler.class);
verify(tested, times(1)).addKeyPressHandler(pressCaptor.capture());
KeyPressHandler pressHandler = pressCaptor.getValue();
KeyPressEvent kePressEvent = mock(KeyPressEvent.class);
when(kePressEvent.getUnicodeCharCode()).thenReturn(33);
pressHandler.onKeyPress(kePressEvent);
verify(presenter, times(1)).onKeyPress(eq(33));
ArgumentCaptor<KeyUpHandler> upCaptor = ArgumentCaptor.forClass(KeyUpHandler.class);
verify(tested, times(1)).addKeyUpHandler(upCaptor.capture());
KeyUpHandler upHandler = upCaptor.getValue();
KeyUpEvent keyUpEvent = mock(KeyUpEvent.class);
when(keyUpEvent.getNativeKeyCode()).thenReturn(55);
upHandler.onKeyUp(keyUpEvent);
verify(presenter, times(1)).onKeyUp(eq(55));
}
Aggregations