]> source.dussan.org Git - vaadin-framework.git/commitdiff
Focus selected row in Table #12540
authorJohn Ahlroos <john@vaadin.com>
Tue, 1 Oct 2013 13:55:39 +0000 (16:55 +0300)
committerVaadin Code Review <review@vaadin.com>
Wed, 9 Oct 2013 11:30:08 +0000 (11:30 +0000)
Change-Id: Ic920f9cb11840a456a7c49746317eaccde1e1406

client/src/com/vaadin/client/ui/VScrollTable.java
uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelection.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/table/TableMoveFocusWithSelectionTest.java [new file with mode: 0644]

index 9cec59a5a2b28c7b8e5ec2f169b64fcad86bbf5f..104cbbd5b9249302bc00771a0dfce5f2ae402ad2 100644 (file)
@@ -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 (file)
index 0000000..20170ef
--- /dev/null
@@ -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<Object> values = new HashSet<Object>(
+                                (Set<Object>) 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 (file)
index 0000000..b38705e
--- /dev/null
@@ -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;
+    }
+}