From 5f42729e994821bbf84b1160083348c62b5353e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 19 Nov 2015 14:31:46 +0200 Subject: [PATCH] Add stylename to sortable Table header cells (#8219) Change-Id: I4774b4079f5c564fdc67e8cabf89278ad7cf2f6f --- .../com/vaadin/client/ui/VScrollTable.java | 12 ++++ .../table/DisabledSortingTableTest.java | 2 +- .../table/SortableHeaderStyles.java | 64 +++++++++++++++++++ .../table/SortableHeaderStylesTest.java | 51 +++++++++++++++ 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 uitest/src/com/vaadin/tests/components/table/SortableHeaderStyles.java create mode 100644 uitest/src/com/vaadin/tests/components/table/SortableHeaderStylesTest.java diff --git a/client/src/com/vaadin/client/ui/VScrollTable.java b/client/src/com/vaadin/client/ui/VScrollTable.java index 3b85356f86..5bd6ed390d 100644 --- a/client/src/com/vaadin/client/ui/VScrollTable.java +++ b/client/src/com/vaadin/client/ui/VScrollTable.java @@ -2808,6 +2808,12 @@ public class VScrollTable extends FlowPanel implements HasWidgets, public void setSortable(boolean b) { sortable = b; + /* + * Should in theory call updateStyleNames here, but that would just + * be a waste of time since this method is only called from + * updateCellsFromUIDL which immediately afterwards calls setAlign + * which also updates the style names. + */ } /** @@ -2881,6 +2887,10 @@ public class VScrollTable extends FlowPanel implements HasWidgets, setStyleName(primaryStyleName + "-header-cell"); } + if (sortable) { + addStyleName(primaryStyleName + "-header-sortable"); + } + final String ALIGN_PREFIX = primaryStyleName + "-caption-container-align-"; @@ -3648,6 +3658,8 @@ public class VScrollTable extends FlowPanel implements HasWidgets, c.setSortable(false); } + // The previous call to setSortable relies on c.setAlign calling + // c.updateStyleNames if (col.hasAttribute("align")) { c.setAlign(col.getStringAttribute("align").charAt(0)); } else { diff --git a/uitest/src/com/vaadin/tests/components/table/DisabledSortingTableTest.java b/uitest/src/com/vaadin/tests/components/table/DisabledSortingTableTest.java index bb15301eee..73a5e68da9 100644 --- a/uitest/src/com/vaadin/tests/components/table/DisabledSortingTableTest.java +++ b/uitest/src/com/vaadin/tests/components/table/DisabledSortingTableTest.java @@ -85,7 +85,7 @@ public class DisabledSortingTableTest extends MultiBrowserTest { waitUntil(new ExpectedCondition() { @Override public Boolean apply(WebDriver input) { - return className.equals(header.getAttribute("class")); + return header.getAttribute("class").contains(className); } @Override diff --git a/uitest/src/com/vaadin/tests/components/table/SortableHeaderStyles.java b/uitest/src/com/vaadin/tests/components/table/SortableHeaderStyles.java new file mode 100644 index 0000000000..a41f4248d9 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/SortableHeaderStyles.java @@ -0,0 +1,64 @@ +/* + * 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 java.util.Collection; + +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.util.PersonContainer; +import com.vaadin.ui.OptionGroup; +import com.vaadin.ui.Table; + +public class SortableHeaderStyles extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + getPage().getStyles().add( + ".v-table-header-sortable { font-weight: bold;}"); + + PersonContainer container = PersonContainer.createWithTestData(); + + Collection sortableContainerPropertyIds = container + .getSortableContainerPropertyIds(); + + final OptionGroup sortableSelector = new OptionGroup( + "Sortable columns", sortableContainerPropertyIds); + sortableSelector.setMultiSelect(true); + sortableSelector.setValue(sortableContainerPropertyIds); + + final Table table = new Table() { + @Override + public Collection getSortableContainerPropertyIds() { + return (Collection) sortableSelector.getValue(); + } + }; + table.setContainerDataSource(container); + + sortableSelector.addValueChangeListener(new ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + // Trigger repaint that will read the value again + table.markAsDirty(); + } + }); + + addComponent(sortableSelector); + addComponent(table); + } +} diff --git a/uitest/src/com/vaadin/tests/components/table/SortableHeaderStylesTest.java b/uitest/src/com/vaadin/tests/components/table/SortableHeaderStylesTest.java new file mode 100644 index 0000000000..060b3a661a --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/SortableHeaderStylesTest.java @@ -0,0 +1,51 @@ +/* + * 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 org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.elements.OptionGroupElement; +import com.vaadin.testbench.elements.TableElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class SortableHeaderStylesTest extends SingleBrowserTest { + @Test + public void testSortableHeaderStyles() { + openTestURL(); + + Assert.assertFalse(hasSortableStyle(0)); + for (int i = 1; i < 8; i++) { + Assert.assertTrue(hasSortableStyle(i)); + } + + OptionGroupElement sortableSelector = $(OptionGroupElement.class) + .first(); + + // Toggle sortability + sortableSelector.selectByText("lastName"); + Assert.assertFalse(hasSortableStyle(3)); + + // Toggle back + sortableSelector.selectByText("lastName"); + Assert.assertTrue(hasSortableStyle(3)); + } + + private boolean hasSortableStyle(int column) { + return $(TableElement.class).first().getHeaderCell(column) + .getAttribute("class").contains("sortable"); + } +} -- 2.39.5