diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2016-08-02 12:46:50 +0300 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-01-03 09:47:18 +0200 |
commit | 6f05873bad384856766e9c4eebb7e4a5a5cece79 (patch) | |
tree | 919c5088d5bd865c96f0455b2292a3c513242b81 /client | |
parent | 16220d8e6735f77cb8165e1a4d8d46c1a1be07d3 (diff) | |
download | vaadin-framework-6f05873bad384856766e9c4eebb7e4a5a5cece79.tar.gz vaadin-framework-6f05873bad384856766e9c4eebb7e4a5a5cece79.zip |
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
Diffstat (limited to 'client')
-rw-r--r-- | client/src/main/java/com/vaadin/client/widgets/Grid.java | 314 |
1 files changed, 181 insertions, 133 deletions
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<T> extends ResizeComposite implements HasSelectionHandlers<T>, } } + /** + * A handler for DOM events emitted by elements in Grid. + * + * @param <T> + * the grid row type + */ + public interface BrowserEventHandler<T> { + /** + * 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<T> cell); + } + private static final String CUSTOM_STYLE_PROPERTY_NAME = "customStyle"; /** @@ -7316,33 +7337,33 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, 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<T> extends ResizeComposite implements HasSelectionHandlers<T>, return w != null; } - private boolean handleEditorEvent(Event event, RowContainer container) { - if (!isEditorEnabled()) { - return false; + private BrowserEventHandler<T> editorEventHandler = new BrowserEventHandler<T>() { + + @Override + public boolean onEvent(Event event, EventCellReference<T> eventCell) { + if (!isEditorEnabled()) { + return false; + } + + Widget widget; + if (editor.focusedColumnIndex < 0) { + widget = null; + } else { + widget = editor.getWidget(getColumn(editor.focusedColumnIndex)); + } + + EditorDomEvent<T> 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<T> superEventHandler = new BrowserEventHandler<T>() { + + @Override + public boolean onEvent(Event event, EventCellReference<T> cell) { + Grid.super.onBrowserEvent(event); + return false; } + }; - EditorDomEvent<T> editorEvent = new EditorDomEvent<>(event, - getEventCell(), w); + private BrowserEventHandler<T> childWidgetEventHandler = new BrowserEventHandler<T>() { - return getEditor().getEventHandler().handleEvent(editorEvent); - } + @Override + public boolean onEvent(Event e, EventCellReference<T> cell) { + return isElementInChildWidget(Element.as(e.getEventTarget())); + } + }; - private boolean handleSuperEvent(Event event, RowContainer container) { - super.onBrowserEvent(event); - return false; - } + private BrowserEventHandler<T> rendererEventHandler = new BrowserEventHandler<T>() { - private boolean handleChildWidgetEvent(Event event, - RowContainer container) { - return isElementInChildWidget(Element.as(event.getEventTarget())); - } + @Override + public boolean onEvent(Event e, EventCellReference<T> cell) { - private boolean handleRendererEvent(Event event, RowContainer container) { + if (!cell.isBody()) { + return false; + } - if (container == escalator.getBody()) { - Column<?, T> gridColumn = eventCell.getColumn(); - boolean enterKey = event.getType().equals(BrowserEvents.KEYDOWN) - && event.getKeyCode() == KeyCodes.KEY_ENTER; - boolean doubleClick = event.getType() - .equals(BrowserEvents.DBLCLICK); + Column<?, T> 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<String> navigation = cellFocusHandler.getNavigationEvents(); - if (navigation.contains(event.getType())) { - cellFocusHandler.handleNavigationEvent(event, eventCell); - } - return false; - } + private BrowserEventHandler<T> cellFocusEventHandler = new BrowserEventHandler<T>() { - 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<T> cell) { + Collection<String> 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<T> headerCellDragStartHandler = new BrowserEventHandler<T>() { + + @Override + public boolean onEvent(Event e, EventCellReference<T> 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<T> cellStyleGenerator; private RowStyleGenerator<T> rowStyleGenerator; private RowReference<T> rowReference = new RowReference<>(this); @@ -7494,94 +7537,99 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, private RendererCellReference rendererCellReference = new RendererCellReference( (RowReference<Object>) 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<T> headerDefaultRowEventHandler = new BrowserEventHandler<T>() { - 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<T> 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") |