Skip to content

Commit

Permalink
Make Grid Editor return focus to last focused cell (#16834)
Browse files Browse the repository at this point in the history
Change-Id: Ifbea95c2b5b39ca3c4532baa7e3f9298423bc030
  • Loading branch information
thevaadinman committed Aug 20, 2015
1 parent 7ed0d2a commit ba70716
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
72 changes: 68 additions & 4 deletions client/src/com/vaadin/client/widgets/Grid.java
Expand Up @@ -50,6 +50,9 @@
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;
Expand Down Expand Up @@ -1157,11 +1160,12 @@ protected enum State {
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;

/*
Expand Down Expand Up @@ -1363,7 +1367,8 @@ public void editRow(final int rowIndex, int columnIndex) {
}
}

this.columnIndex = columnIndex;
this.rowIndex = rowIndex;
this.focusedColumnIndex = columnIndex;
state = State.ACTIVATING;

if (grid.getEscalator().getVisibleRowRange().contains(rowIndex)) {
Expand Down Expand Up @@ -1410,7 +1415,7 @@ private void doCancel() {
hideOverlay();
state = State.INACTIVE;
rowIndex = -1;
columnIndex = -1;
focusedColumnIndex = -1;
grid.getEscalator().setScrollLocked(Direction.VERTICAL, false);
updateSelectionCheckboxesAsNeeded(true);
grid.fireEvent(new EditorCloseEvent(grid.eventCell));
Expand Down Expand Up @@ -1620,7 +1625,28 @@ public void onScroll(ScrollEvent event) {
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) {
Expand Down Expand Up @@ -1750,6 +1776,11 @@ protected void hideOverlay() {
pinnedRowHandle = null;
}

for (HandlerRegistration r : focusHandlers) {
r.removeHandler();
}
focusHandlers.clear();

for (Widget w : columnToWidget.values()) {
setParent(w, null);
}
Expand All @@ -1766,6 +1797,10 @@ protected void hideOverlay() {
scrollHandler.removeHandler();

clearEditorColumnErrors();

if (focusedColumnIndex != -1) {
grid.focusCell(rowIndex, focusedColumnIndex);
}
}

protected void setStylePrimaryName(String primaryName) {
Expand Down Expand Up @@ -5553,6 +5588,35 @@ private void refreshRowContainer(RowContainer rows, StaticSection<?> section) {
}
}

/**
* 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
*/
Expand Down
Expand Up @@ -81,9 +81,19 @@ public void testMouseOpeningClosing() {
getGridElement().getCell(4, 0).doubleClick();
assertNotNull(getEditor());

getCancelButton().click();
// Move focus to the third input field
getEditor().findElements(By.className("gwt-TextBox")).get(2).click();

// Press save button
getSaveButton().click();

// Make sure the editor went away
assertNull(getEditor());

// Check that focus has moved to cell 4,2 - the last one that was
// focused in Editor
assertTrue(getGridElement().getCell(4, 2).isFocused());

// Disable editor
selectMenuPath("Component", "Editor", "Enabled");

Expand Down

0 comments on commit ba70716

Please sign in to comment.