diff options
6 files changed, 144 insertions, 6 deletions
diff --git a/server/src/main/java/com/vaadin/ui/components/grid/Editor.java b/server/src/main/java/com/vaadin/ui/components/grid/Editor.java index 0eff213c7e..26ef8101ab 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/Editor.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/Editor.java @@ -177,6 +177,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. * * @return the grid which owns the editor diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java index 07b792a221..502f530d9a 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java @@ -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 @@ -346,6 +348,12 @@ public class EditorImpl<T> extends AbstractGridExtension<T> } @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 index 0000000000..70e0423991 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorOpenEvent.java @@ -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 index 0000000000..071ed0549a --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorOpenListener.java @@ -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); +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java index bd799a9925..41da1cc9fa 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java @@ -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); diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java index 43cfd66c34..753bd656d6 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java @@ -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)); } |