diff options
5 files changed, 279 insertions, 174 deletions
diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java index bf5494291f..4616c43902 100644 --- a/client/src/com/vaadin/client/widgets/Grid.java +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -1425,6 +1425,13 @@ public class Grid<T> extends ResizeComposite implements } } + protected void hide() { + hideOverlay(); + grid.getEscalator().setScrollLocked(Direction.VERTICAL, false); + state = State.INACTIVE; + updateSelectionCheckboxesAsNeeded(true); + } + protected void setGrid(final Grid<T> grid) { assert grid != null : "Grid cannot be null"; assert this.grid == null : "Can only attach editor to Grid once"; @@ -5063,7 +5070,7 @@ public class Grid<T> extends ResizeComposite implements sinkEvents(getHeader().getConsumedEvents()); sinkEvents(Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.KEYUP, BrowserEvents.KEYPRESS, BrowserEvents.DBLCLICK, - BrowserEvents.MOUSEDOWN)); + BrowserEvents.MOUSEDOWN, BrowserEvents.CLICK)); // Make ENTER and SHIFT+ENTER in the header perform sorting addHeaderKeyUpHandler(new HeaderKeyUpHandler() { @@ -6306,31 +6313,43 @@ public class Grid<T> extends ResizeComposite implements } private boolean handleEditorEvent(Event event, RowContainer container) { + int type = event.getTypeInt(); + boolean editorIsActive = editor.getState() != Editor.State.INACTIVE; - if (editor.getState() != Editor.State.INACTIVE) { - if (event.getTypeInt() == Event.ONKEYDOWN + if (editorIsActive) { + // React to closing by keyboard in buffered and unbuffered mode + if (type == Event.ONKEYDOWN && event.getKeyCode() == Editor.KEYCODE_HIDE) { editor.cancel(); + return true; + } + // Swallow all other events in buffered mode and everything except + // ONCLICK in unbuffered mode + if (editor.isBuffered() || type != Event.ONCLICK) { + return true; } - return true; } if (container == escalator.getBody() && editor.isEnabled()) { - boolean wasOpen = editor.getState() != Editor.State.INACTIVE; boolean opened = false; - if (event.getTypeInt() == Event.ONDBLCLICK) { + if (editorIsActive && !editor.isBuffered() && type == Event.ONCLICK) { + editor.hide(); + cellFocusHandler.setCellFocus(eventCell); + editor.editRow(eventCell.getRowIndex()); + opened = true; + } else if (type == Event.ONDBLCLICK) { editor.editRow(eventCell.getRowIndex()); opened = true; - } else if (event.getTypeInt() == Event.ONKEYDOWN + } else if (type == Event.ONKEYDOWN && event.getKeyCode() == Editor.KEYCODE_SHOW) { editor.editRow(cellFocusHandler.rowWithFocus); opened = true; } if (opened) { - if (wasOpen) { + if (editorIsActive) { fireEvent(new EditorMoveEvent(eventCell)); } else { fireEvent(new EditorOpenEvent(eventCell)); diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index fc5adbbff0..43e82560df 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -3930,13 +3930,13 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, boolean success = false; try { Object id = getContainerDataSource().getIdByIndex(rowIndex); - if (editedItemId == null) { + if (!isEditorBuffered() || editedItemId == null) { editedItemId = id; } if (editedItemId.equals(id)) { - success = true; doEditItem(); + success = true; } } catch (Exception e) { handleError(e); diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorBufferedTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorBufferedTest.java new file mode 100644 index 0000000000..49a5174f8d --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorBufferedTest.java @@ -0,0 +1,153 @@ +/* + * 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.tests.components.grid.basicfeatures.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.shared.ui.grid.GridConstants; +import com.vaadin.testbench.elements.GridElement.GridEditorElement; +import com.vaadin.testbench.elements.NotificationElement; + +public class GridEditorBufferedTest extends GridEditorTest { + + @Override + @Before + public void setUp() { + super.setUp(); + } + + @Test + public void testSave() { + selectMenuPath(EDIT_ITEM_100); + + WebElement textField = getEditorWidgets().get(0); + + textField.click(); + + textField.sendKeys(" changed"); + + WebElement saveButton = getEditor().findElement( + By.className("v-grid-editor-save")); + + saveButton.click(); + + assertEquals("(100, 0) changed", getGridElement().getCell(100, 0) + .getText()); + } + + @Test + public void testProgrammaticSave() { + selectMenuPath(EDIT_ITEM_100); + + WebElement textField = getEditorWidgets().get(0); + + textField.click(); + + textField.sendKeys(" changed"); + + selectMenuPath("Component", "Editor", "Save"); + + assertEquals("(100, 0) changed", getGridElement().getCell(100, 0) + .getText()); + } + + @Test + public void testInvalidEdition() { + selectMenuPath(EDIT_ITEM_5); + assertFalse(logContainsText("Exception occured, java.lang.IllegalStateException")); + + GridEditorElement editor = getGridElement().getEditor(); + + assertFalse( + "Field 7 should not have been marked with an error before error", + editor.isFieldErrorMarked(7)); + + WebElement intField = editor.getField(7); + intField.clear(); + intField.sendKeys("banana phone"); + editor.save(); + + assertEquals("Column 7: Could not convert value to Integer", + editor.getErrorMessage()); + assertTrue("Field 7 should have been marked with an error after error", + editor.isFieldErrorMarked(7)); + editor.cancel(); + + selectMenuPath(EDIT_ITEM_100); + assertFalse("Exception should not exist", + isElementPresent(NotificationElement.class)); + assertEquals("There should be no editor error message", null, + editor.getErrorMessage()); + } + + @Test + public void testEditorInDisabledGrid() { + int originalScrollPos = getGridVerticalScrollPos(); + + selectMenuPath(EDIT_ITEM_5); + assertEditorOpen(); + + selectMenuPath("Component", "State", "Enabled"); + assertEditorOpen(); + + GridEditorElement editor = getGridElement().getEditor(); + editor.save(); + assertEditorOpen(); + + editor.cancel(); + assertEditorOpen(); + + selectMenuPath("Component", "State", "Enabled"); + + scrollGridVerticallyTo(100); + assertEquals("Grid shouldn't scroll vertically while editing", + originalScrollPos, getGridVerticalScrollPos()); + } + + @Test + public void testCaptionChange() { + selectMenuPath(EDIT_ITEM_5); + assertEquals("Save button caption should've been \"" + + GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with", + GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText()); + assertEquals("Cancel button caption should've been \"" + + GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with", + GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton() + .getText()); + + selectMenuPath("Component", "Editor", "Change save caption"); + assertNotEquals( + "Save button caption should've changed while editor is open", + GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText()); + + getCancelButton().click(); + + selectMenuPath("Component", "Editor", "Change cancel caption"); + selectMenuPath(EDIT_ITEM_5); + assertNotEquals( + "Cancel button caption should've changed while editor is closed", + GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton() + .getText()); + } +} diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorTest.java index 8a8792c351..b32d50024d 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorTest.java @@ -17,7 +17,6 @@ package com.vaadin.tests.components.grid.basicfeatures.server; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -32,23 +31,18 @@ import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; -import com.vaadin.shared.ui.grid.GridConstants; import com.vaadin.testbench.elements.GridElement.GridCellElement; -import com.vaadin.testbench.elements.GridElement.GridEditorElement; -import com.vaadin.testbench.elements.NotificationElement; import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures; import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest; -public class GridEditorTest extends GridBasicFeaturesTest { +public abstract class GridEditorTest extends GridBasicFeaturesTest { - private static final String[] EDIT_ITEM_5 = new String[] { "Component", + protected static final String[] EDIT_ITEM_5 = new String[] { "Component", "Editor", "Edit item 5" }; - private static final String[] EDIT_ITEM_100 = new String[] { "Component", + protected static final String[] EDIT_ITEM_100 = new String[] { "Component", "Editor", "Edit item 100" }; - private static final String[] TOGGLE_EDIT_ENABLED = new String[] { + protected static final String[] TOGGLE_EDIT_ENABLED = new String[] { "Component", "Editor", "Enabled" }; - private static final String[] TOGGLE_EDITOR_BUFFERED_ENABLED = new String[] { - "Component", "Editor", "Buffered mode" }; @Before public void setUp() { @@ -129,113 +123,23 @@ public class GridEditorTest extends GridBasicFeaturesTest { assertEquals("<b>100</b>", widgets.get(8).getAttribute("value")); } - @Test - public void testSave() { - selectMenuPath(EDIT_ITEM_100); - - WebElement textField = getEditorWidgets().get(0); - - textField.click(); - - textField.sendKeys(" changed"); - - WebElement saveButton = getEditor().findElement( - By.className("v-grid-editor-save")); - - saveButton.click(); - - assertEquals("(100, 0) changed", getGridElement().getCell(100, 0) - .getText()); - } - - @Test - public void testProgrammaticSave() { - selectMenuPath(EDIT_ITEM_100); - - WebElement textField = getEditorWidgets().get(0); - - textField.click(); - - textField.sendKeys(" changed"); - - selectMenuPath("Component", "Editor", "Save"); - - assertEquals("(100, 0) changed", getGridElement().getCell(100, 0) - .getText()); - } - - @Test - public void testCaptionChange() { - selectMenuPath(EDIT_ITEM_5); - assertEquals("Save button caption should've been \"" - + GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with", - GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText()); - assertEquals("Cancel button caption should've been \"" - + GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with", - GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton() - .getText()); - - selectMenuPath("Component", "Editor", "Change save caption"); - assertNotEquals( - "Save button caption should've changed while editor is open", - GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText()); - - getCancelButton().click(); - - selectMenuPath("Component", "Editor", "Change cancel caption"); - selectMenuPath(EDIT_ITEM_5); - assertNotEquals( - "Cancel button caption should've changed while editor is closed", - GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton() - .getText()); - } - - private void assertEditorOpen() { + protected void assertEditorOpen() { assertNotNull("Editor is supposed to be open", getEditor()); assertEquals("Unexpected number of widgets", GridBasicFeatures.EDITABLE_COLUMNS, getEditorWidgets().size()); } - private void assertEditorClosed() { + protected void assertEditorClosed() { assertNull("Editor is supposed to be closed", getEditor()); } - private List<WebElement> getEditorWidgets() { + protected List<WebElement> getEditorWidgets() { assertNotNull(getEditor()); return getEditor().findElements(By.className("v-textfield")); } @Test - public void testInvalidEdition() { - selectMenuPath(EDIT_ITEM_5); - assertFalse(logContainsText("Exception occured, java.lang.IllegalStateException")); - - GridEditorElement editor = getGridElement().getEditor(); - - assertFalse( - "Field 7 should not have been marked with an error before error", - editor.isFieldErrorMarked(7)); - - WebElement intField = editor.getField(7); - intField.clear(); - intField.sendKeys("banana phone"); - editor.save(); - - assertEquals("Column 7: Could not convert value to Integer", - editor.getErrorMessage()); - assertTrue("Field 7 should have been marked with an error after error", - editor.isFieldErrorMarked(7)); - editor.cancel(); - - selectMenuPath(EDIT_ITEM_100); - assertFalse("Exception should not exist", - isElementPresent(NotificationElement.class)); - assertEquals("There should be no editor error message", null, - editor.getErrorMessage()); - } - - @Test public void testNoScrollAfterEditByAPI() { int originalScrollPos = getGridVerticalScrollPos(); @@ -272,30 +176,6 @@ public class GridEditorTest extends GridBasicFeaturesTest { } @Test - public void testEditorInDisabledGrid() { - int originalScrollPos = getGridVerticalScrollPos(); - - selectMenuPath(EDIT_ITEM_5); - assertEditorOpen(); - - selectMenuPath("Component", "State", "Enabled"); - assertEditorOpen(); - - GridEditorElement editor = getGridElement().getEditor(); - editor.save(); - assertEditorOpen(); - - editor.cancel(); - assertEditorOpen(); - - selectMenuPath("Component", "State", "Enabled"); - - scrollGridVerticallyTo(100); - assertEquals("Grid shouldn't scroll vertically while editing", - originalScrollPos, getGridVerticalScrollPos()); - } - - @Test public void testUneditableColumn() { selectMenuPath(EDIT_ITEM_5); assertEditorOpen(); @@ -304,46 +184,11 @@ public class GridEditorTest extends GridBasicFeaturesTest { getGridElement().getEditor().isEditable(3)); } - @Test - public void testEditorUnbufferedShowsNoButtons() { - selectMenuPath(TOGGLE_EDITOR_BUFFERED_ENABLED); - selectMenuPath(EDIT_ITEM_5); - - assertEditorOpen(); - - boolean saveButtonFound = true; - try { - getSaveButton(); - } catch (NoSuchElementException e) { - saveButtonFound = false; - } - assertFalse("Save button should not be visible in unbuffered mode.", - saveButtonFound); - - boolean cancelButtonFound = true; - try { - getCancelButton(); - } catch (NoSuchElementException e) { - cancelButtonFound = false; - } - assertFalse("Cancel button should not be visible in unbuffered mode.", - cancelButtonFound); - } - - @Test - public void testEditorUnbufferedWhileOpen() { - selectMenuPath(EDIT_ITEM_5); - selectMenuPath(TOGGLE_EDITOR_BUFFERED_ENABLED); - assertEditorOpen(); - boolean thrown = logContainsText("Exception occured, java.lang.IllegalStateException"); - assertTrue("IllegalStateException thrown", thrown); - } - - private WebElement getSaveButton() { + protected WebElement getSaveButton() { return getDriver().findElement(By.className("v-grid-editor-save")); } - private WebElement getCancelButton() { + protected WebElement getCancelButton() { return getDriver().findElement(By.className("v-grid-editor-cancel")); } } diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorUnbufferedTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorUnbufferedTest.java new file mode 100644 index 0000000000..821239f1dc --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorUnbufferedTest.java @@ -0,0 +1,88 @@ +/* + * 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.tests.components.grid.basicfeatures.server; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.NoSuchElementException; + +public class GridEditorUnbufferedTest extends GridEditorTest { + + private static final String[] TOGGLE_EDITOR_BUFFERED = new String[] { + "Component", "Editor", "Buffered mode" }; + + @Override + @Before + public void setUp() { + super.setUp(); + selectMenuPath(TOGGLE_EDITOR_BUFFERED); + } + + @Test + public void testEditorShowsNoButtons() { + selectMenuPath(EDIT_ITEM_5); + + assertEditorOpen(); + + boolean saveButtonFound = true; + try { + getSaveButton(); + } catch (NoSuchElementException e) { + saveButtonFound = false; + } + assertFalse("Save button should not be visible in unbuffered mode.", + saveButtonFound); + + boolean cancelButtonFound = true; + try { + getCancelButton(); + } catch (NoSuchElementException e) { + cancelButtonFound = false; + } + assertFalse("Cancel button should not be visible in unbuffered mode.", + cancelButtonFound); + } + + @Test + public void testToggleEditorUnbufferedWhileOpen() { + selectMenuPath(EDIT_ITEM_5); + assertEditorOpen(); + selectMenuPath(TOGGLE_EDITOR_BUFFERED); + boolean thrown = logContainsText("Exception occured, java.lang.IllegalStateException"); + assertTrue("IllegalStateException thrown", thrown); + } + + @Test + public void testEditorMove() { + selectMenuPath(EDIT_ITEM_5); + + assertEditorOpen(); + + String firstFieldValue = getEditorWidgets().get(0) + .getAttribute("value"); + assertTrue("Editor is not at correct row index (5)", + "(5, 0)".equals(firstFieldValue)); + + getGridElement().getCell(10, 0).click(); + firstFieldValue = getEditorWidgets().get(0).getAttribute("value"); + + assertTrue("Editor is not at correct row index (10)", + "(10, 0)".equals(firstFieldValue)); + } +} |