From 94dfb5d68dd6c1e3542edc80488fe53a9da2d0e5 Mon Sep 17 00:00:00 2001 From: John Ahlroos Date: Wed, 30 Jul 2014 13:27:51 +0300 Subject: [PATCH] Support HTML and Widgets in header/footer cells #13334 Change-Id: I1f9a4388b9b2e9e3892b31977eaf674ed4e0959b --- .../com/vaadin/client/ui/grid/Escalator.java | 8 +- .../src/com/vaadin/client/ui/grid/Grid.java | 82 ++++++++++- .../vaadin/client/ui/grid/GridConnector.java | 2 +- .../com/vaadin/client/ui/grid/GridFooter.java | 10 +- .../com/vaadin/client/ui/grid/GridHeader.java | 14 +- .../client/ui/grid/GridStaticSection.java | 135 +++++++++++++++--- .../grid/basicfeatures/GridFooterTest.java | 59 ++++++++ .../grid/basicfeatures/GridHeaderTest.java | 79 +++++++++- .../basicfeatures/GridStaticSectionTest.java | 40 +++++- .../client/grid/GridBasicClientFeatures.java | 88 +++++++++++- 10 files changed, 460 insertions(+), 57 deletions(-) diff --git a/client/src/com/vaadin/client/ui/grid/Escalator.java b/client/src/com/vaadin/client/ui/grid/Escalator.java index df564be937..7263313356 100644 --- a/client/src/com/vaadin/client/ui/grid/Escalator.java +++ b/client/src/com/vaadin/client/ui/grid/Escalator.java @@ -4405,7 +4405,13 @@ public class Escalator extends Widget { @SuppressWarnings("deprecation") com.google.gwt.user.client.Element castElement = (com.google.gwt.user.client.Element) possibleWidgetNode .cast(); - return Util.findWidget(castElement, null); + Widget w = Util.findWidget(castElement, null); + + // Ensure findWidget did not traverse past the cell element in the + // DOM hierarchy + if (cellNode.isOrHasChild(w.getElement())) { + return w; + } } return null; } diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java index 580cf41165..cf0606a38c 100644 --- a/client/src/com/vaadin/client/ui/grid/Grid.java +++ b/client/src/com/vaadin/client/ui/grid/Grid.java @@ -710,7 +710,7 @@ public class Grid extends Composite implements public Collection getConsumedEvents() { return Arrays.asList(BrowserEvents.TOUCHSTART, BrowserEvents.TOUCHMOVE, BrowserEvents.TOUCHEND, - BrowserEvents.TOUCHCANCEL, BrowserEvents.MOUSEDOWN); + BrowserEvents.TOUCHCANCEL, BrowserEvents.CLICK); } @Override @@ -774,11 +774,14 @@ public class Grid extends Composite implements lazySorter.cancel(); - } else if (BrowserEvents.MOUSEDOWN.equals(event.getType())) { - event.preventDefault(); + } else if (BrowserEvents.CLICK.equals(event.getType())) { lazySorter.setCurrentCell(cell); lazySorter.setMultisort(event.getShiftKey()); lazySorter.run(); + + // Active cell handling is also monitoring the click + // event so we allow event to propagate for it + return false; } return true; } @@ -1046,7 +1049,7 @@ public class Grid extends Composite implements @Override public void update(Row row, Iterable cellsToUpdate) { - GridStaticSection.StaticRow gridRow = section.getRow(row + GridStaticSection.StaticRow staticRow = section.getRow(row .getRow()); final List columnIndices = getVisibleColumnIndices(); @@ -1054,13 +1057,28 @@ public class Grid extends Composite implements for (FlyweightCell cell : cellsToUpdate) { int index = columnIndices.get(cell.getColumn()); - StaticCell metadata = gridRow.getCell(index); + final StaticCell metadata = staticRow.getCell(index); // Assign colspan to cell before rendering cell.setColSpan(metadata.getColspan()); - // Render - gridRow.getRenderer().render(cell, metadata.getText()); + // Decorates cell with possible indicators onto the cell. + // Actual content is rendered below. + staticRow.getRenderer().render(cell, null); + + switch (metadata.getType()) { + case TEXT: + cell.getElement().setInnerText(metadata.getText()); + break; + case HTML: + cell.getElement().setInnerHTML(metadata.getHtml()); + break; + case WIDGET: + preDetach(row, Arrays.asList(cell)); + cell.getElement().setInnerHTML(""); + postAttach(row, Arrays.asList(cell)); + break; + } activeCellHandler.updateActiveCellStyle(cell, container); } @@ -1072,10 +1090,60 @@ public class Grid extends Composite implements @Override public void postAttach(Row row, Iterable attachedCells) { + GridStaticSection.StaticRow gridRow = section.getRow(row + .getRow()); + List columnIndices = getVisibleColumnIndices(); + + for (FlyweightCell cell : attachedCells) { + int index = columnIndices.get(cell.getColumn()); + StaticCell metadata = gridRow.getCell(index); + /* + * If the cell contains widgets that are not currently attach + * then attach them now. + */ + if (StaticCell.Type.WIDGET.equals(metadata.getType())) { + final Widget widget = metadata.getWidget(); + final Element cellElement = cell.getElement(); + + if (!widget.isAttached()) { + + // Physical attach + cellElement.appendChild(widget.getElement()); + + // Logical attach + setParent(widget, Grid.this); + + getLogger().info("Attached widget " + widget); + } + } + } } @Override public void preDetach(Row row, Iterable cellsToDetach) { + if (section.getRowCount() > row.getRow()) { + GridStaticSection.StaticRow gridRow = section.getRow(row + .getRow()); + List columnIndices = getVisibleColumnIndices(); + for (FlyweightCell cell : cellsToDetach) { + int index = columnIndices.get(cell.getColumn()); + StaticCell metadata = gridRow.getCell(index); + + if (StaticCell.Type.WIDGET.equals(metadata.getType()) + && metadata.getWidget().isAttached()) { + + Widget widget = metadata.getWidget(); + + // Logical detach + setParent(widget, null); + + // Physical detach + widget.getElement().removeFromParent(); + + getLogger().info("Detached widget " + widget); + } + } + } } @Override diff --git a/client/src/com/vaadin/client/ui/grid/GridConnector.java b/client/src/com/vaadin/client/ui/grid/GridConnector.java index 039e3b1074..56affc75d1 100644 --- a/client/src/com/vaadin/client/ui/grid/GridConnector.java +++ b/client/src/com/vaadin/client/ui/grid/GridConnector.java @@ -317,7 +317,7 @@ public class GridConnector extends AbstractComponentConnector { section.setVisible(state.visible); - section.refreshSection(); + section.requestSectionRefresh(); } /** diff --git a/client/src/com/vaadin/client/ui/grid/GridFooter.java b/client/src/com/vaadin/client/ui/grid/GridFooter.java index 4470bbf6b9..e798139b9a 100644 --- a/client/src/com/vaadin/client/ui/grid/GridFooter.java +++ b/client/src/com/vaadin/client/ui/grid/GridFooter.java @@ -20,14 +20,6 @@ import com.google.gwt.core.client.Scheduler; /** * Represents the footer section of a Grid. The footer is always empty. * - * TODO Arbitrary number of footer rows (zero by default) - * - * TODO Merging footer cells - * - * TODO Widgets in cells - * - * TODO HTML in cells - * * @since * @author Vaadin Ltd */ @@ -60,7 +52,7 @@ public class GridFooter extends GridStaticSection { } @Override - protected void refreshSection() { + protected void requestSectionRefresh() { markAsDirty = true; /* diff --git a/client/src/com/vaadin/client/ui/grid/GridHeader.java b/client/src/com/vaadin/client/ui/grid/GridHeader.java index e139d7b946..f714848618 100644 --- a/client/src/com/vaadin/client/ui/grid/GridHeader.java +++ b/client/src/com/vaadin/client/ui/grid/GridHeader.java @@ -23,16 +23,6 @@ import com.vaadin.client.ui.grid.Grid.AbstractGridColumn.SortableColumnHeaderRen * row containing a header cell for each column. Each cell has a simple textual * caption. * - * TODO Arbitrary number of header rows (zero included, one by default) - * - * TODO Account for hidden columns when merging header cells - * - * TODO "Default" row with sorting - * - * TODO Widgets in cells - * - * TODO HTML in cells - * * @since * @author Vaadin Ltd */ @@ -117,7 +107,7 @@ public class GridHeader extends GridStaticSection { row.setDefault(true); } defaultRow = row; - refreshSection(); + requestSectionRefresh(); } /** @@ -136,7 +126,7 @@ public class GridHeader extends GridStaticSection { } @Override - protected void refreshSection() { + protected void requestSectionRefresh() { markAsDirty = true; /* diff --git a/client/src/com/vaadin/client/ui/grid/GridStaticSection.java b/client/src/com/vaadin/client/ui/grid/GridStaticSection.java index c40c41594c..14e4d6de9c 100644 --- a/client/src/com/vaadin/client/ui/grid/GridStaticSection.java +++ b/client/src/com/vaadin/client/ui/grid/GridStaticSection.java @@ -21,7 +21,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.List; -import com.vaadin.client.ui.grid.renderers.TextRenderer; +import com.google.gwt.user.client.ui.Widget; /** * Abstract base class for Grid header and footer sections. @@ -36,18 +36,21 @@ abstract class GridStaticSection> /** * A header or footer cell. Has a simple textual caption. * - * TODO HTML content - * - * TODO Widget content */ static class StaticCell { - private String text = ""; + public enum Type { + TEXT, HTML, WIDGET; + } + + private Object content = null; private int colspan = 1; private GridStaticSection section; + private Type type = Type.TEXT; + /** * Sets the text displayed in this cell. * @@ -55,8 +58,9 @@ abstract class GridStaticSection> * a plain text caption */ public void setText(String text) { - this.text = text; - section.refreshSection(); + this.content = text; + this.type = Type.TEXT; + section.requestSectionRefresh(); } /** @@ -65,7 +69,11 @@ abstract class GridStaticSection> * @return the plain text caption */ public String getText() { - return text; + if (type != Type.TEXT) { + throw new IllegalStateException( + "Cannot fetch Text from a cell with type " + type); + } + return (String) content; } protected GridStaticSection getSection() { @@ -78,14 +86,17 @@ abstract class GridStaticSection> } /** - * @return the colspan + * Returns the amount of columns the cell spans. By default is 1. + * + * @return The amount of columns the cell spans. */ public int getColspan() { return colspan; } /** - * Sets the colspan for the cell + * Sets the amount of columns the cell spans. Must be more or equal to + * 1. By default is 1. * * @param colspan * the colspan to set @@ -95,10 +106,78 @@ abstract class GridStaticSection> throw new IllegalArgumentException( "Colspan cannot be less than 1"); } + this.colspan = colspan; - section.refreshSection(); + section.requestSectionRefresh(); + } + + /** + * Returns the html inside the cell. + * + * @throws IllegalStateException + * if trying to retrive HTML from a cell with a type other + * than {@link Type#HTML}. + * @return the html content of the cell. + */ + public String getHtml() { + if (type != Type.HTML) { + throw new IllegalStateException( + "Cannot fetch HTML from a cell with type " + type); + } + return (String) content; } + /** + * Sets the content of the cell to the provided html. All previous + * content is discarded and the cell type is set to {@link Type#HTML}. + * + * @param html + * The html content of the cell + */ + public void setHtml(String html) { + this.content = html; + this.type = Type.HTML; + section.requestSectionRefresh(); + } + + /** + * Returns the widget in the cell. + * + * @throws IllegalStateException + * if the cell is not {@link Type#WIDGET} + * + * @return the widget in the cell + */ + public Widget getWidget() { + if (type != Type.WIDGET) { + throw new IllegalStateException( + "Cannot fetch Widget from a cell with type " + type); + } + return (Widget) content; + } + + /** + * Set widget as the content of the cell. The type of the cell becomes + * {@link Type#WIDGET}. All previous content is discarded. + * + * @param widget + * The widget to add to the cell. Should not be previously + * attached anywhere (widget.getParent == null). + */ + public void setWidget(Widget widget) { + this.content = widget; + this.type = Type.WIDGET; + section.requestSectionRefresh(); + } + + /** + * Returns the type of the cell. + * + * @return the type of content the cell contains. + */ + public Type getType() { + return type; + } } /** @@ -111,7 +190,16 @@ abstract class GridStaticSection> private List cells = new ArrayList(); - private Renderer renderer = new TextRenderer(); + private Renderer renderer = new Renderer() { + + @Override + public void render(FlyweightCell cell, String data) { + /* + * The rendering into the cell is done directly from the updater + * since it needs to handle multiple types of data. + */ + } + }; private GridStaticSection section; @@ -160,10 +248,9 @@ abstract class GridStaticSection> // Create a new group cellGroups.add(new ArrayList(cells)); + // Calculates colspans, triggers refresh on section implicitly calculateColspans(); - getSection().refreshSection(); - // Returns first cell of group return cells.get(0); } @@ -309,8 +396,12 @@ abstract class GridStaticSection> /** * Informs the grid that this section should be re-rendered. + *

