From b4d0865f93c5ba8e08957812bc563f4ad2e9d00f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 19 Nov 2015 15:10:56 +0200 Subject: [PATCH] Add stylename to sortable Grid header cells (#16991) Change-Id: I6c151829236928129c258a99177431d0b972f146 --- .../src/com/vaadin/client/widgets/Grid.java | 21 ++++-- .../components/grid/GridEditorUITest.java | 3 +- .../components/grid/SortableHeaderStyles.java | 67 +++++++++++++++++++ .../grid/SortableHeaderStylesTest.java | 51 ++++++++++++++ 4 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/grid/SortableHeaderStyles.java create mode 100644 uitest/src/com/vaadin/tests/components/grid/SortableHeaderStylesTest.java diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java index 3033d74867..7086ff86d2 100644 --- a/client/src/com/vaadin/client/widgets/Grid.java +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -5612,18 +5612,26 @@ public class Grid extends ResizeComposite implements FlyweightCell cell) { cleanup(cell); + if (!headerRow.isDefault()) { + // Nothing more to do if not in the default row + return; + } Column column = getVisibleColumn(cell.getColumn()); SortOrder sortingOrder = getSortOrder(column); - if (!headerRow.isDefault() || !column.isSortable() - || sortingOrder == null) { - // Only apply sorting indicators to sortable header columns in - // the default header row - return; - } + boolean sortable = column.isSortable(); Element cellElement = cell.getElement(); + if (sortable) { + cellElement.addClassName("sortable"); + } + + if (!sortable || sortingOrder == null) { + // Only apply sorting indicators to sortable header columns + return; + } + if (SortDirection.ASCENDING == sortingOrder.getDirection()) { cellElement.addClassName("sort-asc"); } else { @@ -5656,6 +5664,7 @@ public class Grid extends ResizeComposite implements cellElement.removeAttribute("sort-order"); cellElement.removeClassName("sort-desc"); cellElement.removeClassName("sort-asc"); + cellElement.removeClassName("sortable"); } @Override diff --git a/uitest/src/com/vaadin/tests/components/grid/GridEditorUITest.java b/uitest/src/com/vaadin/tests/components/grid/GridEditorUITest.java index 708c3a827d..3f17d9a80c 100644 --- a/uitest/src/com/vaadin/tests/components/grid/GridEditorUITest.java +++ b/uitest/src/com/vaadin/tests/components/grid/GridEditorUITest.java @@ -90,6 +90,7 @@ public class GridEditorUITest extends MultiBrowserTest { saveEditor(); - assertThat(headerCell.getAttribute("class"), not(containsString("sort"))); + assertThat(headerCell.getAttribute("class"), + not(containsString("sort-"))); } } diff --git a/uitest/src/com/vaadin/tests/components/grid/SortableHeaderStyles.java b/uitest/src/com/vaadin/tests/components/grid/SortableHeaderStyles.java new file mode 100644 index 0000000000..56e6c608b3 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/grid/SortableHeaderStyles.java @@ -0,0 +1,67 @@ +/* + * 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.grid; + +import java.util.Collection; + +import com.vaadin.annotations.Theme; +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.Grid; +import com.vaadin.ui.Grid.Column; +import com.vaadin.ui.OptionGroup; + +@Theme("valo") +public class SortableHeaderStyles extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + getPage() + .getStyles() + .add(".valo .v-grid-header th.v-grid-cell.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 Grid grid = new Grid(container); + + sortableSelector.addValueChangeListener(new ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + Collection sortableCols = (Collection) sortableSelector + .getValue(); + for (Column column : grid.getColumns()) { + column.setSortable(sortableCols.contains(column + .getPropertyId())); + } + } + }); + + addComponent(sortableSelector); + addComponent(grid); + } + +} diff --git a/uitest/src/com/vaadin/tests/components/grid/SortableHeaderStylesTest.java b/uitest/src/com/vaadin/tests/components/grid/SortableHeaderStylesTest.java new file mode 100644 index 0000000000..32a896603b --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/grid/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.grid; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.testbench.elements.OptionGroupElement; +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 $(GridElement.class).first().getHeaderCell(0, column) + .getAttribute("class").contains("sortable"); + } +} -- 2.39.5