Search in sources :

Example 16 with KeyPressHandler

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);
            }
        }
    });
}
Also used : BlurHandler(com.google.gwt.event.dom.client.BlurHandler) KeyPressHandler(com.google.gwt.event.dom.client.KeyPressHandler) KeyPressEvent(com.google.gwt.event.dom.client.KeyPressEvent) TextBox(org.gwtbootstrap3.client.ui.TextBox) BlurEvent(com.google.gwt.event.dom.client.BlurEvent)

Example 17 with KeyPressHandler

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);
            }
        }
    });
}
Also used : BlurHandler(com.google.gwt.event.dom.client.BlurHandler) KeyPressHandler(com.google.gwt.event.dom.client.KeyPressHandler) KeyPressEvent(com.google.gwt.event.dom.client.KeyPressEvent) TextBox(org.gwtbootstrap3.client.ui.TextBox) BlurEvent(com.google.gwt.event.dom.client.BlurEvent)

Example 18 with KeyPressHandler

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);
}
Also used : BlurHandler(com.google.gwt.event.dom.client.BlurHandler) KeyPressHandler(com.google.gwt.event.dom.client.KeyPressHandler) KeyPressEvent(com.google.gwt.event.dom.client.KeyPressEvent) Test(org.junit.Test)

Example 19 with KeyPressHandler

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 + ": @");
}
Also used : BlurHandler(com.google.gwt.event.dom.client.BlurHandler) KeyPressHandler(com.google.gwt.event.dom.client.KeyPressHandler) KeyPressEvent(com.google.gwt.event.dom.client.KeyPressEvent) Test(org.junit.Test)

Example 20 with KeyPressHandler

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));
}
Also used : HandlerRegistration(com.google.gwt.event.shared.HandlerRegistration) KeyPressHandler(com.google.gwt.event.dom.client.KeyPressHandler) KeyDownEvent(com.google.gwt.event.dom.client.KeyDownEvent) KeyPressEvent(com.google.gwt.event.dom.client.KeyPressEvent) KeyDownHandler(com.google.gwt.event.dom.client.KeyDownHandler) KeyUpEvent(com.google.gwt.event.dom.client.KeyUpEvent) KeyUpHandler(com.google.gwt.event.dom.client.KeyUpHandler) Test(org.junit.Test)

Aggregations

KeyPressEvent (com.google.gwt.event.dom.client.KeyPressEvent)20 KeyPressHandler (com.google.gwt.event.dom.client.KeyPressHandler)20 BlurHandler (com.google.gwt.event.dom.client.BlurHandler)9 Test (org.junit.Test)7 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)5 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)4 KeyUpEvent (com.google.gwt.event.dom.client.KeyUpEvent)4 KeyUpHandler (com.google.gwt.event.dom.client.KeyUpHandler)4 Button (com.google.gwt.user.client.ui.Button)4 HintTextBox (com.google.gerrit.client.ui.HintTextBox)3 BlurEvent (com.google.gwt.event.dom.client.BlurEvent)3 KeyDownEvent (com.google.gwt.event.dom.client.KeyDownEvent)3 KeyDownHandler (com.google.gwt.event.dom.client.KeyDownHandler)3 HorizontalPanel (com.google.gwt.user.client.ui.HorizontalPanel)3 TextBox (org.gwtbootstrap3.client.ui.TextBox)3 OnEditEnabler (com.google.gerrit.client.ui.OnEditEnabler)2 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)2 Event (com.google.gwt.user.client.Event)2 FlowPanel (com.google.gwt.user.client.ui.FlowPanel)2 Grid (com.google.gwt.user.client.ui.Grid)2