diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2014-12-18 10:49:10 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2014-12-18 12:43:56 +0200 |
commit | 1326e5cf76093ffed3b832afe7dcf3f909aec846 (patch) | |
tree | 60264f0461604e28fa5b34c4b5f3a9ea829ce1fa /client | |
parent | 1c0a4291cbea0b26351a2493228cda3b1cb05c01 (diff) | |
download | vaadin-framework-1326e5cf76093ffed3b832afe7dcf3f909aec846.tar.gz vaadin-framework-1326e5cf76093ffed3b832afe7dcf3f909aec846.zip |
Use CellReference in Grid events and event handling (#13334)
Change-Id: Ie4ed85e56f0c23850eec56518a7493f5ed3257bd
Diffstat (limited to 'client')
9 files changed, 119 insertions, 50 deletions
diff --git a/client/src/com/vaadin/client/widget/grid/CellReference.java b/client/src/com/vaadin/client/widget/grid/CellReference.java index 6adf8c892c..9a4601c6e2 100644 --- a/client/src/com/vaadin/client/widget/grid/CellReference.java +++ b/client/src/com/vaadin/client/widget/grid/CellReference.java @@ -105,4 +105,13 @@ public class CellReference<T> { public Object getValue() { return getColumn().getValue(getRow()); } + + /** + * Gets the RowReference for this CellReference. + * + * @return the row reference + */ + protected RowReference<T> getRowReference() { + return rowReference; + } }
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/EventCellReference.java b/client/src/com/vaadin/client/widget/grid/EventCellReference.java new file mode 100644 index 0000000000..300bbd90d9 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/EventCellReference.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.widget.grid; + +import com.vaadin.client.widget.escalator.Cell; +import com.vaadin.client.widgets.Grid; + +/** + * A data class which contains information which identifies a cell being the + * target of an event from {@link Grid}. + * <p> + * Since this class follows the <code>Flyweight</code>-pattern any instance of + * this object is subject to change without the user knowing it and so should + * not be stored anywhere outside of the method providing these instances. + * + * @since + * @author Vaadin Ltd + */ +public class EventCellReference<T> extends CellReference<T> { + + private Grid<T> grid; + + public EventCellReference(Grid<T> grid) { + super(new RowReference<T>(grid)); + this.grid = grid; + } + + /** + * Sets the RowReference and CellReference to point to given Cell. + * + * @param targetCell + * cell to point to + */ + public void set(Cell targetCell) { + int row = targetCell.getRow(); + int column = targetCell.getColumn(); + getRowReference().set(row, grid.getDataSource().getRow(row)); + set(column, grid.getColumn(column)); + } + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/GridClickEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridClickEvent.java index aeade4721e..0e4b4b71de 100644 --- a/client/src/com/vaadin/client/widget/grid/events/GridClickEvent.java +++ b/client/src/com/vaadin/client/widget/grid/events/GridClickEvent.java @@ -16,6 +16,7 @@ package com.vaadin.client.widget.grid.events; import com.google.gwt.dom.client.BrowserEvents; +import com.vaadin.client.widget.grid.CellReference; import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridClickHandler; import com.vaadin.client.widgets.Grid; import com.vaadin.client.widgets.Grid.AbstractGridMouseEvent; @@ -29,8 +30,8 @@ import com.vaadin.client.widgets.Grid.Section; */ public class GridClickEvent extends AbstractGridMouseEvent<GridClickHandler> { - public GridClickEvent(Grid<?> grid) { - super(grid); + public GridClickEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); } @Override diff --git a/client/src/com/vaadin/client/widget/grid/events/GridKeyDownEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridKeyDownEvent.java index 6d0787be37..0e4ea107d0 100644 --- a/client/src/com/vaadin/client/widget/grid/events/GridKeyDownEvent.java +++ b/client/src/com/vaadin/client/widget/grid/events/GridKeyDownEvent.java @@ -17,6 +17,7 @@ package com.vaadin.client.widget.grid.events; import com.google.gwt.dom.client.BrowserEvents; import com.google.gwt.event.dom.client.KeyCodes; +import com.vaadin.client.widget.grid.CellReference; import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyDownHandler; import com.vaadin.client.widgets.Grid; import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent; @@ -30,8 +31,8 @@ import com.vaadin.client.widgets.Grid.Section; */ public class GridKeyDownEvent extends AbstractGridKeyEvent<GridKeyDownHandler> { - public GridKeyDownEvent(Grid<?> grid) { - super(grid); + public GridKeyDownEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); } @Override diff --git a/client/src/com/vaadin/client/widget/grid/events/GridKeyPressEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridKeyPressEvent.java index 2cb63aca28..9d96d9175c 100644 --- a/client/src/com/vaadin/client/widget/grid/events/GridKeyPressEvent.java +++ b/client/src/com/vaadin/client/widget/grid/events/GridKeyPressEvent.java @@ -16,6 +16,7 @@ package com.vaadin.client.widget.grid.events; import com.google.gwt.dom.client.BrowserEvents; +import com.vaadin.client.widget.grid.CellReference; import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyPressHandler; import com.vaadin.client.widgets.Grid; import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent; @@ -30,8 +31,8 @@ import com.vaadin.client.widgets.Grid.Section; public class GridKeyPressEvent extends AbstractGridKeyEvent<GridKeyPressHandler> { - public GridKeyPressEvent(Grid<?> grid) { - super(grid); + public GridKeyPressEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); } @Override diff --git a/client/src/com/vaadin/client/widget/grid/events/GridKeyUpEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridKeyUpEvent.java index ddfce5f478..60b7553d53 100644 --- a/client/src/com/vaadin/client/widget/grid/events/GridKeyUpEvent.java +++ b/client/src/com/vaadin/client/widget/grid/events/GridKeyUpEvent.java @@ -17,6 +17,7 @@ package com.vaadin.client.widget.grid.events; import com.google.gwt.dom.client.BrowserEvents; import com.google.gwt.event.dom.client.KeyCodes; +import com.vaadin.client.widget.grid.CellReference; import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyUpHandler; import com.vaadin.client.widgets.Grid; import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent; @@ -30,8 +31,8 @@ import com.vaadin.client.widgets.Grid.Section; */ public class GridKeyUpEvent extends AbstractGridKeyEvent<GridKeyUpHandler> { - public GridKeyUpEvent(Grid<?> grid) { - super(grid); + public GridKeyUpEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); } @Override diff --git a/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java b/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java index 205d879506..dce5a3586d 100644 --- a/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java +++ b/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java @@ -35,7 +35,7 @@ public class ClickSelectHandler<T> { @Override public void onClick(GridClickEvent event) { - T row = grid.getDataSource().getRow(event.getTargetCell().getRow()); + T row = (T) event.getTargetCell().getRow(); if (!grid.isSelected(row)) { grid.select(row); } diff --git a/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java b/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java index 3a3d0354fd..1f9eafa42a 100644 --- a/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java +++ b/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java @@ -17,7 +17,6 @@ package com.vaadin.client.widget.grid.selection; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.shared.HandlerRegistration; -import com.vaadin.client.widget.escalator.Cell; import com.vaadin.client.widget.grid.DataAvailableEvent; import com.vaadin.client.widget.grid.DataAvailableHandler; import com.vaadin.client.widget.grid.events.BodyKeyDownHandler; @@ -53,8 +52,7 @@ public class SpaceSelectHandler<T> { event.getNativeEvent().preventDefault(); spaceDown = true; - Cell focused = event.getFocusedCell(); - final int rowIndex = focused.getRow(); + final int rowIndex = event.getFocusedCell().getRowIndex(); if (scrollHandler != null) { scrollHandler.removeHandler(); diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java index f643fafbea..80493388e0 100644 --- a/client/src/com/vaadin/client/widgets/Grid.java +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -81,6 +81,7 @@ import com.vaadin.client.widget.grid.DataAvailableHandler; import com.vaadin.client.widget.grid.EditorHandler; import com.vaadin.client.widget.grid.EditorHandler.EditorRequest; import com.vaadin.client.widget.grid.EditorHandler.EditorRequest.RequestCallback; +import com.vaadin.client.widget.grid.EventCellReference; import com.vaadin.client.widget.grid.GridUtil; import com.vaadin.client.widget.grid.RendererCellReference; import com.vaadin.client.widget.grid.RowReference; @@ -1306,12 +1307,13 @@ public class Grid<T> extends ResizeComposite implements extends KeyEvent<HANDLER> { private Grid<?> grid; - protected Cell focusedCell; private final Type<HANDLER> associatedType = new Type<HANDLER>( getBrowserEventType(), this); + private final CellReference<?> targetCell; - public AbstractGridKeyEvent(Grid<?> grid) { + public AbstractGridKeyEvent(Grid<?> grid, CellReference<?> targetCell) { this.grid = grid; + this.targetCell = targetCell; } protected abstract String getBrowserEventType(); @@ -1330,8 +1332,8 @@ public class Grid<T> extends ResizeComposite implements * * @return focused cell */ - public Cell getFocusedCell() { - return focusedCell; + public CellReference<?> getFocusedCell() { + return targetCell; } @Override @@ -1340,7 +1342,6 @@ public class Grid<T> extends ResizeComposite implements if (Element.is(target) && !grid.isElementInChildWidget(Element.as(target))) { - focusedCell = grid.cellFocusHandler.getFocusedCell(); Section section = Section.FOOTER; final RowContainer container = grid.cellFocusHandler.containerWithFocus; if (container == grid.escalator.getHeader()) { @@ -1365,12 +1366,13 @@ public class Grid<T> extends ResizeComposite implements extends MouseEvent<HANDLER> { private Grid<?> grid; - protected Cell targetCell; + private final CellReference<?> targetCell; private final Type<HANDLER> associatedType = new Type<HANDLER>( getBrowserEventType(), this); - public AbstractGridMouseEvent(Grid<?> grid) { + public AbstractGridMouseEvent(Grid<?> grid, CellReference<?> targetCell) { this.grid = grid; + this.targetCell = targetCell; } protected abstract String getBrowserEventType(); @@ -1385,11 +1387,11 @@ public class Grid<T> extends ResizeComposite implements } /** - * Gets the target cell for this event. + * Gets the reference of target cell for this event. * * @return target cell */ - public Cell getTargetCell() { + public CellReference<?> getTargetCell() { return targetCell; } @@ -1414,12 +1416,6 @@ public class Grid<T> extends ResizeComposite implements return; } - targetCell = container.getCell(targetElement); - if (targetCell == null) { - // Is not a cell in given container. - return; - } - Section section = Section.FOOTER; if (container == grid.escalator.getHeader()) { section = Section.HEADER; @@ -1440,10 +1436,11 @@ public class Grid<T> extends ResizeComposite implements private static final String CUSTOM_STYLE_PROPERTY_NAME = "customStyle"; - private GridKeyDownEvent keyDown = new GridKeyDownEvent(this); - private GridKeyUpEvent keyUp = new GridKeyUpEvent(this); - private GridKeyPressEvent keyPress = new GridKeyPressEvent(this); - private GridClickEvent clickEvent = new GridClickEvent(this); + private EventCellReference<T> eventCell = new EventCellReference<T>(this); + private GridKeyDownEvent keyDown = new GridKeyDownEvent(this, eventCell); + private GridKeyUpEvent keyUp = new GridKeyUpEvent(this, eventCell); + private GridKeyPressEvent keyPress = new GridKeyPressEvent(this, eventCell); + private GridClickEvent clickEvent = new GridClickEvent(this, eventCell); private class CellFocusHandler { @@ -1920,14 +1917,15 @@ public class Grid<T> extends ResizeComposite implements private final class UserSorter { private final Timer timer; - private Cell scheduledCell; private boolean scheduledMultisort; + private Column<?, T> column; private UserSorter() { timer = new Timer() { + @Override public void run() { - UserSorter.this.sort(scheduledCell, scheduledMultisort); + UserSorter.this.sort(column, scheduledMultisort); } }; } @@ -1945,9 +1943,14 @@ public class Grid<T> extends ResizeComposite implements * whether the sort command should act as a multi-sort stack * or not */ - public void sort(Cell cell, boolean multisort) { + public void sort(Column<?, ?> column, boolean multisort) { + + if (!columns.contains(column)) { + throw new IllegalArgumentException( + "Given column is not a column in this grid. " + + column.toString()); + } - final Column<?, T> column = getColumn(cell.getColumn()); if (!column.isSortable()) { return; } @@ -1993,8 +1996,8 @@ public class Grid<T> extends ResizeComposite implements * @param delay * delay, in milliseconds */ - public void sortAfterDelay(int delay, Cell cell, boolean multisort) { - scheduledCell = cell; + public void sortAfterDelay(int delay, boolean multisort) { + column = eventCell.getColumn(); scheduledMultisort = multisort; timer.schedule(delay); } @@ -3430,7 +3433,8 @@ public class Grid<T> extends ResizeComposite implements return; } - sorter.sort(event.getFocusedCell(), event.isShiftKeyDown()); + sorter.sort(event.getFocusedCell().getColumn(), + event.isShiftKeyDown()); } }); @@ -4451,6 +4455,7 @@ public class Grid<T> extends ResizeComposite implements assert cell != null : "received " + eventType + "-event with a null cell target"; + eventCell.set(cell); // Editor can steal focus from Grid and is still handled if (handleEditorEvent(event, container, cell)) { @@ -4463,7 +4468,7 @@ public class Grid<T> extends ResizeComposite implements if (!isElementInChildWidget(e)) { // Sorting through header Click / KeyUp - if (handleHeaderDefaultRowEvent(event, container, cell)) { + if (handleHeaderDefaultRowEvent(event, container)) { return; } @@ -4471,7 +4476,7 @@ public class Grid<T> extends ResizeComposite implements return; } - if (handleNavigationEvent(event, container, cell)) { + if (handleNavigationEvent(event, container)) { return; } @@ -4530,8 +4535,8 @@ public class Grid<T> extends ResizeComposite implements private boolean handleRendererEvent(Event event, RowContainer container, Cell cell) { - if (container == escalator.getBody() && cell != null) { - Column<?, T> gridColumn = getColumn(cell.getColumn()); + 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() @@ -4564,8 +4569,7 @@ public class Grid<T> extends ResizeComposite implements return false; } - private boolean handleNavigationEvent(Event event, RowContainer unused, - Cell cell) { + private boolean handleNavigationEvent(Event event, RowContainer unused) { if (!event.getType().equals(BrowserEvents.KEYDOWN)) { // Only handle key downs return false; @@ -4624,14 +4628,14 @@ public class Grid<T> extends ResizeComposite implements (RowReference<Object>) rowReference); private boolean handleHeaderDefaultRowEvent(Event event, - RowContainer container, final Cell cell) { + RowContainer container) { if (container != escalator.getHeader()) { return false; } - if (!getHeader().getRow(cell.getRow()).isDefault()) { + if (!getHeader().getRow(eventCell.getRowIndex()).isDefault()) { return false; } - if (!getColumn(cell.getColumn()).isSortable()) { + if (!eventCell.getColumn().isSortable()) { // Only handle sorting events if the column is sortable return false; } @@ -4647,7 +4651,7 @@ public class Grid<T> extends ResizeComposite implements rowEventTouchStartingPoint = new Point(touch.getClientX(), touch.getClientY()); - sorter.sortAfterDelay(GridConstants.LONG_TAP_DELAY, cell, true); + sorter.sortAfterDelay(GridConstants.LONG_TAP_DELAY, true); return true; @@ -4681,7 +4685,7 @@ public class Grid<T> extends ResizeComposite implements if (sorter.isDelayedSortScheduled()) { // Not a long tap yet, perform single sort sorter.cancelDelayedSort(); - sorter.sort(cell, false); + sorter.sort(eventCell.getColumn(), false); } return true; @@ -4697,7 +4701,7 @@ public class Grid<T> extends ResizeComposite implements } else if (BrowserEvents.CLICK.equals(event.getType())) { - sorter.sort(cell, event.getShiftKey()); + sorter.sort(eventCell.getColumn(), event.getShiftKey()); // Click events should go onward to cell focus logic return false; |