From: Henrik Paul Date: Mon, 2 Feb 2015 14:51:49 +0000 (+0200) Subject: Makes it possible to change save/cancel captions in Grid editor (#16551) X-Git-Tag: 7.5.0.alpha1~143 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=323e8bb4588fcb4eefa6e988cceb8444f963d733;p=vaadin-framework.git Makes it possible to change save/cancel captions in Grid editor (#16551) Change-Id: I4e303613f66a13b3ad6a9b2284537e5548391a4a --- diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java index e112d868b4..aee1c1fcc8 100644 --- a/client/src/com/vaadin/client/widgets/Grid.java +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -971,8 +971,8 @@ public class Grid extends ResizeComposite implements private HandlerRegistration scrollHandler; - private Button saveButton; - private Button cancelButton; + private final Button saveButton; + private final Button cancelButton; private static final int SAVE_TIMEOUT_MS = 5000; private final Timer saveTimeout = new Timer() { @@ -1053,6 +1053,28 @@ public class Grid extends ResizeComposite implements } }; + public Editor() { + saveButton = new Button(); + saveButton.setText(GridConstants.DEFAULT_SAVE_CAPTION); + saveButton.setStylePrimaryName("v-nativebutton"); + saveButton.addClickHandler(new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + save(); + } + }); + + cancelButton = new Button(); + cancelButton.setText(GridConstants.DEFAULT_CANCEL_CAPTION); + cancelButton.setStylePrimaryName("v-nativebutton"); + cancelButton.addClickHandler(new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + cancel(); + } + }); + } + public int getRow() { return rowIndex; } @@ -1295,28 +1317,7 @@ public class Grid extends ResizeComposite implements } } - saveButton = new Button(); - saveButton.setText("Save"); - saveButton.setStylePrimaryName("v-nativebutton"); - saveButton.addStyleName(styleName + "-save"); - saveButton.addClickHandler(new ClickHandler() { - @Override - public void onClick(ClickEvent event) { - save(); - } - }); attachWidget(saveButton, editorOverlay); - - cancelButton = new Button(); - cancelButton.setText("Cancel"); - cancelButton.setStylePrimaryName("v-nativebutton"); - cancelButton.addStyleName(styleName + "-cancel"); - cancelButton.addClickHandler(new ClickHandler() { - @Override - public void onClick(ClickEvent event) { - cancel(); - } - }); attachWidget(cancelButton, editorOverlay); /* @@ -1362,6 +1363,9 @@ public class Grid extends ResizeComposite implements } columnToWidget.clear(); + detachWidget(saveButton); + detachWidget(cancelButton); + editorOverlay.removeAllChildren(); editorOverlay.removeFromParent(); @@ -1371,9 +1375,14 @@ public class Grid extends ResizeComposite implements protected void setStylePrimaryName(String primaryName) { if (styleName != null) { editorOverlay.removeClassName(styleName); + saveButton.removeStyleName(styleName + "-save"); + cancelButton.removeStyleName(styleName + "-cancel"); } styleName = primaryName + "-editor"; editorOverlay.addClassName(styleName); + + saveButton.addStyleName(styleName + "-save"); + cancelButton.addStyleName(styleName + "-cancel"); } /** @@ -1401,6 +1410,11 @@ public class Grid extends ResizeComposite implements setParent(w, grid); } + private void detachWidget(Widget w) { + setParent(w, null); + w.getElement().removeFromParent(); + } + private static void setBounds(Element e, double left, double top, double width, double height) { Style style = e.getStyle(); @@ -1431,6 +1445,32 @@ public class Grid extends ResizeComposite implements saveButton.setEnabled(enabled); cancelButton.setEnabled(enabled); } + + public void setSaveCaption(String saveCaption) + throws IllegalArgumentException { + if (saveCaption == null) { + throw new IllegalArgumentException( + "Save caption cannot be null"); + } + saveButton.setText(saveCaption); + } + + public String getSaveCaption() { + return saveButton.getText(); + } + + public void setCancelCaption(String cancelCaption) + throws IllegalArgumentException { + if (cancelCaption == null) { + throw new IllegalArgumentException( + "Cancel caption cannot be null"); + } + cancelButton.setText(cancelCaption); + } + + public String getCancelCaption() { + return cancelButton.getText(); + } } public static abstract class AbstractGridKeyEvent @@ -5825,6 +5865,50 @@ public class Grid extends ResizeComposite implements return editor.getWidget(column); } + /** + * Sets the caption on the save button in the Grid editor. + * + * @param saveCaption + * the caption to set + * @throws IllegalArgumentException + * if {@code saveCaption} is {@code null} + */ + public void setEditorSaveCaption(String saveCaption) + throws IllegalArgumentException { + editor.setSaveCaption(saveCaption); + } + + /** + * Gets the current caption on the save button in the Grid editor. + * + * @return the current caption on the save button + */ + public String getEditorSaveCaption() { + return editor.getSaveCaption(); + } + + /** + * Sets the caption on the cancel button in the Grid editor. + * + * @param cancelCaption + * the caption to set + * @throws IllegalArgumentException + * if {@code cancelCaption} is {@code null} + */ + public void setEditorCancelCaption(String cancelCaption) + throws IllegalArgumentException { + editor.setCancelCaption(cancelCaption); + } + + /** + * Gets the caption on the cancel button in the Grid editor. + * + * @return the current caption on the cancel button + */ + public String getEditorCancelCaption() { + return editor.getCancelCaption(); + } + @Override protected void onAttach() { super.onAttach(); diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 458522f58e..f3c3bfa26b 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -4862,6 +4862,56 @@ public class Grid extends AbstractComponent implements SelectionNotifier, return editorFieldGroup.getFieldFactory(); } + /** + * Sets the caption on the save button in the Grid editor. + * + * @param saveCaption + * the caption to set + * @throws IllegalArgumentException + * if {@code saveCaption} is {@code null} + */ + public void setEditorSaveCaption(String saveCaption) + throws IllegalArgumentException { + if (saveCaption == null) { + throw new IllegalArgumentException("Save caption cannot be null"); + } + getState().editorSaveCaption = saveCaption; + } + + /** + * Gets the current caption of the save button in the Grid editor. + * + * @return the current caption of the save button + */ + public String getEditorSaveCaption() { + return getState(false).editorSaveCaption; + } + + /** + * Sets the caption on the cancel button in the Grid editor. + * + * @param cancelCaption + * the caption to set + * @throws IllegalArgumentException + * if {@code cancelCaption} is {@code null} + */ + public void setEditorCancelCaption(String cancelCaption) + throws IllegalArgumentException { + if (cancelCaption == null) { + throw new IllegalArgumentException("Cancel caption cannot be null"); + } + getState().editorCancelCaption = cancelCaption; + } + + /** + * Gets the current caption of the cancel button in the Grid editor. + * + * @return the current caption of the cancel button + */ + public String getCancelCaption() { + return getState(false).editorCancelCaption; + } + @Override public void addItemClickListener(ItemClickListener listener) { addListener(GridConstants.ITEM_CLICK_EVENT_ID, ItemClickEvent.class, diff --git a/shared/src/com/vaadin/shared/ui/grid/GridConstants.java b/shared/src/com/vaadin/shared/ui/grid/GridConstants.java index b36a162476..0606e4b1cc 100644 --- a/shared/src/com/vaadin/shared/ui/grid/GridConstants.java +++ b/shared/src/com/vaadin/shared/ui/grid/GridConstants.java @@ -68,4 +68,10 @@ public final class GridConstants implements Serializable { * Event ID for item click events */ public static final String ITEM_CLICK_EVENT_ID = "itemClick"; + + /** The default save button caption in the editor */ + public static final String DEFAULT_SAVE_CAPTION = "Save"; + + /** The default cancel button caption in the editor */ + public static final String DEFAULT_CANCEL_CAPTION = "Cancel"; } diff --git a/shared/src/com/vaadin/shared/ui/grid/GridState.java b/shared/src/com/vaadin/shared/ui/grid/GridState.java index 2b18d5b642..9d94a2cb8e 100644 --- a/shared/src/com/vaadin/shared/ui/grid/GridState.java +++ b/shared/src/com/vaadin/shared/ui/grid/GridState.java @@ -146,4 +146,11 @@ public class GridState extends AbstractComponentState { /** Whether row data might contain generated cell styles */ public boolean hasCellStyleGenerator; + /** The caption for the save button in the editor */ + @DelegateToWidget + public String editorSaveCaption = GridConstants.DEFAULT_SAVE_CAPTION; + + /** The caption for the cancel button in the editor */ + @DelegateToWidget + public String editorCancelCaption = GridConstants.DEFAULT_CANCEL_CAPTION; } diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java index 7a625e2f25..89fff6871b 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java @@ -960,6 +960,22 @@ public class GridBasicFeatures extends AbstractComponentTest { c.cancelEditor(); } }, null); + + createClickAction("Change save caption", "Editor", + new Command() { + @Override + public void execute(Grid c, String value, Object data) { + c.setEditorSaveCaption("ǝʌɐS"); + } + }, null); + + createClickAction("Change cancel caption", "Editor", + new Command() { + @Override + public void execute(Grid c, String value, Object data) { + c.setEditorCancelCaption("ʃǝɔuɐↃ"); + } + }, null); } @SuppressWarnings("boxing") diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridEditorClientTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridEditorClientTest.java index a67b901198..a81d374167 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridEditorClientTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridEditorClientTest.java @@ -17,6 +17,7 @@ package com.vaadin.tests.components.grid.basicfeatures.client; 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; @@ -31,11 +32,17 @@ 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.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest; import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures; public class GridEditorClientTest extends GridBasicClientFeaturesTest { + private static final String[] EDIT_ROW_100 = new String[] { "Component", + "Editor", "Edit row 100" }; + private static final String[] EDIT_ROW_5 = new String[] { "Component", + "Editor", "Edit row 5" }; + @Before public void setUp() { openTestURL(); @@ -44,7 +51,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest { @Test public void testProgrammaticOpeningClosing() { - selectMenuPath("Component", "Editor", "Edit row 5"); + selectMenuPath(EDIT_ROW_5); assertNotNull(getEditor()); selectMenuPath("Component", "Editor", "Cancel edit"); @@ -55,13 +62,13 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest { @Test public void testProgrammaticOpeningWithScroll() { - selectMenuPath("Component", "Editor", "Edit row 100"); + selectMenuPath(EDIT_ROW_100); assertNotNull(getEditor()); } @Test(expected = NoSuchElementException.class) public void testVerticalScrollLocking() { - selectMenuPath("Component", "Editor", "Edit row 5"); + selectMenuPath(EDIT_ROW_5); getGridElement().getCell(200, 0); } @@ -89,7 +96,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest { @Test public void testWidgetBinding() throws Exception { - selectMenuPath("Component", "Editor", "Edit row 100"); + selectMenuPath(EDIT_ROW_100); WebElement editor = getEditor(); List widgets = editor.findElements(By @@ -108,7 +115,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest { @Test public void testWithSelectionColumn() throws Exception { selectMenuPath("Component", "State", "Selection mode", "multi"); - selectMenuPath("Component", "State", "Editor", "Edit row 5"); + selectMenuPath(EDIT_ROW_5); WebElement editor = getEditor(); List selectorDivs = editor.findElements(By @@ -122,7 +129,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest { @Test public void testSave() { - selectMenuPath("Component", "Editor", "Edit row 100"); + selectMenuPath(EDIT_ROW_100); WebElement textField = getEditor().findElements( By.className("gwt-TextBox")).get(0); @@ -140,7 +147,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest { @Test public void testProgrammaticSave() { - selectMenuPath("Component", "Editor", "Edit row 100"); + selectMenuPath(EDIT_ROW_100); WebElement textField = getEditor().findElements( By.className("gwt-TextBox")).get(0); @@ -152,4 +159,39 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest { assertEquals("Changed", getGridElement().getCell(100, 0).getText()); } + + @Test + public void testCaptionChange() { + selectMenuPath(EDIT_ROW_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_ROW_5); + assertNotEquals( + "Cancel button caption should've changed while editor is closed", + GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton() + .getText()); + } + + protected WebElement getSaveButton() { + return getEditor().findElement(By.className("v-grid-editor-save")); + } + + protected WebElement getCancelButton() { + return getEditor().findElement(By.className("v-grid-editor-cancel")); + } + } 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 c6eb8d042b..61dbfbc7c9 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,6 +17,7 @@ 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; @@ -31,6 +32,7 @@ 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; @@ -160,6 +162,32 @@ public class GridEditorTest extends GridBasicFeaturesTest { .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() { assertNotNull("Editor is supposed to be open", getEditor()); assertEquals("Unexpected number of widgets", GridBasicFeatures.COLUMNS, @@ -257,4 +285,12 @@ public class GridEditorTest extends GridBasicFeaturesTest { assertEquals("Grid shouldn't scroll vertically while editing", originalScrollPos, getGridVerticalScrollPos()); } + + private WebElement getSaveButton() { + return getDriver().findElement(By.className("v-grid-editor-save")); + } + + private WebElement getCancelButton() { + return getDriver().findElement(By.className("v-grid-editor-cancel")); + } } diff --git a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java index 25a04b88f9..7a635ad420 100644 --- a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java +++ b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java @@ -956,6 +956,20 @@ public class GridBasicClientFeaturesWidget extends } }, "Component", "Editor"); + addMenuCommand("Change Save Caption", new ScheduledCommand() { + @Override + public void execute() { + grid.setEditorSaveCaption("ǝʌɐS"); + } + }, "Component", "Editor"); + + addMenuCommand("Change Cancel Caption", new ScheduledCommand() { + @Override + public void execute() { + grid.setEditorCancelCaption("ʃǝɔuɐↃ"); + } + }, "Component", "Editor"); + } private void configureFooterRow(final FooterRow row) {