diff --git a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTFocusWidget.java b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTFocusWidget.java index 8fcf4af14..8632ad495 100644 --- a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTFocusWidget.java +++ b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTFocusWidget.java @@ -23,20 +23,26 @@ package com.ponysdk.core.terminal.ui; +import java.util.function.Consumer; + +import com.google.gwt.event.dom.client.DomEvent; +import com.google.gwt.event.dom.client.KeyEvent; import com.google.gwt.event.dom.client.MouseEvent; +import com.google.gwt.json.client.JSONArray; import com.google.gwt.user.client.ui.FocusWidget; import com.ponysdk.core.model.DomHandlerType; import com.ponysdk.core.model.ServerToClientModel; import com.ponysdk.core.terminal.PonySDK; import com.ponysdk.core.terminal.UIBuilder; +import com.ponysdk.core.terminal.instruction.PTInstruction; import com.ponysdk.core.terminal.model.BinaryModel; import com.ponysdk.core.terminal.model.ReaderBuffer; public abstract class PTFocusWidget extends PTWidget { + protected boolean enabled = true; private boolean showLoadingOnRequest = false; private boolean enabledOnRequest = false; - private boolean enabled = true; @Override public void create(final ReaderBuffer buffer, final int objectId, final UIBuilder uiBuilder) { @@ -67,10 +73,22 @@ public boolean update(final ReaderBuffer buffer, final BinaryModel binaryModel) } @Override - protected void triggerMouseEvent(final DomHandlerType domHandlerType, final MouseEvent event) { + protected void triggerDomEvent(final DomHandlerType domHandlerType, final DomEvent event, + final Consumer enricher) { + if (enabled) super.triggerDomEvent(domHandlerType, event, enricher); + } + + @Override + protected void triggerMouseClickEvent(final DomHandlerType domHandlerType, final MouseEvent event) { + if (!enabled) return; if (!enabledOnRequest) uiObject.setEnabled(false); if (showLoadingOnRequest) uiObject.addStyleName("pony-Loading"); super.triggerMouseEvent(domHandlerType, event); } + @Override + protected void triggerKeyEvent(final DomHandlerType domHandlerType, final KeyEvent event, final JSONArray keyFilter) { + if (enabled) super.triggerKeyEvent(domHandlerType, event, keyFilter); + } + } diff --git a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTPasswordTextBox.java b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTPasswordTextBox.java index 25ba3fa32..54c5c71eb 100644 --- a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTPasswordTextBox.java +++ b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTPasswordTextBox.java @@ -42,8 +42,8 @@ public int getTabIndex() { @Override public void onBrowserEvent(final Event event) { super.onBrowserEvent(event); - if (Event.ONPASTE == event.getTypeInt()) { - if (handlePasteEnabled) Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event)); + if (handlePasteEnabled && Event.ONPASTE == event.getTypeInt() && enabled) { + Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event)); } } }; diff --git a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextArea.java b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextArea.java index 736ca3a80..162de2f07 100644 --- a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextArea.java +++ b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextArea.java @@ -45,8 +45,8 @@ public int getTabIndex() { @Override public void onBrowserEvent(final Event event) { super.onBrowserEvent(event); - if (Event.ONPASTE == event.getTypeInt()) { - if (handlePasteEnabled) Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event)); + if (handlePasteEnabled && Event.ONPASTE == event.getTypeInt() && enabled) { + Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event)); } } }; diff --git a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextBox.java b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextBox.java index 11a1c468d..ea9e8e169 100644 --- a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextBox.java +++ b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextBox.java @@ -54,7 +54,7 @@ public int getTabIndex() { @Override public void onBrowserEvent(final Event event) { super.onBrowserEvent(event); - if (Event.ONPASTE == event.getTypeInt()) { + if (Event.ONPASTE == event.getTypeInt() && enabled) { filterText(); if (handlePasteEnabled) Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event)); } diff --git a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextBoxBase.java b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextBoxBase.java index 129d4eba0..52b5203e3 100644 --- a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextBoxBase.java +++ b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTTextBoxBase.java @@ -25,6 +25,7 @@ import java.util.Objects; +import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.json.client.JSONArray; import com.google.gwt.json.client.JSONNumber; import com.google.gwt.user.client.ui.TextBoxBase; @@ -64,42 +65,42 @@ public boolean update(final ReaderBuffer buffer, final BinaryModel binaryModel) } @Override - protected void triggerKeyUpEvent(final DomHandlerType domHandlerType, final JSONArray keyUpFilter) { - uiObject.addKeyUpHandler(event -> { - if (keyUpFilter != null) { - for (int i = 0; i < keyUpFilter.size(); i++) { - final JSONNumber keyCode = keyUpFilter.get(i).isNumber(); - if (keyCode.doubleValue() == event.getNativeKeyCode()) { - final String newValue = uiObject.getText(); - if (!Objects.equals(newValue, this.lastValue)) { - this.lastValue = newValue; - final PTInstruction changeHandlerInstruction = new PTInstruction(getObjectID()); - changeHandlerInstruction.put(ClientToServerModel.HANDLER_STRING_VALUE_CHANGE, this.lastValue); - uiBuilder.sendDataToServer(changeHandlerInstruction); - } + protected void triggerKeyUpEvent(final DomHandlerType domHandlerType, final KeyUpEvent event, final JSONArray keyFilter) { + if (!enabled) return; + final int nativeKeyCode = event.getNativeKeyCode(); + if (keyFilter != null) { + for (int i = 0; i < keyFilter.size(); i++) { + final JSONNumber keyCode = keyFilter.get(i).isNumber(); + if (keyCode.doubleValue() == nativeKeyCode) { + final String newValue = uiObject.getText(); + if (!Objects.equals(newValue, this.lastValue)) { + this.lastValue = newValue; + final PTInstruction changeHandlerInstruction = new PTInstruction(getObjectID()); + changeHandlerInstruction.put(ClientToServerModel.HANDLER_STRING_VALUE_CHANGE, this.lastValue); + uiBuilder.sendDataToServer(changeHandlerInstruction); + } - final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); - eventInstruction.put(ClientToServerModel.VALUE_KEY, event.getNativeKeyCode()); - uiBuilder.sendDataToServer(eventInstruction); + final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); + eventInstruction.put(ClientToServerModel.VALUE_KEY, nativeKeyCode); + uiBuilder.sendDataToServer(eventInstruction); - break; - } - } - } else { - final String newValue = uiObject.getText(); - if (!Objects.equals(newValue, this.lastValue)) { - this.lastValue = newValue; - final PTInstruction changeHandlerInstruction = new PTInstruction(getObjectID()); - changeHandlerInstruction.put(ClientToServerModel.HANDLER_STRING_VALUE_CHANGE, this.lastValue); - uiBuilder.sendDataToServer(changeHandlerInstruction); + break; } - - final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); - eventInstruction.put(ClientToServerModel.VALUE_KEY, event.getNativeKeyCode()); - uiBuilder.sendDataToServer(eventInstruction); } - preventOrStopEvent(event); - }); + } else { + final String newValue = uiObject.getText(); + if (!Objects.equals(newValue, this.lastValue)) { + this.lastValue = newValue; + final PTInstruction changeHandlerInstruction = new PTInstruction(getObjectID()); + changeHandlerInstruction.put(ClientToServerModel.HANDLER_STRING_VALUE_CHANGE, this.lastValue); + uiBuilder.sendDataToServer(changeHandlerInstruction); + } + + final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); + eventInstruction.put(ClientToServerModel.VALUE_KEY, nativeKeyCode); + uiBuilder.sendDataToServer(eventInstruction); + } + preventOrStopEvent(event); } } diff --git a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTWidget.java b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTWidget.java index ee47b22f6..68715b23a 100644 --- a/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTWidget.java +++ b/ponysdk/src/main/java/com/ponysdk/core/terminal/ui/PTWidget.java @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.Set; +import java.util.function.Consumer; import java.util.logging.Logger; import com.google.gwt.dom.client.Element; @@ -41,6 +42,7 @@ import com.google.gwt.event.dom.client.DropEvent; import com.google.gwt.event.dom.client.FocusEvent; import com.google.gwt.event.dom.client.KeyDownEvent; +import com.google.gwt.event.dom.client.KeyEvent; import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.MouseDownEvent; @@ -120,9 +122,9 @@ public T asWidget() { private void addDomHandler(final ReaderBuffer buffer, final DomHandlerType domHandlerType) { if (DomHandlerType.CLICK == domHandlerType) { - uiObject.addDomHandler(event -> triggerMouseEvent(domHandlerType, event), ClickEvent.getType()); + uiObject.addDomHandler(event -> triggerMouseClickEvent(domHandlerType, event), ClickEvent.getType()); } else if (DomHandlerType.DOUBLE_CLICK == domHandlerType) { - uiObject.addDomHandler(event -> triggerMouseEvent(domHandlerType, event), DoubleClickEvent.getType()); + uiObject.addDomHandler(event -> triggerMouseClickEvent(domHandlerType, event), DoubleClickEvent.getType()); } else if (DomHandlerType.MOUSE_OVER == domHandlerType) { uiObject.addDomHandler(event -> triggerMouseEvent(domHandlerType, event), MouseOverEvent.getType()); } else if (DomHandlerType.MOUSE_OUT == domHandlerType) { @@ -138,11 +140,14 @@ private void addDomHandler(final ReaderBuffer buffer, final DomHandlerType domHa } else if (DomHandlerType.FOCUS == domHandlerType) { uiObject.addDomHandler(event -> triggerDomEvent(domHandlerType, event), FocusEvent.getType()); } else if (DomHandlerType.KEY_PRESS == domHandlerType) { - triggerKeyPressEvent(domHandlerType, extractKeyFilter(buffer)); + final JSONArray keyFilter = extractKeyFilter(buffer); + uiObject.addDomHandler(event -> triggerKeyEvent(domHandlerType, event, keyFilter), KeyPressEvent.getType()); } else if (DomHandlerType.KEY_DOWN == domHandlerType) { - triggerKeyDownEvent(domHandlerType, extractKeyFilter(buffer)); + final JSONArray keyFilter = extractKeyFilter(buffer); + uiObject.addDomHandler(event -> triggerKeyEvent(domHandlerType, event, keyFilter), KeyDownEvent.getType()); } else if (DomHandlerType.KEY_UP == domHandlerType) { - triggerKeyUpEvent(domHandlerType, extractKeyFilter(buffer)); + final JSONArray keyFilter = extractKeyFilter(buffer); + uiObject.addDomHandler(event -> triggerKeyUpEvent(domHandlerType, event, keyFilter), KeyUpEvent.getType()); } else if (DomHandlerType.DRAG_START == domHandlerType) { uiObject.getElement().setDraggable(Element.DRAGGABLE_TRUE); uiObject.addBitlessDomHandler(event -> { @@ -164,8 +169,9 @@ private void addDomHandler(final ReaderBuffer buffer, final DomHandlerType domHa uiObject.addBitlessDomHandler(event -> { event.preventDefault(); - final String dragWidgetID = event.getData("text"); + final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); + final String dragWidgetID = event.getData("text"); if (dragWidgetID != null) eventInstruction.put(ClientToServerModel.DRAG_SRC, Long.parseLong(dragWidgetID)); uiBuilder.sendDataToServer(uiObject, eventInstruction); }, DropEvent.getType()); @@ -183,121 +189,90 @@ protected PTInstruction buildEventInstruction(final DomHandlerType domHandlerTyp } private void triggerDomEvent(final DomHandlerType domHandlerType, final DomEvent event) { + triggerDomEvent(domHandlerType, event, null); + } + + protected void triggerDomEvent(final DomHandlerType domHandlerType, final DomEvent event, + final Consumer enricher) { final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); + if (enricher != null) enricher.accept(eventInstruction); uiBuilder.sendDataToServer(uiObject, eventInstruction); preventOrStopEvent(event); } protected void triggerMouseEvent(final DomHandlerType domHandlerType, final MouseEvent event) { - final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); - - final JSONArray eventInfo = new JSONArray(); - eventInfo.set(0, new JSONNumber(event.getClientX())); - eventInfo.set(1, new JSONNumber(event.getClientY())); - eventInfo.set(2, new JSONNumber(event.getX())); - eventInfo.set(3, new JSONNumber(event.getY())); - eventInfo.set(4, new JSONNumber(event.getNativeButton())); - eventInfo.set(5, JSONBoolean.getInstance(event.isControlKeyDown())); - eventInfo.set(6, JSONBoolean.getInstance(event.isAltKeyDown())); - eventInfo.set(7, JSONBoolean.getInstance(event.isShiftKeyDown())); - eventInfo.set(8, JSONBoolean.getInstance(event.isMetaKeyDown())); - eventInstruction.put(ClientToServerModel.EVENT_INFO, eventInfo); - - final JSONArray widgetInfo = new JSONArray(); - widgetInfo.set(0, new JSONNumber(uiObject.getAbsoluteLeft())); - widgetInfo.set(1, new JSONNumber(uiObject.getAbsoluteTop())); - widgetInfo.set(2, new JSONNumber(uiObject.getOffsetHeight())); - widgetInfo.set(3, new JSONNumber(uiObject.getOffsetWidth())); - eventInstruction.put(ClientToServerModel.WIDGET_POSITION, widgetInfo); - - uiBuilder.sendDataToServer(uiObject, eventInstruction); + triggerDomEvent(domHandlerType, event, instruction -> { + final JSONArray eventInfo = new JSONArray(); + eventInfo.set(0, new JSONNumber(event.getClientX())); + eventInfo.set(1, new JSONNumber(event.getClientY())); + eventInfo.set(2, new JSONNumber(event.getX())); + eventInfo.set(3, new JSONNumber(event.getY())); + eventInfo.set(4, new JSONNumber(event.getNativeButton())); + eventInfo.set(5, JSONBoolean.getInstance(event.isControlKeyDown())); + eventInfo.set(6, JSONBoolean.getInstance(event.isAltKeyDown())); + eventInfo.set(7, JSONBoolean.getInstance(event.isShiftKeyDown())); + eventInfo.set(8, JSONBoolean.getInstance(event.isMetaKeyDown())); + instruction.put(ClientToServerModel.EVENT_INFO, eventInfo); + + final JSONArray widgetInfo = new JSONArray(); + widgetInfo.set(0, new JSONNumber(uiObject.getAbsoluteLeft())); + widgetInfo.set(1, new JSONNumber(uiObject.getAbsoluteTop())); + widgetInfo.set(2, new JSONNumber(uiObject.getOffsetHeight())); + widgetInfo.set(3, new JSONNumber(uiObject.getOffsetWidth())); + instruction.put(ClientToServerModel.WIDGET_POSITION, widgetInfo); + }); + } - preventOrStopEvent(event); + protected void triggerMouseClickEvent(final DomHandlerType domHandlerType, final MouseEvent event) { + triggerMouseEvent(domHandlerType, event); } private void triggerMouseWhellEvent(final DomHandlerType domHandlerType, final MouseWheelEvent event) { - final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); - final JSONArray eventInfo = new JSONArray(); - eventInfo.set(0, new JSONNumber(event.getClientX())); - eventInfo.set(1, new JSONNumber(event.getClientY())); - eventInfo.set(2, new JSONNumber(event.getX())); - eventInfo.set(3, new JSONNumber(event.getY())); - eventInfo.set(4, new JSONNumber(event.getNativeButton())); - eventInfo.set(5, new JSONNumber(event.getDeltaY())); - eventInstruction.put(ClientToServerModel.EVENT_INFO, eventInfo); - - final JSONArray widgetInfo = new JSONArray(); - widgetInfo.set(0, new JSONNumber(uiObject.getAbsoluteLeft())); - widgetInfo.set(1, new JSONNumber(uiObject.getAbsoluteTop())); - widgetInfo.set(2, new JSONNumber(uiObject.getOffsetHeight())); - widgetInfo.set(3, new JSONNumber(uiObject.getOffsetWidth())); - eventInstruction.put(ClientToServerModel.WIDGET_POSITION, widgetInfo); - - uiBuilder.sendDataToServer(uiObject, eventInstruction); - - preventOrStopEvent(event); + triggerDomEvent(domHandlerType, event, instruction -> { + final JSONArray eventInfo = new JSONArray(); + eventInfo.set(0, new JSONNumber(event.getClientX())); + eventInfo.set(1, new JSONNumber(event.getClientY())); + eventInfo.set(2, new JSONNumber(event.getX())); + eventInfo.set(3, new JSONNumber(event.getY())); + eventInfo.set(4, new JSONNumber(event.getNativeButton())); + eventInfo.set(5, new JSONNumber(event.getDeltaY())); + instruction.put(ClientToServerModel.EVENT_INFO, eventInfo); + + final JSONArray widgetInfo = new JSONArray(); + widgetInfo.set(0, new JSONNumber(uiObject.getAbsoluteLeft())); + widgetInfo.set(1, new JSONNumber(uiObject.getAbsoluteTop())); + widgetInfo.set(2, new JSONNumber(uiObject.getOffsetHeight())); + widgetInfo.set(3, new JSONNumber(uiObject.getOffsetWidth())); + instruction.put(ClientToServerModel.WIDGET_POSITION, widgetInfo); + }); } - private void triggerKeyPressEvent(final DomHandlerType domHandlerType, final JSONArray keyFilter) { - uiObject.addDomHandler(event -> { - final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); - eventInstruction.put(ClientToServerModel.VALUE_KEY, event.getNativeEvent().getKeyCode()); - - if (keyFilter != null) { - for (int i = 0; i < keyFilter.size(); i++) { - final JSONNumber keyCode = keyFilter.get(i).isNumber(); - if (keyCode.doubleValue() == event.getNativeEvent().getKeyCode()) { - uiBuilder.sendDataToServer(uiObject, eventInstruction); - break; - } + protected void triggerKeyEvent(final DomHandlerType domHandlerType, final KeyEvent event, final JSONArray keyFilter) { + boolean needToBeSent = false; + final int nativeKeyCode = event.getNativeEvent().getKeyCode(); + if (keyFilter != null) { + for (int i = 0; i < keyFilter.size(); i++) { + final JSONNumber keyCode = keyFilter.get(i).isNumber(); + if (keyCode.doubleValue() == nativeKeyCode) { + needToBeSent = true; + break; } - } else { - uiBuilder.sendDataToServer(uiObject, eventInstruction); } + } else { + needToBeSent = true; + } - preventOrStopEvent(event); - }, KeyPressEvent.getType()); - } - - private void triggerKeyDownEvent(final DomHandlerType domHandlerType, final JSONArray keyFilter) { - uiObject.addDomHandler(event -> { + if (needToBeSent) { final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); - eventInstruction.put(ClientToServerModel.VALUE_KEY, event.getNativeKeyCode()); - - if (keyFilter != null) { - for (int i = 0; i < keyFilter.size(); i++) { - final JSONNumber keyCode = keyFilter.get(i).isNumber(); - if (keyCode.doubleValue() == event.getNativeKeyCode()) { - uiBuilder.sendDataToServer(uiObject, eventInstruction); - break; - } - } - } else { - uiBuilder.sendDataToServer(uiObject, eventInstruction); - } + eventInstruction.put(ClientToServerModel.VALUE_KEY, nativeKeyCode); + uiBuilder.sendDataToServer(uiObject, eventInstruction); + } - preventOrStopEvent(event); - }, KeyDownEvent.getType()); + preventOrStopEvent(event); } - protected void triggerKeyUpEvent(final DomHandlerType domHandlerType, final JSONArray keyUpFilter) { - uiObject.addDomHandler(event -> { - final PTInstruction eventInstruction = buildEventInstruction(domHandlerType); - eventInstruction.put(ClientToServerModel.VALUE_KEY, event.getNativeKeyCode()); - - if (keyUpFilter != null) { - for (int i = 0; i < keyUpFilter.size(); i++) { - final JSONNumber keyCode = keyUpFilter.get(i).isNumber(); - if (keyCode.doubleValue() == event.getNativeKeyCode()) { - uiBuilder.sendDataToServer(uiObject, eventInstruction); - break; - } - } - } else { - uiBuilder.sendDataToServer(uiObject, eventInstruction); - } - preventOrStopEvent(event); - }, KeyUpEvent.getType()); + protected void triggerKeyUpEvent(final DomHandlerType domHandlerType, final KeyUpEvent event, final JSONArray keyFilter) { + triggerKeyEvent(domHandlerType, event, keyFilter); } protected void preventOrStopEvent(final DomEvent event) {