Browse Source

add Grid.Column#get/setAssistiveCaption (#10219)

* add Grid#Column#get/setAssistiveCaption

* fix test and update doc

* move testGridAssistiveCaption to GridTest

* delete test file

* delete test file

* Delete GridAssistiveCaptionTest.java

* Create GridAssistiveCaptionTest

* Create GridAssistiveCaption

* Rename GridAssistiveCaption to GridAssistiveCaption.java

* Rename GridAssistiveCaptionTest to GridAssistiveCaptionTest.java

* Reformat using eclipse
tags/8.2.0.alpha3
Knoobie 6 years ago
parent
commit
e65efdfb50

+ 5
- 0
client/src/main/java/com/vaadin/client/connectors/grid/ColumnConnector.java View File

@@ -98,6 +98,11 @@ public class ColumnConnector extends AbstractExtensionConnector {
column.setHeaderCaption(getState().caption);
}

@OnStateChange("assistiveCaption")
void updateAssistiveCaption() {
column.setAssistiveCaption(getState().assistiveCaption);
}

@OnStateChange("sortable")
void updateSortable() {
column.setSortable(getState().sortable);

+ 48
- 2
client/src/main/java/com/vaadin/client/widgets/Grid.java View File

@@ -26,6 +26,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
@@ -4722,6 +4723,8 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,

private String headerCaption = "";

private String assistiveCaption = null;

private String hidingToggleCaption = null;

private double minimumWidthPx = GridConstants.DEFAULT_MIN_WIDTH;
@@ -4836,6 +4839,36 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
return headerCaption;
}

/**
* Sets the header aria-label for this column.
*
* @param caption
* The header aria-label for this column
* @return the column itself
*
* @since
*/
public Column<C, T> setAssistiveCaption(String caption) {
if (!Objects.equals(this.assistiveCaption, caption)) {
this.assistiveCaption = caption;
if (grid != null) {
grid.getHeader().requestSectionRefresh();
}
}

return this;
}
/**
* Returns the current header aria-label for this column.
*
* @return the header aria-label string
*
* @since
*/
public String getAssistiveCaption() {
return assistiveCaption;
}

private void updateHeader() {
HeaderRow row = grid.getHeader().getDefaultRow();
if (row != null) {
@@ -5730,6 +5763,7 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,

// Decorate default row with sorting indicators
if (staticRow instanceof HeaderRow) {
addAriaLabelToHeaderRow(cell);
addSortingIndicatorsToHeaderRow((HeaderRow) staticRow,
cell);
}
@@ -5954,8 +5988,20 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
}
}

private void addSortingIndicatorsToHeaderRow(HeaderRow headerRow,
FlyweightCell cell) {
private void addAriaLabelToHeaderRow(FlyweightCell cell) {

Element cellElement = cell.getElement();

final Column<?, T> column = getVisibleColumn(cell.getColumn());

if (column.getAssistiveCaption() != null) {
cellElement.setAttribute("aria-label", column.getAssistiveCaption());
} else {
cellElement.removeAttribute("aria-label");
}
}

private void addSortingIndicatorsToHeaderRow(HeaderRow headerRow, FlyweightCell cell) {

Element cellElement = cell.getElement();


+ 34
- 0
server/src/main/java/com/vaadin/ui/Grid.java View File

@@ -1202,6 +1202,36 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
return getState(false).sortable;
}

/**
* Sets the header aria-label for this column.
*
* @param caption
* the header aria-label, null removes
* the aria-label from this column
*
* @return this column
*
* @since
*/
public Column<T, V> setAssistiveCaption(String caption) {
if (Objects.equals(caption, getAssistiveCaption())) {
return this;
}
getState().assistiveCaption = caption;
return this;
}

/**
* Gets the header caption for this column.
*
* @return header caption
*
* @since
*/
public String getAssistiveCaption() {
return getState(false).assistiveCaption;
}

/**
* Sets the header caption for this column.
*
@@ -2131,6 +2161,10 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
setHidingToggleCaption(DesignAttributeHandler.readAttribute(
"hiding-toggle-caption", attributes, String.class));
}
if (design.hasAttr("assistive-caption")) {
setAssistiveCaption(DesignAttributeHandler.readAttribute(
"assistive-caption", attributes, String.class));
}

// Read size info where necessary.
if (design.hasAttr("width")) {

+ 7
- 0
server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java View File

@@ -79,6 +79,13 @@ public class GridTest {
.setId("randomColumnId");
}

@Test
public void testGridAssistiveCaption() {
assertEquals(null, fooColumn.getAssistiveCaption());
fooColumn.setAssistiveCaption("Press Enter to sort.");
assertEquals("Press Enter to sort.", fooColumn.getAssistiveCaption());
}

@Test
public void testCreateGridWithDataCommunicator() {
DataCommunicator<String> specificDataCommunicator = new DataCommunicator<>();

+ 3
- 0
shared/src/main/java/com/vaadin/shared/ui/grid/ColumnState.java View File

@@ -30,6 +30,9 @@ public class ColumnState extends AbstractGridExtensionState {
public boolean sortable = false;
public boolean editable = false;

/** The assistive device caption for the column. */
public String assistiveCaption;

/** The caption for the column hiding toggle. */
public String hidingToggleCaption;


+ 40
- 0
uitest/src/main/java/com/vaadin/tests/components/grid/GridAssistiveCaption.java View File

@@ -0,0 +1,40 @@
/*
* Copyright 2000-2017 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.data.ValueProvider;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;

public class GridAssistiveCaption extends AbstractTestUI {

@Override
protected void setup(VaadinRequest request) {
Grid<String> grid = new Grid<>();
grid.addColumn(ValueProvider.identity());
grid.setItems("a", "b");
addComponent(grid);

addComponent(new Button("addAssistiveCaption", event -> {
grid.getColumns().get(0).setAssistiveCaption("Press Enter to sort.");
}));
addComponent(new Button("removeAssistiveCaption", event -> {
grid.getColumns().get(0).setAssistiveCaption(null);
}));
}
}

+ 49
- 0
uitest/src/test/java/com/vaadin/tests/components/grid/GridAssistiveCaptionTest.java View File

@@ -0,0 +1,49 @@
/*
* Copyright 2000-2017 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 static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.tests.tb3.SingleBrowserTest;

public class GridAssistiveCaptionTest extends SingleBrowserTest {

@Test
public void checkGridAriaLabel() {
openTestURL();

GridElement.GridCellElement headerCell = $(GridElement.class).first()
.getHeaderCell(0, 0);

// default grid has no aria-label
assertNull("Column should not contain aria-label",
headerCell.getAttribute("aria-label"));

$(ButtonElement.class).caption("addAssistiveCaption").first().click();
assertTrue("Column should contain aria-label", headerCell
.getAttribute("aria-label").equals("Press Enter to sort."));

$(ButtonElement.class).caption("removeAssistiveCaption").first()
.click();
assertNull("Column should not contain aria-label",
headerCell.getAttribute("aria-label"));
}
}

Loading…
Cancel
Save