From 6f05873bad384856766e9c4eebb7e4a5a5cece79 Mon Sep 17 00:00:00 2001 From: Johannes Dahlström Date: Tue, 2 Aug 2016 12:46:50 +0300 Subject: Refactor Grid event handling methods to instances of a handler interface Change-Id: Icbed14cbef14c434f1909fa0350cb0364235c774 # Conflicts: # client/src/main/java/com/vaadin/client/widgets/Grid.java --- .../main/java/com/vaadin/client/widgets/Grid.java | 314 ++++++++++++--------- 1 file changed, 181 insertions(+), 133 deletions(-) (limited to 'client') diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index c663b7af50..4bf27cee5c 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -2393,6 +2393,27 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, } } + /** + * A handler for DOM events emitted by elements in Grid. + * + * @param + * the grid row type + */ + public interface BrowserEventHandler { + /** + * Attempts to handle the given DOM event. Returns whether the event was + * handled and subsequent handlers should not be run. + * + * @param event + * the event that occurred + * @param cell + * the cell where the event occurred + * @return true if subsequent handlers should not be run, false + * otherwise + */ + public boolean onEvent(Event event, EventCellReference cell); + } + private static final String CUSTOM_STYLE_PROPERTY_NAME = "customStyle"; /** @@ -7316,33 +7337,33 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, eventCell.set(cell, getSectionFromContainer(container)); // Editor can steal focus from Grid and is still handled - if (handleEditorEvent(event, container)) { + if (editorEventHandler.onEvent(event, eventCell)) { return; } // Fire GridKeyEvents and GridClickEvents. Pass the event to escalator. - if (handleSuperEvent(event, container)) { + if (superEventHandler.onEvent(event, eventCell)) { return; } - if (handleChildWidgetEvent(event, container)) { + if (childWidgetEventHandler.onEvent(event, eventCell)) { return; } - if (handleHeaderCellDragStartEvent(event, container)) { + if (headerCellDragStartHandler.onEvent(event, eventCell)) { return; } // Sorting through header Click / KeyUp - if (handleHeaderDefaultRowEvent(event, container)) { + if (headerDefaultRowEventHandler.onEvent(event, eventCell)) { return; } - if (handleRendererEvent(event, container)) { + if (rendererEventHandler.onEvent(event, eventCell)) { return; } - if (handleCellFocusEvent(event, container)) { + if (cellFocusEventHandler.onEvent(event, eventCell)) { return; } } @@ -7396,97 +7417,119 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, return w != null; } - private boolean handleEditorEvent(Event event, RowContainer container) { - if (!isEditorEnabled()) { - return false; + private BrowserEventHandler editorEventHandler = new BrowserEventHandler() { + + @Override + public boolean onEvent(Event event, EventCellReference eventCell) { + if (!isEditorEnabled()) { + return false; + } + + Widget widget; + if (editor.focusedColumnIndex < 0) { + widget = null; + } else { + widget = editor.getWidget(getColumn(editor.focusedColumnIndex)); + } + + EditorDomEvent editorEvent = new EditorDomEvent<>(event, + eventCell, widget); + + return getEditor().getEventHandler().handleEvent(editorEvent); } + }; - Widget w; - if (editor.focusedColumnIndex < 0) { - w = null; - } else { - w = editor.getWidget(getColumn(editor.focusedColumnIndex)); + private BrowserEventHandler superEventHandler = new BrowserEventHandler() { + + @Override + public boolean onEvent(Event event, EventCellReference cell) { + Grid.super.onBrowserEvent(event); + return false; } + }; - EditorDomEvent editorEvent = new EditorDomEvent<>(event, - getEventCell(), w); + private BrowserEventHandler childWidgetEventHandler = new BrowserEventHandler() { - return getEditor().getEventHandler().handleEvent(editorEvent); - } + @Override + public boolean onEvent(Event e, EventCellReference cell) { + return isElementInChildWidget(Element.as(e.getEventTarget())); + } + }; - private boolean handleSuperEvent(Event event, RowContainer container) { - super.onBrowserEvent(event); - return false; - } + private BrowserEventHandler rendererEventHandler = new BrowserEventHandler() { - private boolean handleChildWidgetEvent(Event event, - RowContainer container) { - return isElementInChildWidget(Element.as(event.getEventTarget())); - } + @Override + public boolean onEvent(Event e, EventCellReference cell) { - private boolean handleRendererEvent(Event event, RowContainer container) { + if (!cell.isBody()) { + return false; + } - if (container == escalator.getBody()) { - Column gridColumn = eventCell.getColumn(); - boolean enterKey = event.getType().equals(BrowserEvents.KEYDOWN) - && event.getKeyCode() == KeyCodes.KEY_ENTER; - boolean doubleClick = event.getType() - .equals(BrowserEvents.DBLCLICK); + Column gridColumn = cell.getColumn(); + boolean enterKey = e.getType().equals(BrowserEvents.KEYDOWN) + && e.getKeyCode() == KeyCodes.KEY_ENTER; + boolean doubleClick = e.getType().equals(BrowserEvents.DBLCLICK); if (gridColumn.getRenderer() instanceof ComplexRenderer) { ComplexRenderer cplxRenderer = (ComplexRenderer) gridColumn .getRenderer(); - if (cplxRenderer.getConsumedEvents() - .contains(event.getType())) { - if (cplxRenderer.onBrowserEvent(eventCell, event)) { + if (cplxRenderer.getConsumedEvents().contains(e.getType())) { + if (cplxRenderer.onBrowserEvent(cell, e)) { return true; } } // Calls onActivate if KeyDown and Enter or double click if ((enterKey || doubleClick) - && cplxRenderer.onActivate(eventCell)) { + && cplxRenderer.onActivate(cell)) { return true; } } + return false; } - return false; - } + }; - private boolean handleCellFocusEvent(Event event, RowContainer container) { - Collection navigation = cellFocusHandler.getNavigationEvents(); - if (navigation.contains(event.getType())) { - cellFocusHandler.handleNavigationEvent(event, eventCell); - } - return false; - } + private BrowserEventHandler cellFocusEventHandler = new BrowserEventHandler() { - private boolean handleHeaderCellDragStartEvent(Event event, - RowContainer container) { - if (!isColumnReorderingAllowed()) { - return false; - } - if (container != escalator.getHeader()) { - return false; - } - if (eventCell.getColumnIndex() < escalator.getColumnConfiguration() - .getFrozenColumnCount()) { + @Override + public boolean onEvent(Event e, EventCellReference cell) { + Collection navigation = cellFocusHandler + .getNavigationEvents(); + if (navigation.contains(e.getType())) { + cellFocusHandler.handleNavigationEvent(e, cell); + } return false; } + }; - if (event.getTypeInt() == Event.ONMOUSEDOWN - && event.getButton() == NativeEvent.BUTTON_LEFT - || event.getTypeInt() == Event.ONTOUCHSTART) { - dndHandler.onDragStartOnDraggableElement(event, - headerCellDndCallback); - event.preventDefault(); - event.stopPropagation(); - return true; + private BrowserEventHandler headerCellDragStartHandler = new BrowserEventHandler() { + + @Override + public boolean onEvent(Event e, EventCellReference cell) { + if (!isColumnReorderingAllowed()) { + return false; + } + if (!cell.isHeader()) { + return false; + } + if (cell.getColumnIndex() < escalator.getColumnConfiguration() + .getFrozenColumnCount()) { + return false; + } + + if (e.getTypeInt() == Event.ONMOUSEDOWN + && e.getButton() == NativeEvent.BUTTON_LEFT + || e.getTypeInt() == Event.ONTOUCHSTART) { + dndHandler.onDragStartOnDraggableElement(e, + headerCellDndCallback); + e.preventDefault(); + e.stopPropagation(); + return true; + } + return false; } - return false; - } + }; - private Point rowEventTouchStartingPoint; private CellStyleGenerator cellStyleGenerator; private RowStyleGenerator rowStyleGenerator; private RowReference rowReference = new RowReference<>(this); @@ -7494,94 +7537,99 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, private RendererCellReference rendererCellReference = new RendererCellReference( (RowReference) rowReference); - private boolean handleHeaderDefaultRowEvent(Event event, - RowContainer container) { - if (container != escalator.getHeader()) { - return false; - } - if (!getHeader().getRow(eventCell.getRowIndex()).isDefault()) { - return false; - } - if (!eventCell.getColumn().isSortable()) { - // Only handle sorting events if the column is sortable - return false; - } + private BrowserEventHandler headerDefaultRowEventHandler = new BrowserEventHandler() { - if (BrowserEvents.MOUSEDOWN.equals(event.getType()) - && event.getShiftKey()) { - // Don't select text when shift clicking on a header. - event.preventDefault(); - } + private Point rowEventTouchStartingPoint; - if (BrowserEvents.TOUCHSTART.equals(event.getType())) { - if (event.getTouches().length() > 1) { + @Override + public boolean onEvent(Event event, EventCellReference cell) { + if (!cell.isHeader()) { + return false; + } + if (!getHeader().getRow(cell.getRowIndex()).isDefault()) { + return false; + } + if (!cell.getColumn().isSortable()) { + // Only handle sorting events if the column is sortable return false; } - event.preventDefault(); + if (BrowserEvents.MOUSEDOWN.equals(event.getType()) + && event.getShiftKey()) { + // Don't select text when shift clicking on a header. + event.preventDefault(); + } - Touch touch = event.getChangedTouches().get(0); - rowEventTouchStartingPoint = new Point(touch.getClientX(), - touch.getClientY()); + if (BrowserEvents.TOUCHSTART.equals(event.getType())) { + if (event.getTouches().length() > 1) { + return false; + } - sorter.sortAfterDelay(GridConstants.LONG_TAP_DELAY, true); + event.preventDefault(); - return true; + Touch touch = event.getChangedTouches().get(0); + rowEventTouchStartingPoint = new Point(touch.getClientX(), + touch.getClientY()); - } else if (BrowserEvents.TOUCHMOVE.equals(event.getType())) { - if (event.getTouches().length() > 1) { - return false; - } + sorter.sortAfterDelay(GridConstants.LONG_TAP_DELAY, true); - event.preventDefault(); + return true; - Touch touch = event.getChangedTouches().get(0); - double diffX = Math.abs( - touch.getClientX() - rowEventTouchStartingPoint.getX()); - double diffY = Math.abs( - touch.getClientY() - rowEventTouchStartingPoint.getY()); + } else if (BrowserEvents.TOUCHMOVE.equals(event.getType())) { + if (event.getTouches().length() > 1) { + return false; + } - // Cancel long tap if finger strays too far from - // starting point - if (diffX > GridConstants.LONG_TAP_THRESHOLD - || diffY > GridConstants.LONG_TAP_THRESHOLD) { - sorter.cancelDelayedSort(); - } + event.preventDefault(); - return true; + Touch touch = event.getChangedTouches().get(0); + double diffX = Math.abs( + touch.getClientX() - rowEventTouchStartingPoint.getX()); + double diffY = Math.abs( + touch.getClientY() - rowEventTouchStartingPoint.getY()); + + // Cancel long tap if finger strays too far from + // starting point + if (diffX > GridConstants.LONG_TAP_THRESHOLD + || diffY > GridConstants.LONG_TAP_THRESHOLD) { + sorter.cancelDelayedSort(); + } - } else if (BrowserEvents.TOUCHEND.equals(event.getType())) { - if (event.getTouches().length() > 1) { - return false; - } + return true; - if (sorter.isDelayedSortScheduled()) { - // Not a long tap yet, perform single sort - sorter.cancelDelayedSort(); - sorter.sort(eventCell.getColumn(), false); - } + } else if (BrowserEvents.TOUCHEND.equals(event.getType())) { + if (event.getTouches().length() > 1) { + return false; + } - return true; + if (sorter.isDelayedSortScheduled()) { + // Not a long tap yet, perform single sort + sorter.cancelDelayedSort(); + sorter.sort(cell.getColumn(), false); + } - } else if (BrowserEvents.TOUCHCANCEL.equals(event.getType())) { - if (event.getTouches().length() > 1) { - return false; - } + return true; - sorter.cancelDelayedSort(); + } else if (BrowserEvents.TOUCHCANCEL.equals(event.getType())) { + if (event.getTouches().length() > 1) { + return false; + } - return true; + sorter.cancelDelayedSort(); - } else if (BrowserEvents.CLICK.equals(event.getType())) { + return true; - sorter.sort(eventCell.getColumn(), event.getShiftKey()); + } else if (BrowserEvents.CLICK.equals(event.getType())) { - // Click events should go onward to cell focus logic - return false; - } else { - return false; + sorter.sort(cell.getColumn(), event.getShiftKey()); + + // Click events should go onward to cell focus logic + return false; + } else { + return false; + } } - } + }; @Override @SuppressWarnings("deprecation") -- cgit v1.2.3