From 40ab6dc0397f697001c3e05820863680fcac2e73 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 8 Feb 2017 14:55:11 +0200 Subject: Provide a way to set styles for Header/Footer Cells and Rows in a Grid (#8499) Fixes #8422 --- .../vaadin/tests/components/grid/GridEditorUI.java | 17 +- .../components/grid/GridHeaderStyleNames.java | 101 ++++++++++++ .../components/grid/GridHeaderStyleNames.java | 105 ------------- .../components/grid/GridHeaderStyleNamesTest.java | 158 +++++++++++++++++++ .../components/grid/GridHeaderStyleNamesTest.java | 174 --------------------- 5 files changed, 269 insertions(+), 286 deletions(-) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/grid/GridHeaderStyleNames.java delete mode 100644 uitest/src/main/java/com/vaadin/v7/tests/components/grid/GridHeaderStyleNames.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/grid/GridHeaderStyleNamesTest.java delete mode 100644 uitest/src/test/java/com/vaadin/v7/tests/components/grid/GridHeaderStyleNamesTest.java (limited to 'uitest') diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorUI.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorUI.java index d2d865498e..9efa5c1e11 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorUI.java @@ -66,20 +66,23 @@ public class GridEditorUI extends AbstractTestUI { protected Grid createGrid() { Grid grid = new Grid<>(); - grid.addColumn(Person::getEmail).setCaption("Email"); + grid.addColumn(Person::getEmail).setCaption("Email").setId("email"); Column fistNameColumn = grid - .addColumn(Person::getFirstName).setCaption("First Name"); + .addColumn(Person::getFirstName).setCaption("First Name") + .setId("firstName"); Column lastNameColumn = grid - .addColumn(Person::getLastName).setCaption("Last Name"); + .addColumn(Person::getLastName).setCaption("Last Name") + .setId("lastName"); Column phoneColumn = grid - .addColumn(Person::getPhoneNumber).setCaption("Phone Number"); + .addColumn(Person::getPhoneNumber).setCaption("Phone Number") + .setId("phone"); grid.addColumn(person -> person.getAddress().getStreetAddress()) - .setCaption("Street Address"); + .setCaption("Street Address").setId("street"); grid.addColumn(person -> person.getAddress().getPostalCode(), - new NumberRenderer()).setCaption("Postal Code"); + new NumberRenderer()).setCaption("Postal Code").setId("zip"); grid.addColumn(person -> person.getAddress().getCity()) - .setCaption("City"); + .setCaption("City").setId("city"); grid.getEditor().setEnabled(true); diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridHeaderStyleNames.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridHeaderStyleNames.java new file mode 100644 index 0000000000..0295a37175 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridHeaderStyleNames.java @@ -0,0 +1,101 @@ +/* + * Copyright 2000-2016 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 com.vaadin.server.VaadinRequest; +import com.vaadin.tests.util.Person; +import com.vaadin.ui.Button; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.SelectionMode; +import com.vaadin.ui.components.grid.FooterCell; +import com.vaadin.ui.components.grid.FooterRow; +import com.vaadin.ui.components.grid.HeaderCell; +import com.vaadin.ui.components.grid.HeaderRow; + +public class GridHeaderStyleNames extends GridEditorUI { + + private HeaderCell nameHeaderCell; + private HeaderCell mergedCityCountryCell; + private FooterCell nameFooterCell; + private HeaderRow headerRow; + private FooterRow footerRow; + + private boolean stylesOn = true; + + @Override + protected void setup(VaadinRequest request) { + Grid grid = createGrid(); + grid.setItems(createTestData()); + grid.setSelectionMode(SelectionMode.MULTI); + + nameHeaderCell = grid.getDefaultHeaderRow().getCell("firstName"); + grid.getDefaultHeaderRow().setStyleName("foo"); + headerRow = grid.prependHeaderRow(); + mergedCityCountryCell = headerRow.join("city", "street"); + mergedCityCountryCell.setText("Merged cell"); + + grid.setColumns("email", "firstName", "city", "street", "lastName", + "zip"); + addComponent(grid); + + footerRow = grid.appendFooterRow(); + nameFooterCell = footerRow.getCell("firstName"); + + getPage().getStyles().add( + ".name {background-image: linear-gradient(to bottom,green 2%, #efefef 98%) !important;}"); + getPage().getStyles().add( + ".valo .v-grid-header .v-grid-cell.city-country {background-image: linear-gradient(to bottom,yellow 2%, #efefef 98%) !important;}"); + getPage().getStyles().add( + ".valo .v-grid-footer .v-grid-cell.name-footer {background-image: linear-gradient(to bottom,blue 2%, #efefef 98%) !important;}"); + getPage().getStyles().add( + ".valo .v-grid .v-grid-row.custom-row > * {background-image: linear-gradient(to bottom,purple 2%, #efefef 98%);}"); + + setCellStyles(true); + setRowStyles(true); + + Button button = new Button("Toggle styles"); + button.addClickListener(event -> { + setCellStyles(!stylesOn); + setRowStyles(!stylesOn); + stylesOn = !stylesOn; + }); + addComponent(button); + } + + protected void setCellStyles(boolean set) { + if (set) { + nameHeaderCell.setStyleName("name"); + nameFooterCell.setStyleName("name-footer"); + mergedCityCountryCell.setStyleName("city-country"); + } else { + nameHeaderCell.setStyleName(null); + nameFooterCell.setStyleName(null); + mergedCityCountryCell.setStyleName(null); + } + + } + + protected void setRowStyles(boolean set) { + if (set) { + headerRow.setStyleName("custom-row"); + footerRow.setStyleName("custom-row"); + } else { + headerRow.setStyleName(null); + footerRow.setStyleName(null); + } + + } +} diff --git a/uitest/src/main/java/com/vaadin/v7/tests/components/grid/GridHeaderStyleNames.java b/uitest/src/main/java/com/vaadin/v7/tests/components/grid/GridHeaderStyleNames.java deleted file mode 100644 index 9604f9f218..0000000000 --- a/uitest/src/main/java/com/vaadin/v7/tests/components/grid/GridHeaderStyleNames.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2000-2016 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.v7.tests.components.grid; - -import com.vaadin.server.VaadinRequest; -import com.vaadin.tests.components.AbstractTestUIWithLog; -import com.vaadin.tests.components.beanitemcontainer.BeanItemContainerGenerator; -import com.vaadin.ui.Button; -import com.vaadin.ui.Button.ClickEvent; -import com.vaadin.ui.Button.ClickListener; -import com.vaadin.v7.ui.Grid; -import com.vaadin.v7.ui.Grid.FooterCell; -import com.vaadin.v7.ui.Grid.FooterRow; -import com.vaadin.v7.ui.Grid.HeaderCell; -import com.vaadin.v7.ui.Grid.HeaderRow; -import com.vaadin.v7.ui.Grid.SelectionMode; - -public class GridHeaderStyleNames extends AbstractTestUIWithLog { - - private HeaderCell ageHeaderCell; - private HeaderCell mergedCityCountryCell; - private FooterCell ageFooterCell; - private HeaderRow headerRow; - private FooterRow footerRow; - - @Override - protected void setup(VaadinRequest request) { - Grid grid = new Grid(); - grid.setSelectionMode(SelectionMode.MULTI); - grid.setContainerDataSource( - BeanItemContainerGenerator.createContainer(100)); - - ageHeaderCell = grid.getDefaultHeaderRow().getCell("age"); - grid.getDefaultHeaderRow().setStyleName("foo"); - headerRow = grid.prependHeaderRow(); - mergedCityCountryCell = headerRow.join("city", "country"); - mergedCityCountryCell.setText("Merged cell"); - addComponent(grid); - - footerRow = grid.appendFooterRow(); - ageFooterCell = footerRow.getCell("age"); - - getPage().getStyles().add( - ".age {background-image: linear-gradient(to bottom,green 2%, #efefef 98%) !important;}"); - getPage().getStyles().add( - ".valo .v-grid-header .v-grid-cell.city-country {background-image: linear-gradient(to bottom,yellow 2%, #efefef 98%) !important;}"); - getPage().getStyles().add( - ".valo .v-grid-footer .v-grid-cell.age-footer {background-image: linear-gradient(to bottom,blue 2%, #efefef 98%) !important;}"); - getPage().getStyles().add( - ".valo .v-grid .v-grid-row.custom-row > * {background-image: linear-gradient(to bottom,purple 2%, #efefef 98%);}"); - - setCellStyles(true); - setRowStyles(true); - - Button b = new Button("Toggle styles"); - b.addClickListener(new ClickListener() { - private boolean stylesOn = true; - - @Override - public void buttonClick(ClickEvent event) { - setCellStyles(!stylesOn); - setRowStyles(!stylesOn); - stylesOn = !stylesOn; - } - }); - addComponent(b); - } - - protected void setCellStyles(boolean set) { - if (set) { - ageHeaderCell.setStyleName("age"); - ageFooterCell.setStyleName("age-footer"); - mergedCityCountryCell.setStyleName("city-country"); - } else { - ageHeaderCell.setStyleName(null); - ageFooterCell.setStyleName(null); - mergedCityCountryCell.setStyleName(null); - } - - } - - protected void setRowStyles(boolean set) { - if (set) { - headerRow.setStyleName("custom-row"); - footerRow.setStyleName("custom-row"); - } else { - headerRow.setStyleName(null); - footerRow.setStyleName(null); - } - - } -} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridHeaderStyleNamesTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridHeaderStyleNamesTest.java new file mode 100644 index 0000000000..0297b30876 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridHeaderStyleNamesTest.java @@ -0,0 +1,158 @@ +/* + * Copyright 2000-2016 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.Before; +import org.junit.Test; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.testbench.elements.GridElement.GridCellElement; +import com.vaadin.testbench.parallel.TestCategory; +import com.vaadin.tests.tb3.SingleBrowserTest; + +@TestCategory("grid") +public class GridHeaderStyleNamesTest extends SingleBrowserTest { + + private GridElement grid; + + @Before + public void findGridCells() { + openTestURL(); + grid = $(GridElement.class).first(); + } + + private GridCellElement getMergedHeaderCell() { + return grid.getHeaderCell(0, 3); + } + + private GridCellElement getNameFooterCell() { + return grid.getFooterCell(0, 2); + } + + @Test + public void cellStyleNamesCanBeAddedAndRemoved() { + ButtonElement toggleStyles = $(ButtonElement.class) + .caption("Toggle styles").first(); + + assertStylesSet(true); + toggleStyles.click(); + assertStylesSet(false); + toggleStyles.click(); + assertStylesSet(true); + } + + @Test + public void rowStyleNamesCanBeAddedAndRemoved() { + ButtonElement toggleStyles = $(ButtonElement.class) + .caption("Toggle styles").first(); + + assertRowStylesSet(true); + toggleStyles.click(); + assertRowStylesSet(false); + toggleStyles.click(); + assertRowStylesSet(true); + + } + + private void assertStylesSet(boolean set) { + if (set) { + assertHasStyleName( + "Footer cell should have the assigned 'name-footer' class name", + getNameFooterCell(), "name-footer"); + assertHasStyleName( + "Header cell should have the assigned 'name' class name", + getAgeHeaderCell(), "name"); + assertHasStyleName( + "The merged header cell should have the assigned 'city-country' class name", + getMergedHeaderCell(), "city-country"); + } else { + assertHasNotStyleName( + "Footer cell should not have the removed 'name-footer' class name", + getNameFooterCell(), "name-footer"); + assertHasNotStyleName( + "Header cell should not have the removed 'name' class name", + getAgeHeaderCell(), "name"); + assertHasNotStyleName( + "Ther merged header cell should not have the removed 'city-country' class name", + getMergedHeaderCell(), "city-country"); + } + assertHasStyleName( + "The default v-grid-cell style name should not be removed from the header cell", + getAgeHeaderCell(), "v-grid-cell"); + assertHasStyleName( + "The default v-grid-cell style name should not be removed from the footer cell", + getNameFooterCell(), "v-grid-cell"); + assertHasStyleName( + "The default v-grid-cell style name should not be removed from the merged header cell", + getMergedHeaderCell(), "v-grid-cell"); + + } + + private void assertRowStylesSet(boolean set) { + if (set) { + assertHasStyleName( + "Footer row should have the assigned 'custom-row' class name", + getFooterRow(), "custom-row"); + assertHasStyleName( + "Header row should have the assigned 'custom-row' class name", + getHeaderRow(), "custom-row"); + } else { + assertHasNotStyleName( + "Footer row should not have the removed 'custom-row' class name", + getFooterRow(), "custom-row"); + assertHasNotStyleName( + "Header row should not have the removed 'custom-row' class name", + getHeaderRow(), "custom-row"); + } + assertHasStyleName( + "The default v-grid-row style name should not be removed from the header row", + getHeaderRow(), "v-grid-row"); + assertHasStyleName( + "The default v-grid-row style name should not be removed from the footer row", + getFooterRow(), "v-grid-row"); + + } + + private WebElement getAgeHeaderCell() { + return grid.getHeaderCell(1, 2); + } + + private WebElement getFooterRow() { + return grid.getFooterRow(0); + } + + private WebElement getHeaderRow() { + return grid.getHeaderRow(0); + } + + private void assertHasStyleName(String message, WebElement element, + String stylename) { + if (!hasCssClass(element, stylename)) { + Assert.fail(message); + } + } + + private void assertHasNotStyleName(String message, WebElement element, + String stylename) { + if (hasCssClass(element, stylename)) { + Assert.fail(message); + } + } + +} diff --git a/uitest/src/test/java/com/vaadin/v7/tests/components/grid/GridHeaderStyleNamesTest.java b/uitest/src/test/java/com/vaadin/v7/tests/components/grid/GridHeaderStyleNamesTest.java deleted file mode 100644 index 07bf047546..0000000000 --- a/uitest/src/test/java/com/vaadin/v7/tests/components/grid/GridHeaderStyleNamesTest.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright 2000-2016 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.v7.tests.components.grid; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openqa.selenium.By; -import org.openqa.selenium.WebElement; - -import com.vaadin.testbench.elements.GridElement; -import com.vaadin.testbench.elements.ButtonElement; -import com.vaadin.testbench.elements.GridElement.GridCellElement; -import com.vaadin.testbench.parallel.TestCategory; -import com.vaadin.tests.tb3.SingleBrowserTest; - -@TestCategory("grid") -public class GridHeaderStyleNamesTest extends SingleBrowserTest { - - private GridElement grid; - - @Before - public void findGridCells() { - openTestURL(); - grid = $(GridElement.class).first(); - } - - private GridCellElement getMergedHeaderCell() { - return grid.getHeaderCell(0, 3); - } - - private WebElement getMergedHeaderCellContent() { - return getMergedHeaderCell().findElement( - By.cssSelector("div.v-grid-column-header-content")); - } - - private GridCellElement getAgeFooterCell() { - return grid.getFooterCell(0, 2); - } - - private WebElement getAgeFooterCellContent() { - return getAgeFooterCell().findElement( - By.cssSelector("div.v-grid-column-footer-content")); - } - - @Test - public void cellStyleNamesCanBeAddedAndRemoved() { - ButtonElement toggleStyles = $(ButtonElement.class) - .caption("Toggle styles").first(); - - assertStylesSet(true); - toggleStyles.click(); - assertStylesSet(false); - toggleStyles.click(); - assertStylesSet(true); - } - - @Test - public void rowStyleNamesCanBeAddedAndRemoved() { - ButtonElement toggleStyles = $(ButtonElement.class) - .caption("Toggle styles").first(); - - assertRowStylesSet(true); - toggleStyles.click(); - assertRowStylesSet(false); - toggleStyles.click(); - assertRowStylesSet(true); - - } - - private void assertStylesSet(boolean set) { - if (set) { - assertHasStyleName( - "Footer cell should have the assigned 'age-footer' class name", - getAgeFooterCell(), "age-footer"); - assertHasStyleName( - "Header cell should have the assigned 'age' class name", - getAgeHeaderCell(), "age"); - assertHasStyleName( - "The merged header cell should have the assigned 'city-country' class name", - getMergedHeaderCell(), "city-country"); - } else { - assertHasNotStyleName( - "Footer cell should not have the removed 'age-footer' class name", - getAgeFooterCell(), "age-footer"); - assertHasNotStyleName( - "Header cell should not have the removed 'age' class name", - getAgeHeaderCell(), "age"); - assertHasNotStyleName( - "Ther merged header cell should not have the removed 'city-country' class name", - getMergedHeaderCell(), "city-country"); - } - assertHasStyleName( - "The default v-grid-cell style name should not be removed from the header cell", - getAgeHeaderCell(), "v-grid-cell"); - assertHasStyleName( - "The default v-grid-cell style name should not be removed from the footer cell", - getAgeFooterCell(), "v-grid-cell"); - assertHasStyleName( - "The default v-grid-cell style name should not be removed from the merged header cell", - getMergedHeaderCell(), "v-grid-cell"); - - } - - private void assertRowStylesSet(boolean set) { - if (set) { - assertHasStyleName( - "Footer row should have the assigned 'custom-row' class name", - getFooterRow(), "custom-row"); - assertHasStyleName( - "Header row should have the assigned 'custom-row' class name", - getHeaderRow(), "custom-row"); - } else { - assertHasNotStyleName( - "Footer row should not have the removed 'custom-row' class name", - getFooterRow(), "custom-row"); - assertHasNotStyleName( - "Header row should not have the removed 'custom-row' class name", - getHeaderRow(), "custom-row"); - } - assertHasStyleName( - "The default v-grid-row style name should not be removed from the header row", - getHeaderRow(), "v-grid-row"); - assertHasStyleName( - "The default v-grid-row style name should not be removed from the footer row", - getFooterRow(), "v-grid-row"); - - } - - private WebElement getAgeHeaderCell() { - return grid.getHeaderCell(1, 2); - } - - private WebElement getAgeHeaderCellContent() { - return getAgeHeaderCell().findElement( - By.cssSelector("div.v-grid-column-header-content")); - } - - private WebElement getFooterRow() { - return grid.getFooterRow(0); - } - - private WebElement getHeaderRow() { - return grid.getHeaderRow(0); - } - - private void assertHasStyleName(String message, WebElement element, - String stylename) { - if (!hasCssClass(element, stylename)) { - Assert.fail(message); - } - } - - private void assertHasNotStyleName(String message, WebElement element, - String stylename) { - if (hasCssClass(element, stylename)) { - Assert.fail(message); - } - } - -} -- cgit v1.2.3