diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2015-10-13 18:07:55 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2015-10-14 13:46:13 +0000 |
commit | e6165e8e18f0aeea35071db60f82e7b1122c8253 (patch) | |
tree | 36dd2387c155f6b1237f18d5373de24d13fc97b7 | |
parent | a2b1860c163a26b92b8e2ca3ae227a27b75ca55c (diff) | |
download | vaadin-framework-e6165e8e18f0aeea35071db60f82e7b1122c8253.tar.gz vaadin-framework-e6165e8e18f0aeea35071db60f82e7b1122c8253.zip |
Support ContextClicks in empty areas of Table and TreeTable (#19130)
When the user clicks an empty area, the TableContextClickEvent will still
fire, but will have a null property.
Change-Id: I39303f1a72d072db09c4fe6df8141d6caf0764c2
5 files changed, 67 insertions, 12 deletions
diff --git a/client/src/com/vaadin/client/ui/table/TableConnector.java b/client/src/com/vaadin/client/ui/table/TableConnector.java index badfcf7d28..a14ff15481 100644 --- a/client/src/com/vaadin/client/ui/table/TableConnector.java +++ b/client/src/com/vaadin/client/ui/table/TableConnector.java @@ -86,13 +86,12 @@ public class TableConnector extends AbstractFieldConnector implements protected void sendContextClickEvent(ContextMenuEvent event) { EventTarget eventTarget = event.getNativeEvent().getEventTarget(); if (!Element.is(eventTarget)) { - super.sendContextClickEvent(event); return; } Element e = Element.as(eventTarget); Section section; - String colKey; + String colKey = null; String rowKey = null; if (getWidget().tFoot.getElement().isOrHasChild(e)) { section = Section.FOOTER; @@ -102,15 +101,21 @@ public class TableConnector extends AbstractFieldConnector implements section = Section.HEADER; HeaderCell w = WidgetUtil.findWidget(e, HeaderCell.class); colKey = w.getColKey(); - } else if (getWidget().scrollBody.getElement().isOrHasChild(e)) { - section = Section.BODY; - VScrollTableRow w = getScrollTableRow(e); - rowKey = w.getKey(); - colKey = getWidget().tHead.getHeaderCell( - getElementIndex(e, w.getElement())).getColKey(); } else { - super.sendContextClickEvent(event); - return; + section = Section.BODY; + if (getWidget().scrollBody.getElement().isOrHasChild(e)) { + VScrollTableRow w = getScrollTableRow(e); + /* + * if w is null because we've clicked on an empty area, we will + * let rowKey and colKey be null too, which will then lead to + * the server side returning a null object. + */ + if (w != null) { + rowKey = w.getKey(); + colKey = getWidget().tHead.getHeaderCell( + getElementIndex(e, w.getElement())).getColKey(); + } + } } MouseEventDetails details = MouseEventDetailsBuilder diff --git a/server/src/com/vaadin/ui/Table.java b/server/src/com/vaadin/ui/Table.java index cbc216661a..66fa257ac2 100644 --- a/server/src/com/vaadin/ui/Table.java +++ b/server/src/com/vaadin/ui/Table.java @@ -6420,7 +6420,8 @@ public class Table extends AbstractSelect implements Action.Container, /** * Returns the item id of context clicked row. * - * @return item id of clicked row; <code>null</code> if header or footer + * @return item id of clicked row; <code>null</code> if header, footer + * or empty area of Table */ public Object getItemId() { return itemId; @@ -6429,7 +6430,8 @@ public class Table extends AbstractSelect implements Action.Container, /** * Returns the property id of context clicked column. * - * @return property id + * @return property id; or <code>null</code> if we've clicked on the + * empty area of the Table */ public Object getPropertyId() { return propertyId; diff --git a/uitest/src/com/vaadin/tests/contextclick/TableContextClick.java b/uitest/src/com/vaadin/tests/contextclick/TableContextClick.java index d8816f4bdc..95fa85dc90 100644 --- a/uitest/src/com/vaadin/tests/contextclick/TableContextClick.java +++ b/uitest/src/com/vaadin/tests/contextclick/TableContextClick.java @@ -18,6 +18,9 @@ package com.vaadin.tests.contextclick; import com.vaadin.data.Item; import com.vaadin.shared.ui.table.TableConstants.Section; import com.vaadin.tests.util.PersonContainer; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Table; import com.vaadin.ui.Table.TableContextClickEvent; @@ -50,4 +53,18 @@ public class TableContextClick extends log("ContextClickEvent value: " + value + ", propertyId: " + propertyId + ", section: " + event.getSection()); } + + @Override + protected HorizontalLayout createContextClickControls() { + HorizontalLayout controls = super.createContextClickControls(); + controls.addComponent(new Button("Remove all content", + new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + testComponent.getContainerDataSource().removeAllItems(); + } + })); + return controls; + } } diff --git a/uitest/src/com/vaadin/tests/contextclick/TableContextClickTest.java b/uitest/src/com/vaadin/tests/contextclick/TableContextClickTest.java index e7e716a02f..bfd8862440 100644 --- a/uitest/src/com/vaadin/tests/contextclick/TableContextClickTest.java +++ b/uitest/src/com/vaadin/tests/contextclick/TableContextClickTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import org.junit.Test; +import com.vaadin.testbench.elements.ButtonElement; import com.vaadin.testbench.elements.TableElement; public class TableContextClickTest extends TableContextClickTestBase { @@ -64,4 +65,17 @@ public class TableContextClickTest extends TableContextClickTestBase { getLogRow(0)); } + @Test + public void testContextClickInEmptyTable() { + addOrRemoveTypedListener(); + + $(ButtonElement.class).caption("Remove all content").first().click(); + + contextClick($(TableElement.class).first(), 100, 100); + + assertEquals( + "1. ContextClickEvent value: , propertyId: null, section: BODY", + getLogRow(0)); + } + } diff --git a/uitest/src/com/vaadin/tests/contextclick/TreeTableContextClick.java b/uitest/src/com/vaadin/tests/contextclick/TreeTableContextClick.java index 7d995710c9..431f231cca 100644 --- a/uitest/src/com/vaadin/tests/contextclick/TreeTableContextClick.java +++ b/uitest/src/com/vaadin/tests/contextclick/TreeTableContextClick.java @@ -18,6 +18,9 @@ package com.vaadin.tests.contextclick; import com.vaadin.data.Item; import com.vaadin.shared.ui.table.TableConstants.Section; import com.vaadin.tests.util.PersonContainer; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Table.TableContextClickEvent; import com.vaadin.ui.TreeTable; @@ -29,6 +32,7 @@ public class TreeTableContextClick extends TreeTable treeTable = new TreeTable(); treeTable.setContainerDataSource(PersonContainer.createWithTestData()); treeTable.setFooterVisible(true); + treeTable.setHeight("400px"); return treeTable; } @@ -50,4 +54,17 @@ public class TreeTableContextClick extends + ", section: " + event.getSection()); } + @Override + protected HorizontalLayout createContextClickControls() { + HorizontalLayout controls = super.createContextClickControls(); + controls.addComponent(new Button("Remove all content", + new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + testComponent.getContainerDataSource().removeAllItems(); + } + })); + return controls; + } } |