]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add editor open event to Grid Editor (#9623)
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>
Mon, 3 Jul 2017 09:45:53 +0000 (12:45 +0300)
committerHenri Sara <henri.sara@gmail.com>
Mon, 3 Jul 2017 09:45:53 +0000 (12:45 +0300)
Fixes #9596

server/src/main/java/com/vaadin/ui/components/grid/Editor.java
server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java
server/src/main/java/com/vaadin/ui/components/grid/EditorOpenEvent.java [new file with mode: 0644]
server/src/main/java/com/vaadin/ui/components/grid/EditorOpenListener.java [new file with mode: 0644]
uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java
uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java

index 0eff213c7ec5008e7bb91ce4373334a1cde6a0ac..26ef8101abe1a5696d8b795c5576d7e83f933b9d 100644 (file)
@@ -176,6 +176,17 @@ public interface Editor<T> extends Serializable {
      */
     public Registration addCancelListener(EditorCancelListener<T> listener);
 
+    /**
+     * Adds an editor open {@code listener}.
+     * 
+     * @param listener
+     *            open listener
+     * @return a registration object for removing the listener
+     * 
+     * @since 8.1
+     */
+    public Registration addOpenListener(EditorOpenListener<T> listener);
+
     /**
      * Gets the Grid instance which this editor belongs to.
      * 
index 07b792a221816805bf7f024eb212ca6c9ed8b182..502f530d9acfdb3392dc2fdb164b85b3d053a1a8 100644 (file)
@@ -238,6 +238,8 @@ public class EditorImpl<T> extends AbstractGridExtension<T>
                     getState().columnFields.put(getInternalIdForColumn(c),
                             component.getConnectorId());
                 });
+
+        eventRouter.fireEvent(new EditorOpenEvent<T>(this, edited));
     }
 
     @Override
@@ -345,6 +347,12 @@ public class EditorImpl<T> extends AbstractGridExtension<T>
                 EditorCancelListener.class.getDeclaredMethods()[0]);
     }
 
+    @Override
+    public Registration addOpenListener(EditorOpenListener<T> listener) {
+        return eventRouter.addListener(EditorOpenEvent.class, listener,
+                EditorOpenListener.class.getDeclaredMethods()[0]);
+    }
+
     @Override
     public Grid<T> getGrid() {
         return getParent();
diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorOpenEvent.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorOpenEvent.java
new file mode 100644 (file)
index 0000000..70e0423
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2000-2016 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.ui.components.grid;
+
+import java.util.EventObject;
+
+import com.vaadin.ui.Grid;
+
+/**
+ * An event that is fired when a Grid editor is opened.
+ *
+ * @author Vaadin Ltd
+ * @since 8.1
+ *
+ * @see EditorOpenListener
+ * @see Editor#addOpenListener(EditorOpenListener)
+ *
+ * @param <T>
+ *            the bean type
+ */
+public class EditorOpenEvent<T> extends EventObject {
+
+    private T bean;
+
+    /**
+     * Constructor for a editor open event.
+     *
+     * @param editor
+     *            the source of the event
+     * @param bean
+     *            the bean being edited
+     */
+    public EditorOpenEvent(Editor<T> editor, T bean) {
+        super(editor);
+        this.bean = bean;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public Editor<T> getSource() {
+        return (Editor<T>) super.getSource();
+    }
+
+    /**
+     * Gets the editors' grid.
+     *
+     * @return the editors' grid
+     */
+    public Grid<T> getGrid() {
+        return getSource().getGrid();
+    }
+
+    /**
+     * Gets the bean being edited.
+     *
+     * @return the bean being edited
+     */
+    public T getBean() {
+        return bean;
+    }
+}
diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorOpenListener.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorOpenListener.java
new file mode 100644 (file)
index 0000000..071ed05
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2000-2016 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.ui.components.grid;
+
+import java.io.Serializable;
+
+import com.vaadin.ui.Grid;
+
+/**
+ * An event listener for a {@link Grid} editor save events.
+ * 
+ * @author Vaadin Ltd
+ * @since 8.1
+ * 
+ * @param <T>
+ *            the bean type
+ *
+ * @see EditorOpenEvent
+ * @see Editor#addOpenListener(EditorOpenListener)
+ */
+@FunctionalInterface
+public interface EditorOpenListener<T> extends Serializable {
+
+    /**
+     * Called when the editor is opened.
+     * 
+     * @param event
+     *            open event
+     */
+    public void onEditorOpen(EditorOpenEvent<T> event);
+}
index bd799a9925ee875aae8c1d1345e85014ecc7cd56..41da1cc9fa49979a1b62101794d418ce43b8e1c9 100644 (file)
@@ -50,6 +50,7 @@ public class GridEditorEvents extends AbstractTestUIWithLog {
                 Person::getFirstName, Person::setFirstName);
         column.setEditorBinding(binding);
 
+        grid.getEditor().addOpenListener(event -> log("editor is opened"));
         grid.getEditor().addCancelListener(event -> log("editor is canceled"));
         grid.getEditor().addSaveListener(event -> log("editor is saved"));
         addComponent(grid);
index 43cfd66c34245c17e6aac203944925e02f64f2a6..753bd656d6b15feabab675521dd26329af4f7c92 100644 (file)
@@ -25,10 +25,6 @@ import com.vaadin.testbench.elements.GridElement;
 import com.vaadin.testbench.elements.GridElement.GridEditorElement;
 import com.vaadin.tests.tb3.MultiBrowserTest;
 
-/**
- * @author Vaadin Ltd
- *
- */
 public class GridEditorEventsTest extends MultiBrowserTest {
 
     @Test
@@ -45,13 +41,17 @@ public class GridEditorEventsTest extends MultiBrowserTest {
         GridEditorElement editor = updateField(index, grid, "foo");
         editor.save();
 
-        Assert.assertEquals((index * 2 + 1) + ". editor is saved",
+        Assert.assertEquals((index * 4 + 1) + ". editor is opened",
+                getLogRow(1));
+        Assert.assertEquals((index * 4 + 2) + ". editor is saved",
                 getLogRow(0));
 
         editor = updateField(index, grid, "bar");
         editor.cancel();
 
-        Assert.assertEquals((index * 2 + 2) + ". editor is canceled",
+        Assert.assertEquals((index * 4 + 3) + ". editor is opened",
+                getLogRow(1));
+        Assert.assertEquals((index * 4 + 4) + ". editor is canceled",
                 getLogRow(0));
     }