From 3e593b0949b2b26fdc89b2aa5e8e93b006a513d0 Mon Sep 17 00:00:00 2001 From: John Ahlroos Date: Tue, 1 Oct 2013 16:55:39 +0300 Subject: [PATCH] Focus selected row in Table #12540 Change-Id: Ic920f9cb11840a456a7c49746317eaccde1e1406 --- .../com/vaadin/client/ui/VScrollTable.java | 11 ++ .../table/TableMoveFocusWithSelection.java | 125 ++++++++++++++++++ .../TableMoveFocusWithSelectionTest.java | 75 +++++++++++ 3 files changed, 211 insertions(+) create mode 100644 uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelection.java create mode 100644 uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelectionTest.java diff --git a/client/src/com/vaadin/client/ui/VScrollTable.java b/client/src/com/vaadin/client/ui/VScrollTable.java index 9cec59a5a2..104cbbd5b9 100644 --- a/client/src/com/vaadin/client/ui/VScrollTable.java +++ b/client/src/com/vaadin/client/ui/VScrollTable.java @@ -1072,6 +1072,17 @@ public class VScrollTable extends FlowPanel implements HasWidgets, } if (selected != row.isSelected()) { row.toggleSelection(); + + if (selected) { + if (focusedRow == null + || !selectedRowKeys.contains(focusedRow + .getKey())) { + // The focus is no longer on a selected row, + // move focus to first selected row + setRowFocus(row); + } + } + if (!isSingleSelectMode() && !selected) { // Update selection range in case a row is // unselected from the middle of a range - #8076 diff --git a/uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelection.java b/uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelection.java new file mode 100644 index 0000000000..20170efa13 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelection.java @@ -0,0 +1,125 @@ +/* + * Copyright 2000-2013 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.tests.components.table; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import com.vaadin.event.LayoutEvents.LayoutClickEvent; +import com.vaadin.event.LayoutEvents.LayoutClickListener; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.Table; +import com.vaadin.ui.VerticalLayout; + +public class TableMoveFocusWithSelection extends AbstractTestUI { + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server. + * VaadinRequest) + */ + @Override + protected void setup(VaadinRequest request) { + final Table t = new Table(); + t.setImmediate(true); + t.setId("test-table"); + t.setSizeFull(); + t.setSelectable(true); + t.addContainerProperty("layout", VerticalLayout.class, null); + t.addContainerProperty("string", String.class, null); + + for (int i = 0; i < 100; i++) { + t.addItem(i); + final VerticalLayout l = new VerticalLayout(); + l.setId("row-" + i); + l.setHeight(20, Unit.PIXELS); + l.setData(i); + l.addLayoutClickListener(new LayoutClickListener() { + @Override + public void layoutClick(LayoutClickEvent event) { + if (t.isMultiSelect()) { + Set values = new HashSet( + (Set) t.getValue()); + values.add(l.getData()); + t.setValue(values); + } else { + t.setValue(l.getData()); + } + } + }); + t.getContainerProperty(i, "layout").setValue(l); + t.getContainerProperty(i, "string").setValue("Item #" + i); + } + addComponent(t); + + // Select mode + Button toggleSelectMode = new Button( + t.isMultiSelect() ? "Press to use single select" + : "Press to use multi select"); + toggleSelectMode.setId("toggle-mode"); + toggleSelectMode.addClickListener(new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + t.setMultiSelect(!t.isMultiSelect()); + + event.getButton().setCaption( + t.isMultiSelect() ? "Press to use single select" + : "Press to use multi select"); + } + }); + + addComponent(toggleSelectMode); + + Button select5210 = new Button("Select row 5-10", + new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + t.setValue(Arrays.asList(5, 6, 7, 8, 9, 10)); + } + }); + select5210.setId("select-510"); + addComponent(select5210); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTestDescription() + */ + @Override + protected String getTestDescription() { + return "Changing selection in single select mode should move focus"; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTicketNumber() + */ + @Override + protected Integer getTicketNumber() { + return 12540; + } + +} diff --git a/uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelectionTest.java b/uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelectionTest.java new file mode 100644 index 0000000000..b38705eeb6 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelectionTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 2000-2013 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.tests.components.table; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.By; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Tests if table focus is moved correctly to the selected item + * + * @since + * @author Vaadin Ltd + */ +public class TableMoveFocusWithSelectionTest extends MultiBrowserTest { + + @Test + public void selectUnfocusedTableAndAssumeSelectionGetsFocus() { + + openTestURL(); + + // Click on row 5 + getDriver().findElement(By.id("row-5")).click(); + + // Ensure row 5 gets focused + WebElement row5TableRow = getDriver().findElement( + By.xpath("//div[@id='row-5']/../../..")); + String row5StyleName = row5TableRow.getAttribute("class"); + assertTrue(row5StyleName.contains("v-table-focus")); + } + + @Test + public void focusShouldStayOnUserSelectedRowIfSelectionChangesServerSide() { + + openTestURL(); + + // Select multiselect + getDriver().findElement(By.id("toggle-mode")).click(); + + // Click on row 7 + getDriver().findElement(By.id("row-7")).click(); + + // Select row 5-10 server side + getDriver().findElement(By.id("select-510")).click(); + + // Ensure row 7 is still focused + WebElement row7TableRow = getDriver().findElement( + By.xpath("//div[@id='row-7']/../../..")); + String row7StyleName = row7TableRow.getAttribute("class"); + assertTrue(row7StyleName.contains("v-table-focus")); + } + + @Override + protected Class getUIClass() { + // FIXME Remove when this is done automatically + return TableMoveFocusWithSelection.class; + } +} -- 2.39.5