+ * Note that re-render means calling update() on each cell, + * preAttach()/postAttach()/preDetach()/postDetach() is not called as the + * cells are not removed from the DOM. */ - protected abstract void refreshSection(); + protected abstract void requestSectionRefresh(); /** * Sets the visibility of the whole section. @@ -320,7 +411,7 @@ abstract class GridStaticSection> */ public void setVisible(boolean visible) { this.visible = visible; - refreshSection(); + requestSectionRefresh(); } /** @@ -349,7 +440,8 @@ abstract class GridStaticSection> row.addCell(i); } rows.add(index, row); - refreshSection(); + + requestSectionRefresh(); return row; } @@ -382,7 +474,7 @@ abstract class GridStaticSection> */ public void removeRow(int index) { rows.remove(index); - refreshSection(); + requestSectionRefresh(); } /** @@ -414,7 +506,12 @@ abstract class GridStaticSection> * if the index is out of bounds */ public ROWTYPE getRow(int index) { - return rows.get(index); + try { + return rows.get(index); + } catch (IndexOutOfBoundsException e) { + throw new IllegalArgumentException("Row with index " + index + + " does not exist"); + } } /** diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridFooterTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridFooterTest.java index c4e86369f9..d6a865ee29 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridFooterTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridFooterTest.java @@ -17,9 +17,12 @@ package com.vaadin.tests.components.grid.basicfeatures; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; import com.vaadin.tests.components.grid.GridElement.GridCellElement; @@ -141,6 +144,62 @@ public class GridFooterTest extends GridStaticSectionTest { } } + @Test + public void testInitialCellTypes() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Footer", "Append row"); + + GridCellElement textCell = getGridElement().getFooterCell(0, 0); + assertEquals("Footer (0,0)", textCell.getText()); + + GridCellElement widgetCell = getGridElement().getFooterCell(0, 1); + assertTrue(widgetCell.isElementPresent(By.className("gwt-HTML"))); + + GridCellElement htmlCell = getGridElement().getFooterCell(0, 2); + assertHTML("Footer (0,2)", htmlCell); + } + + @Test + public void testDynamicallyChangingCellType() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Footer", "Append row"); + + selectMenuPath("Component", "Columns", "Column 0", "Footer Type", + "Widget Footer"); + GridCellElement widgetCell = getGridElement().getFooterCell(0, 0); + assertTrue(widgetCell.isElementPresent(By.className("gwt-Button"))); + + selectMenuPath("Component", "Columns", "Column 1", "Footer Type", + "HTML Footer"); + GridCellElement htmlCell = getGridElement().getFooterCell(0, 1); + assertHTML("HTML Footer", htmlCell); + + selectMenuPath("Component", "Columns", "Column 2", "Footer Type", + "Text Footer"); + GridCellElement textCell = getGridElement().getFooterCell(0, 2); + assertEquals("Text Footer", textCell.getText()); + } + + @Test + public void testCellWidgetInteraction() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Footer", "Append row"); + + selectMenuPath("Component", "Columns", "Column 0", "Footer Type", + "Widget Footer"); + GridCellElement widgetCell = getGridElement().getFooterCell(0, 0); + WebElement button = widgetCell.findElement(By.className("gwt-Button")); + + assertNotEquals("Clicked", button.getText()); + + button.click(); + + assertEquals("Clicked", button.getText()); + } + private void assertFooterCount(int count) { assertEquals("footer count", count, getGridElement().getFooterCount()); } diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridHeaderTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridHeaderTest.java index 2665d5c669..ccffee854a 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridHeaderTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridHeaderTest.java @@ -17,12 +17,15 @@ package com.vaadin.tests.components.grid.basicfeatures; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.List; import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; import com.vaadin.testbench.TestBenchElement; import com.vaadin.tests.components.grid.GridElement.GridCellElement; @@ -72,8 +75,8 @@ public class GridHeaderTest extends GridStaticSectionTest { assertEquals(GridBasicFeatures.COLUMNS - 2, cells.size()); assertText("Header (0,0)", cells.get(0)); - assertText("Header (0,2)", cells.get(1)); - assertText("Header (0,4)", cells.get(2)); + assertHTML("Header (0,2)", cells.get(1)); + assertHTML("Header (0,4)", cells.get(2)); selectMenuPath("Component", "Columns", "Column 3", "Visible"); @@ -81,9 +84,9 @@ public class GridHeaderTest extends GridStaticSectionTest { assertEquals(GridBasicFeatures.COLUMNS - 1, cells.size()); assertText("Header (0,0)", cells.get(0)); - assertText("Header (0,2)", cells.get(1)); + assertHTML("Header (0,2)", cells.get(1)); assertText("Header (0,3)", cells.get(2)); - assertText("Header (0,4)", cells.get(3)); + assertHTML("Header (0,4)", cells.get(3)); } @Test @@ -274,6 +277,74 @@ public class GridHeaderTest extends GridStaticSectionTest { assertEquals("2", spannedCell.getAttribute("colspan")); spannedCell = getGridElement().getHeaderCell(1, 3); assertEquals("3", spannedCell.getAttribute("colspan")); + + } + + @Test + public void testInitialCellTypes() throws Exception { + openTestURL(); + + GridCellElement textCell = getGridElement().getHeaderCell(0, 0); + assertEquals("Header (0,0)", textCell.getText()); + + GridCellElement widgetCell = getGridElement().getHeaderCell(0, 1); + assertTrue(widgetCell.isElementPresent(By.className("gwt-HTML"))); + + GridCellElement htmlCell = getGridElement().getHeaderCell(0, 2); + assertHTML("Header (0,2)", htmlCell); + } + + @Test + public void testDynamicallyChangingCellType() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Columns", "Column 0", "Header Type", + "Widget Header"); + GridCellElement widgetCell = getGridElement().getHeaderCell(0, 0); + assertTrue(widgetCell.isElementPresent(By.className("gwt-Button"))); + + selectMenuPath("Component", "Columns", "Column 1", "Header Type", + "HTML Header"); + GridCellElement htmlCell = getGridElement().getHeaderCell(0, 1); + assertHTML("HTML Header", htmlCell); + + selectMenuPath("Component", "Columns", "Column 2", "Header Type", + "Text Header"); + GridCellElement textCell = getGridElement().getHeaderCell(0, 2); + assertEquals("Text Header", textCell.getText()); + } + + @Test + public void testCellWidgetInteraction() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Columns", "Column 0", "Header Type", + "Widget Header"); + GridCellElement widgetCell = getGridElement().getHeaderCell(0, 0); + WebElement button = widgetCell.findElement(By.className("gwt-Button")); + + button.click(); + + assertEquals("Clicked", button.getText()); + } + + @Test + public void widgetInSortableCellInteraction() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Columns", "Column 0", "Header Type", + "Widget Header"); + + selectMenuPath("Component", "Columns", "Column 0", "Sortable"); + + GridCellElement widgetCell = getGridElement().getHeaderCell(0, 0); + WebElement button = widgetCell.findElement(By.className("gwt-Button")); + + assertNotEquals("Clicked", button.getText()); + + button.click(); + + assertEquals("Clicked", button.getText()); } private void assertHeaderCount(int count) { diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridStaticSectionTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridStaticSectionTest.java index 8f6739e16d..5fac9cf860 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridStaticSectionTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridStaticSectionTest.java @@ -30,7 +30,18 @@ public abstract class GridStaticSectionTest extends GridBasicClientFeaturesTest protected void assertHeaderTexts(int headerId, int rowIndex) { int i = 0; for (TestBenchElement cell : getGridElement().getHeaderCells(rowIndex)) { - assertText(String.format("Header (%d,%d)", headerId, i), cell); + + if (i % 3 == 0) { + assertText(String.format("Header (%d,%d)", headerId, i), cell); + } else if (i % 2 == 0) { + assertHTML(String.format("Header (%d,%d)", headerId, i), + cell); + } else { + assertHTML(String.format( + "

Header (%d,%d)
", + headerId, i), cell); + } + i++; } assertEquals("number of header columns", GridBasicFeatures.COLUMNS, i); @@ -39,7 +50,16 @@ public abstract class GridStaticSectionTest extends GridBasicClientFeaturesTest protected void assertFooterTexts(int footerId, int rowIndex) { int i = 0; for (TestBenchElement cell : getGridElement().getFooterCells(rowIndex)) { - assertText(String.format("Footer (%d,%d)", footerId, i), cell); + if (i % 3 == 0) { + assertText(String.format("Footer (%d,%d)", footerId, i), cell); + } else if (i % 2 == 0) { + assertHTML(String.format("Footer (%d,%d)", footerId, i), + cell); + } else { + assertHTML(String.format( + "
Footer (%d,%d)
", + footerId, i), cell); + } i++; } assertEquals("number of footer columns", GridBasicFeatures.COLUMNS, i); @@ -49,4 +69,20 @@ public abstract class GridStaticSectionTest extends GridBasicClientFeaturesTest // TBE.getText returns "" if the element is scrolled out of view assertEquals(text, e.getAttribute("innerHTML")); } + + protected static void assertHTML(String text, TestBenchElement e) { + String html = e.getAttribute("innerHTML"); + + // IE 8 returns tags as upper case while other browsers do not, make the + // comparison non-casesensive + html = html.toLowerCase(); + text = text.toLowerCase(); + + // IE 8 returns attributes without quotes, make the comparison without + // quotes + html = html.replaceAll("\"", ""); + text = html.replaceAll("\"", ""); + + assertEquals(text, html); + } } diff --git a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeatures.java b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeatures.java index 24cfe49239..50f60f9847 100644 --- a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeatures.java +++ b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeatures.java @@ -21,6 +21,10 @@ import java.util.List; import java.util.Random; import com.google.gwt.core.client.Scheduler.ScheduledCommand; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.HTML; import com.vaadin.client.ui.grid.FlyweightCell; import com.vaadin.client.ui.grid.Grid; import com.vaadin.client.ui.grid.Grid.SelectionMode; @@ -277,6 +281,7 @@ public class GridBasicClientFeatures extends !grid.getColumn(index).isSortable()); } }, "Component", "Columns", "Column " + i); + addMenuCommand("auto", new ScheduledCommand() { @Override public void execute() { @@ -295,6 +300,66 @@ public class GridBasicClientFeatures extends grid.getColumn(index).setWidth(200); } }, "Component", "Columns", "Column " + i, "Width"); + + // Header types + addMenuCommand("Text Header", new ScheduledCommand() { + @Override + public void execute() { + grid.getHeader().getRow(0).getCell(index) + .setText("Text Header"); + } + }, "Component", "Columns", "Column " + i, "Header Type"); + addMenuCommand("HTML Header", new ScheduledCommand() { + @Override + public void execute() { + grid.getHeader().getRow(0).getCell(index) + .setHtml("HTML Header"); + } + }, "Component", "Columns", "Column " + i, "Header Type"); + addMenuCommand("Widget Header", new ScheduledCommand() { + @Override + public void execute() { + final Button button = new Button("Button Header"); + button.addClickHandler(new ClickHandler() { + + @Override + public void onClick(ClickEvent event) { + button.setText("Clicked"); + } + }); + grid.getHeader().getRow(0).getCell(index).setWidget(button); + } + }, "Component", "Columns", "Column " + i, "Header Type"); + + // Footer types + addMenuCommand("Text Footer", new ScheduledCommand() { + @Override + public void execute() { + grid.getFooter().getRow(0).getCell(index) + .setText("Text Footer"); + } + }, "Component", "Columns", "Column " + i, "Footer Type"); + addMenuCommand("HTML Footer", new ScheduledCommand() { + @Override + public void execute() { + grid.getFooter().getRow(0).getCell(index) + .setHtml("HTML Footer"); + } + }, "Component", "Columns", "Column " + i, "Footer Type"); + addMenuCommand("Widget Footer", new ScheduledCommand() { + @Override + public void execute() { + final Button button = new Button("Button Footer"); + button.addClickHandler(new ClickHandler() { + + @Override + public void onClick(ClickEvent event) { + button.setText("Clicked"); + } + }); + grid.getFooter().getRow(0).getCell(index).setWidget(button); + } + }, "Component", "Columns", "Column " + i, "Footer Type"); } } @@ -303,14 +368,32 @@ public class GridBasicClientFeatures extends private void setHeaderTexts(HeaderRow row) { for (int i = 0; i < COLUMNS; ++i) { - row.getCell(i).setText("Header (" + headerCounter + "," + i + ")"); + String caption = "Header (" + headerCounter + "," + i + ")"; + + // Lets use some different cell types + if (i % 3 == 0) { + row.getCell(i).setText(caption); + } else if (i % 2 == 0) { + row.getCell(i).setHtml("" + caption + ""); + } else { + row.getCell(i).setWidget(new HTML(caption)); + } } headerCounter++; } private void setFooterTexts(FooterRow row) { for (int i = 0; i < COLUMNS; ++i) { - row.getCell(i).setText("Footer (" + footerCounter + "," + i + ")"); + String caption = "Footer (" + footerCounter + "," + i + ")"; + + // Lets use some different cell types + if (i % 3 == 0) { + row.getCell(i).setText(caption); + } else if (i % 2 == 0) { + row.getCell(i).setHtml("" + caption + ""); + } else { + row.getCell(i).setWidget(new HTML(caption)); + } } footerCounter++; } @@ -449,6 +532,7 @@ public class GridBasicClientFeatures extends addMenuCommand("Remove bottom row", new ScheduledCommand() { @Override public void execute() { + assert footer.getRowCount() > 0; footer.removeRow(footer.getRowCount() - 1); } }, menuPath); -- 2.39.5