diff options
author | patrik <patrik@vaadin.com> | 2015-08-12 16:02:50 +0300 |
---|---|---|
committer | patrik <patrik@vaadin.com> | 2015-08-20 13:30:46 +0300 |
commit | ba7071612bb546a74e49fcd82d37ceab9bbe420a (patch) | |
tree | a716711a8a847277617a5ab3577fe70131e84843 /client | |
parent | 7ed0d2a10e250b6ccadca1a6a1d3b9e52b9973b7 (diff) | |
download | vaadin-framework-ba7071612bb546a74e49fcd82d37ceab9bbe420a.tar.gz vaadin-framework-ba7071612bb546a74e49fcd82d37ceab9bbe420a.zip |
Make Grid Editor return focus to last focused cell (#16834)
Change-Id: Ifbea95c2b5b39ca3c4532baa7e3f9298423bc030
Diffstat (limited to 'client')
-rw-r--r-- | client/src/com/vaadin/client/widgets/Grid.java | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java index eb54f9c720..3271f51656 100644 --- a/client/src/com/vaadin/client/widgets/Grid.java +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -50,6 +50,9 @@ import com.google.gwt.dom.client.TableSectionElement; import com.google.gwt.dom.client.Touch; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.dom.client.FocusEvent; +import com.google.gwt.event.dom.client.FocusHandler; +import com.google.gwt.event.dom.client.HasFocusHandlers; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyDownEvent; import com.google.gwt.event.dom.client.KeyDownHandler; @@ -1157,11 +1160,12 @@ public class Grid<T> extends ResizeComposite implements private DivElement message = DivElement.as(DOM.createDiv()); private Map<Column<?, T>, Widget> columnToWidget = new HashMap<Column<?, T>, Widget>(); + private List<HandlerRegistration> focusHandlers = new ArrayList<HandlerRegistration>(); private boolean enabled = false; private State state = State.INACTIVE; private int rowIndex = -1; - private int columnIndex = -1; + private int focusedColumnIndex = -1; private String styleName = null; /* @@ -1363,7 +1367,8 @@ public class Grid<T> extends ResizeComposite implements } } - this.columnIndex = columnIndex; + this.rowIndex = rowIndex; + this.focusedColumnIndex = columnIndex; state = State.ACTIVATING; if (grid.getEscalator().getVisibleRowRange().contains(rowIndex)) { @@ -1410,7 +1415,7 @@ public class Grid<T> extends ResizeComposite implements hideOverlay(); state = State.INACTIVE; rowIndex = -1; - columnIndex = -1; + focusedColumnIndex = -1; grid.getEscalator().setScrollLocked(Direction.VERTICAL, false); updateSelectionCheckboxesAsNeeded(true); grid.fireEvent(new EditorCloseEvent(grid.eventCell)); @@ -1620,7 +1625,28 @@ public class Grid<T> extends ResizeComposite implements attachWidget(editor, cell); } - if (i == columnIndex) { + final int currentColumnIndex = i; + if (editor instanceof HasFocusHandlers) { + // Use a proper focus handler if available + focusHandlers.add(((HasFocusHandlers) editor) + .addFocusHandler(new FocusHandler() { + @Override + public void onFocus(FocusEvent event) { + focusedColumnIndex = currentColumnIndex; + } + })); + } else { + // Try sniffing for DOM focus events + focusHandlers.add(editor.addDomHandler( + new FocusHandler() { + @Override + public void onFocus(FocusEvent event) { + focusedColumnIndex = currentColumnIndex; + } + }, FocusEvent.getType())); + } + + if (i == focusedColumnIndex) { if (editor instanceof Focusable) { ((Focusable) editor).focus(); } else if (editor instanceof com.google.gwt.user.client.ui.Focusable) { @@ -1750,6 +1776,11 @@ public class Grid<T> extends ResizeComposite implements pinnedRowHandle = null; } + for (HandlerRegistration r : focusHandlers) { + r.removeHandler(); + } + focusHandlers.clear(); + for (Widget w : columnToWidget.values()) { setParent(w, null); } @@ -1766,6 +1797,10 @@ public class Grid<T> extends ResizeComposite implements scrollHandler.removeHandler(); clearEditorColumnErrors(); + + if (focusedColumnIndex != -1) { + grid.focusCell(rowIndex, focusedColumnIndex); + } } protected void setStylePrimaryName(String primaryName) { @@ -5554,6 +5589,35 @@ public class Grid<T> extends ResizeComposite implements } /** + * Try to focus a cell by row and column index. Purely internal method. + * + * @param rowIndex + * row index from Editor + * @param columnIndex + * column index from Editor + */ + void focusCell(int rowIndex, int columnIndex) { + RowReference<T> row = new RowReference<T>(this); + Range visibleRows = escalator.getVisibleRowRange(); + + TableRowElement rowElement; + if (visibleRows.contains(rowIndex)) { + rowElement = escalator.getBody().getRowElement(rowIndex); + } else { + rowElement = null; + getLogger().warning("Row index was not among visible row range"); + } + row.set(rowIndex, dataSource.getRow(rowIndex), rowElement); + + CellReference<T> cell = new CellReference<T>(row); + cell.set(columnIndex, columnIndex, getVisibleColumn(columnIndex)); + + cellFocusHandler.setCellFocus(cell); + + WidgetUtil.focus(getElement()); + } + + /** * Refreshes all header rows */ void refreshHeader() { |