/** * Update the view data based on the current value. * * @param parent the parent element * @param viewData the {@link ViewData} object to update * @param isEditing true if in edit mode * @return the new value */ private String updateViewData(Element parent, ViewData viewData, boolean isEditing) { TextAreaElement input = (TextAreaElement) parent.getFirstChild(); String value = input.getValue(); viewData.setText(value); viewData.setEditing(isEditing); return value; }
@Override public void onBrowserEvent( Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) { Object key = context.getKey(); ViewData viewData = getViewData(key); if (viewData != null && viewData.isEditing()) { // Handle the edit event. editEvent(context, parent, value, viewData, event, valueUpdater); } else { String type = event.getType(); int keyCode = event.getKeyCode(); boolean enterPressed = KEYUP.equals(type) && keyCode == KeyCodes.KEY_ENTER; if (CLICK.equals(type) || enterPressed) { // Go into edit mode. if (viewData == null) { viewData = new ViewData(value); setViewData(key, viewData); } else { viewData.setEditing(true); } edit(context, parent, value); } } }
private void editEvent( Context context, Element parent, String value, ViewData viewData, NativeEvent event, ValueUpdater<String> valueUpdater) { String type = event.getType(); boolean keyUp = KEYUP.equals(type); boolean keyDown = KEYDOWN.equals(type); if (keyUp || keyDown) { int keyCode = event.getKeyCode(); if (keyUp && keyCode == KeyCodes.KEY_ENTER && (event.getCtrlKey() || event.getAltKey() || event.getShiftKey())) { // Commit the change. commit(context, parent, viewData, valueUpdater); } else if (keyUp && keyCode == KeyCodes.KEY_ESCAPE) { // Cancel edit mode. String originalText = viewData.getOriginal(); if (viewData.isEditingAgain()) { viewData.setText(originalText); viewData.setEditing(false); } else { setViewData(context.getKey(), null); } cancel(context, parent, value); } else { // Update the text in the view data on each key. updateViewData(parent, viewData, true); } } else if (BLUR.equals(type)) { // Commit the change. Ensure that we are blurring the input element and // not the parent element itself. EventTarget eventTarget = event.getEventTarget(); if (Element.is(eventTarget)) { Element target = Element.as(eventTarget); if ("input".equals(target.getTagName().toLowerCase())) { commit(context, parent, viewData, valueUpdater); } } } }