if (widget == null) {
return;
}
// trigger keyDown and keyPress
Event keyDownEvent = EventBuilder.create(Event.ONKEYDOWN).setKeyCode(keyCode).build();
Event keyPressEvent = EventBuilder.create(Event.ONKEYPRESS).setKeyCode(keyCode).build();
dispatchEventsInternal(widget, true, keyDownEvent, keyPressEvent);
// check if one on the events has been prevented
boolean keyDownEventPreventDefault = JavaScriptObjects.getBoolean(keyDownEvent,
JsoProperties.EVENT_PREVENTDEFAULT);
boolean keyPressEventPreventDefault = JavaScriptObjects.getBoolean(keyPressEvent,
JsoProperties.EVENT_PREVENTDEFAULT);
if (!keyDownEventPreventDefault && !keyPressEventPreventDefault) {
StringBuilder sb = new StringBuilder(widget.getText());
// remove selectionRange
int selectionStart = JavaScriptObjects.getInteger(widget.asWidget().getElement(),
JsoProperties.SELECTION_START);
int selectionEnd = JavaScriptObjects.getInteger(widget.asWidget().getElement(),
JsoProperties.SELECTION_END);
switch (keyCode) {
case KeyCodes.KEY_ALT:
case KeyCodes.KEY_CTRL:
case KeyCodes.KEY_DELETE:
case KeyCodes.KEY_DOWN:
case KeyCodes.KEY_END:
case KeyCodes.KEY_ENTER:
case KeyCodes.KEY_ESCAPE:
case KeyCodes.KEY_HOME:
case KeyCodes.KEY_LEFT:
case KeyCodes.KEY_PAGEDOWN:
case KeyCodes.KEY_PAGEUP:
case KeyCodes.KEY_RIGHT:
case KeyCodes.KEY_SHIFT:
case KeyCodes.KEY_UP:
// nothing to do
break;
case KeyCodes.KEY_TAB:
blur(widget);
break;
case KeyCodes.KEY_BACKSPACE:
if (selectionStart == selectionEnd) {
sb.deleteCharAt(selectionStart);
} else {
sb.replace(selectionStart, selectionEnd, "");
}
widget.setText(sb.toString());
break;
default:
sb.replace(selectionStart, selectionEnd, "");
sb.insert(selectionStart, (char) keyCode);
selectionStart = selectionEnd = selectionStart + 1;
widget.setText(sb.toString());
JavaScriptObjects.setProperty(widget.asWidget().getElement(),
JsoProperties.SELECTION_START, selectionStart);
JavaScriptObjects.setProperty(widget.asWidget().getElement(),
JsoProperties.SELECTION_END, selectionEnd);
}
// trigger keyUp
Event keyUpEvent = EventBuilder.create(Event.ONKEYUP).setKeyCode(keyCode).build();
dispatchEventsInternal(widget, true, keyUpEvent);
}
}