From 5b965ff478ef22a83a476eeebdf70554a776bae6 Mon Sep 17 00:00:00 2001 From: Ilya Ermakov Date: Fri, 5 Dec 2014 17:54:52 +0300 Subject: [PATCH] Table.setVisibleColumns() causes table to forget column headers, icons, alignment (#6245) Effect of this patch: when making column invisible and visible again, column headers, icons, alignment are preserved. Change-Id: Ia0718699f1a5fb8f60fec25a835ee64c58ca5404 --- server/src/com/vaadin/ui/Table.java | 20 ---- .../table/TableToggleColumnVisibility.java | 96 +++++++++++++++++++ .../TableToggleColumnVisibilityTest.java | 77 +++++++++++++++ 3 files changed, 173 insertions(+), 20 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/table/TableToggleColumnVisibility.java create mode 100644 uitest/src/com/vaadin/tests/components/table/TableToggleColumnVisibilityTest.java diff --git a/server/src/com/vaadin/ui/Table.java b/server/src/com/vaadin/ui/Table.java index cb61fa31ec..42c4beab6c 100644 --- a/server/src/com/vaadin/ui/Table.java +++ b/server/src/com/vaadin/ui/Table.java @@ -676,26 +676,6 @@ public class Table extends AbstractSelect implements Action.Container, } } - // Removes alignments, icons and headers from hidden columns - if (this.visibleColumns != null) { - boolean disabledHere = disableContentRefreshing(); - try { - for (final Iterator i = this.visibleColumns.iterator(); i - .hasNext();) { - final Object col = i.next(); - if (!newVC.contains(col)) { - setColumnHeader(col, null); - setColumnAlignment(col, (Align) null); - setColumnIcon(col, null); - } - } - } finally { - if (disabledHere) { - enableContentRefreshing(false); - } - } - } - this.visibleColumns = newVC; // Assures visual refresh diff --git a/uitest/src/com/vaadin/tests/components/table/TableToggleColumnVisibility.java b/uitest/src/com/vaadin/tests/components/table/TableToggleColumnVisibility.java new file mode 100644 index 0000000000..e78f247184 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/TableToggleColumnVisibility.java @@ -0,0 +1,96 @@ +/* + * Copyright 2000-2014 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 com.vaadin.server.ClassResource; +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; + +/** + * Test that toggling column visibility does not change custom header, icon, + * alignment + */ +@SuppressWarnings("serial") +public class TableToggleColumnVisibility extends AbstractTestUI { + + private final Object[][] columnSets = new Object[][] { { "Name" }, + { "Name", "Last Name" }, { "Last Name", "Name" } }; + private int currentSetNumber = 1; + + @Override + protected void setup(VaadinRequest request) { + VerticalLayout layout = new VerticalLayout(); + + final Table table = new Table(); + + table.addContainerProperty("Name", String.class, ""); + table.addContainerProperty("Last Name", String.class, null, + "Hello World", new ClassResource("fi.gif"), Table.Align.RIGHT); + + table.setHeight("200px"); + + table.addItem(new Object[] { "Muoso", "Virtanen" }, new Integer(1)); + table.addItem(new Object[] { "Nehemias", "Korhonen" }, new Integer(2)); + table.addItem(new Object[] { "Hannu", "Nieminen" }, new Integer(3)); + table.addItem(new Object[] { "Eelante", "Mäkinen" }, new Integer(4)); + table.setVisibleColumns(columnSets[1]); + + final Button visibToggler = new Button("visibility"); + visibToggler.setId("visib-toggler"); + visibToggler.addClickListener(new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + currentSetNumber = (currentSetNumber == 0) ? 1 : 0; + table.setVisibleColumns(columnSets[currentSetNumber]); + } + }); + + final Button orderToggler = new Button("change order"); + orderToggler.setId("order-toggler"); + orderToggler.addClickListener(new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + currentSetNumber = (currentSetNumber == 1) ? 2 : 1; + table.setVisibleColumns(columnSets[currentSetNumber]); + } + }); + + layout.addComponent(table); + layout.addComponent(visibToggler); + layout.addComponent(orderToggler); + + addComponent(layout); + } + + @Override + protected String getTestDescription() { + return "Toggling visibility of column should not change custom header," + + " icon, alignment"; + } + + @Override + protected Integer getTicketNumber() { + return 6245; + } + +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/components/table/TableToggleColumnVisibilityTest.java b/uitest/src/com/vaadin/tests/components/table/TableToggleColumnVisibilityTest.java new file mode 100644 index 0000000000..4c44dcfed8 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/TableToggleColumnVisibilityTest.java @@ -0,0 +1,77 @@ +/* + * 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 org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Tests that column keeps its header, icon, alignment after toggling visibility + * (#6245). + * + * @author Vaadin Ltd + */ +public class TableToggleColumnVisibilityTest extends MultiBrowserTest { + + @Test + public void testColumnWidthRestoredAfterTogglingVisibility() { + openTestURL(); + + WebElement toggleVisibilityButton = findElement(By.id("visib-toggler")); + WebElement changeOrderButton = findElement(By.id("order-toggler")); + + checkHeaderAttributes(1); + + toggleVisibilityButton.click(); // hide column #1 + Assert.assertEquals("One column should be visible", + findElements(By.className("v-table-header-cell")).size(), 1); + + toggleVisibilityButton.click(); // restore column #1 + Assert.assertEquals("Two columns should be visible", + findElements(By.className("v-table-header-cell")).size(), 2); + checkHeaderAttributes(1); + + changeOrderButton.click(); // change column order, column #1 now becomes + // column #0 + checkHeaderAttributes(0); + + } + + /* + * Checks column header with number columnNumber. + */ + private void checkHeaderAttributes(int columnNumber) { + WebElement headerCell = findElements( + By.className("v-table-header-cell")).get(columnNumber); + + Assert.assertTrue("Column header text should be custom", headerCell + .getText().equalsIgnoreCase("Hello World")); + + Assert.assertTrue("Column should have an icon", headerCell + .findElements(By.className("v-icon")).size() > 0); + + Assert.assertTrue( + "Column should have alignment to the right", + headerCell.findElements( + By.className("v-table-caption-container-align-right")) + .size() > 0); + } + +} \ No newline at end of file -- 2.39.5