]> source.dussan.org Git - vaadin-framework.git/commitdiff
Makes it possible to change save/cancel captions in Grid editor (#16551)
authorHenrik Paul <henrik@vaadin.com>
Mon, 2 Feb 2015 14:51:49 +0000 (16:51 +0200)
committerVaadin Code Review <review@vaadin.com>
Thu, 5 Feb 2015 06:50:44 +0000 (06:50 +0000)
Change-Id: I4e303613f66a13b3ad6a9b2284537e5548391a4a

client/src/com/vaadin/client/widgets/Grid.java
server/src/com/vaadin/ui/Grid.java
shared/src/com/vaadin/shared/ui/grid/GridConstants.java
shared/src/com/vaadin/shared/ui/grid/GridState.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridEditorClientTest.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorTest.java
uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java

index e112d868b4bc36faefdf43e8804d784fd91e8038..aee1c1fcc88eda41ca06be721c8bfd869dc3bda9 100644 (file)
@@ -971,8 +971,8 @@ public class Grid<T> 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<T> 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<T> 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<T> extends ResizeComposite implements
             }
             columnToWidget.clear();
 
+            detachWidget(saveButton);
+            detachWidget(cancelButton);
+
             editorOverlay.removeAllChildren();
             editorOverlay.removeFromParent();
 
@@ -1371,9 +1375,14 @@ public class Grid<T> 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<T> 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<T> 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<HANDLER extends AbstractGridKeyEventHandler>
@@ -5825,6 +5865,50 @@ public class Grid<T> 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();
index 458522f58e68cc1c12f12f03be88401cd75fbfa8..f3c3bfa26b94848304e858fe89d7de82dbe70c9a 100644 (file)
@@ -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,
index b36a1624760ed63aafdd594ee35d09d62f97f2cb..0606e4b1ccabfbfa37d0faaae580b679f53e0f1f 100644 (file)
@@ -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";
 }
index 2b18d5b642a59956ea8befc6b48ada2c754030e3..9d94a2cb8e9e8ead137f0db6f6ac0281c2f21837 100644 (file)
@@ -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;
 }
index 7a625e2f25476e75649882a52641dff6a82191ac..89fff6871b37524f74841e5ff1e0cd70ed3e41d7 100644 (file)
@@ -960,6 +960,22 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> {
                 c.cancelEditor();
             }
         }, null);
+
+        createClickAction("Change save caption", "Editor",
+                new Command<Grid, String>() {
+                    @Override
+                    public void execute(Grid c, String value, Object data) {
+                        c.setEditorSaveCaption("ǝʌɐS");
+                    }
+                }, null);
+
+        createClickAction("Change cancel caption", "Editor",
+                new Command<Grid, String>() {
+                    @Override
+                    public void execute(Grid c, String value, Object data) {
+                        c.setEditorCancelCaption("ʃǝɔuɐↃ");
+                    }
+                }, null);
     }
 
     @SuppressWarnings("boxing")
index a67b90119889709e6c03bfa44a7eb1538588b14b..a81d374167bb0dcfd84680d0906617c8d662e605 100644 (file)
@@ -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<WebElement> 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<WebElement> 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"));
+    }
+
 }
index c6eb8d042bd187a9ea7e42dfd5042e6b5bfe1577..61dbfbc7c93602c90864c209a1f49619ec6cac9e 100644 (file)
@@ -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"));
+    }
 }
index 25a04b88f951d2122a0056d480747281fb40644b..7a635ad420d8f53b856675aeeeed2cbc7d9371af 100644 (file)
@@ -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) {