diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2015-04-16 14:39:31 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-04-20 07:26:13 +0000 |
commit | 4845b804fd09163c70cf7ecac9c26e91a6cb46c9 (patch) | |
tree | 531cc5578f4564b0a6b3e403067e137537099f99 /server | |
parent | be90c6d21607d504cc559884c5f7df49289def30 (diff) | |
download | vaadin-framework-4845b804fd09163c70cf7ecac9c26e91a6cb46c9.tar.gz vaadin-framework-4845b804fd09163c70cf7ecac9c26e91a6cb46c9.zip |
Declarative write support for Table (#16367)
Change-Id: I9caa766be3152b35f7fe354b989e8cd6d615a043
Diffstat (limited to 'server')
4 files changed, 138 insertions, 22 deletions
diff --git a/server/src/com/vaadin/ui/Table.java b/server/src/com/vaadin/ui/Table.java index 202e58f0c9..eb3d35be3e 100644 --- a/server/src/com/vaadin/ui/Table.java +++ b/server/src/com/vaadin/ui/Table.java @@ -6175,12 +6175,115 @@ public class Table extends AbstractSelect implements Action.Container, } @Override + public void writeDesign(Element design, DesignContext context) { + Table def = context.getDefaultInstance(this); + + DesignAttributeHandler.writeAttribute("sortable", design.attributes(), + isSortEnabled(), def.isSortEnabled(), boolean.class); + + Element table = null; + boolean hasColumns = getVisibleColumns().length != 0; + if (hasColumns) { + table = design.appendElement("table"); + writeColumns(table, def); + writeHeader(table, def); + } + super.writeDesign(design, context); + if (hasColumns) { + writeFooter(table); + } + } + + private void writeColumns(Element table, Table def) { + Object[] columns = getVisibleColumns(); + if (columns.length == 0) { + return; + } + + Element colgroup = table.appendElement("colgroup"); + for (Object id : columns) { + Element col = colgroup.appendElement("col"); + + col.attr("property-id", id.toString()); + + if (getColumnAlignment(id) == Align.CENTER) { + col.attr("center", ""); + } else if (getColumnAlignment(id) == Align.RIGHT) { + col.attr("right", ""); + } + + DesignAttributeHandler.writeAttribute("width", col.attributes(), + getColumnWidth(id), def.getColumnWidth(null), int.class); + + DesignAttributeHandler.writeAttribute("expand", col.attributes(), + getColumnExpandRatio(id), def.getColumnExpandRatio(null), + float.class); + + DesignAttributeHandler.writeAttribute("collapsible", + col.attributes(), isColumnCollapsible(id), + def.isColumnCollapsible(null), boolean.class); + + DesignAttributeHandler.writeAttribute("collapsed", + col.attributes(), isColumnCollapsed(id), + def.isColumnCollapsed(null), boolean.class); + } + } + + private void writeHeader(Element table, Table def) { + Object[] columns = getVisibleColumns(); + if (columns.length == 0 + || (columnIcons.isEmpty() && columnHeaders.isEmpty())) { + return; + } + + Element header = table.appendElement("thead").appendElement("tr"); + for (Object id : columns) { + Element th = header.appendElement("th"); + th.html(getColumnHeader(id)); + DesignAttributeHandler.writeAttribute("icon", th.attributes(), + getColumnIcon(id), def.getColumnIcon(null), Resource.class); + } + + } + + private void writeFooter(Element table) { + Object[] columns = getVisibleColumns(); + if (columns.length == 0 || columnFooters.isEmpty()) { + return; + } + + Element footer = table.appendElement("tfoot").appendElement("tr"); + for (Object id : columns) { + footer.appendElement("td").text(getColumnFooter(id)); + } + } + + @Override + protected void writeItems(Element design, DesignContext context) { + Element tbody = design.child(0).appendElement("tbody"); + super.writeItems(tbody, context); + } + + @Override + protected Element writeItem(Element tbody, Object itemId, + DesignContext context) { + Element tr = tbody.appendElement("tr"); + Item item = getItem(itemId); + for (Object id : getVisibleColumns()) { + Element td = tr.appendElement("td"); + td.html(item.getItemProperty(id).getValue().toString()); + } + return tr; + } + + @Override protected Collection<String> getCustomAttributes() { Collection<String> result = super.getCustomAttributes(); result.add("sortable"); result.add("sort-enabled"); result.add("sort-disabled"); result.add("footer-visible"); + result.add("item-caption-mode"); result.add("current-page-first-item-id"); result.add("current-page-first-item-index"); return result; diff --git a/server/tests/src/com/vaadin/tests/design/DeclarativeTestBaseBase.java b/server/tests/src/com/vaadin/tests/design/DeclarativeTestBaseBase.java index 3afef0e2ee..f0714ef3bd 100644 --- a/server/tests/src/com/vaadin/tests/design/DeclarativeTestBaseBase.java +++ b/server/tests/src/com/vaadin/tests/design/DeclarativeTestBaseBase.java @@ -94,7 +94,7 @@ public abstract class DeclarativeTestBaseBase<T extends Component> { Assert.assertEquals(message + ": array length", a1.length, a2.length); for (int i = 0; i < a1.length; i++) { - assertEquals(message, a1[i], a2[i]); + assertEquals(message + ": element " + i, a1[i], a2[i]); } return; } diff --git a/server/tests/src/com/vaadin/tests/server/component/table/TableDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/table/TableDeclarativeTest.java index 27888dee14..796d30049e 100644 --- a/server/tests/src/com/vaadin/tests/server/component/table/TableDeclarativeTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/table/TableDeclarativeTest.java @@ -15,8 +15,6 @@ */ package com.vaadin.tests.server.component.table; -import java.io.UnsupportedEncodingException; - import org.junit.Test; import com.vaadin.server.ExternalResource; @@ -38,28 +36,31 @@ public class TableDeclarativeTest extends TableDeclarativeTestBase { @Test public void testBasicAttributes() { - String design = "<v-table page-length=30 cache-rate=3 selectable editable sortable=false " - + "drag-mode=row multi-select-mode=simple column-header-mode=id " - + "column-reordering-allowed column-collapsing-allowed sort-ascending=false " - + "row-header-mode=id sort-container-property-id=foo />"; + String design = "<v-table page-length=30 cache-rate=3 selectable=true editable=true " + + "sortable=false sort-ascending=false sort-container-property-id=foo " + + "drag-mode=row multi-select-mode=simple column-header-mode=id row-header-mode=id " + + "column-reordering-allowed=true column-collapsing-allowed=true />"; Table table = new Table(); table.setPageLength(30); table.setCacheRate(3); table.setSelectable(true); table.setEditable(true); + table.setSortEnabled(false); + table.setSortAscending(false); + table.setSortContainerPropertyId("foo"); + table.setDragMode(TableDragMode.ROW); table.setMultiSelectMode(MultiSelectMode.SIMPLE); table.setColumnHeaderMode(ColumnHeaderMode.ID); table.setRowHeaderMode(RowHeaderMode.ID); + table.setColumnReorderingAllowed(true); table.setColumnCollapsingAllowed(true); - table.setSortAscending(false); - table.setSortContainerPropertyId("foo"); testRead(design, table); - // testWrite(design, table); + testWrite(design, table); } @Test @@ -67,7 +68,7 @@ public class TableDeclarativeTest extends TableDeclarativeTestBase { String design = "<v-table column-collapsing-allowed=true>" // + " <table>" // + " <colgroup>" - + " <col property-id='foo'>" + + " <col property-id='foo' width=300>" + " <col property-id='bar' center expand=1 collapsible=false>" + " <col property-id='baz' right expand=2 collapsed=true>" + " </colgroup>" // @@ -79,7 +80,7 @@ public class TableDeclarativeTest extends TableDeclarativeTestBase { table.addContainerProperty("foo", String.class, null); table.setColumnAlignment("foo", Align.LEFT); - table.setColumnExpandRatio("foo", 0); + table.setColumnWidth("foo", 300); table.addContainerProperty("bar", String.class, null); table.setColumnAlignment("bar", Align.CENTER); @@ -92,7 +93,7 @@ public class TableDeclarativeTest extends TableDeclarativeTestBase { table.setColumnCollapsed("baz", true); testRead(design, table); - // testWrite(design, table); + testWrite(design, table); } @Test @@ -123,15 +124,15 @@ public class TableDeclarativeTest extends TableDeclarativeTestBase { table.setColumnFooter("bar", "bar"); testRead(design, table); - // testWrite(design, table); + testWrite(design, table); } @Test - public void testInlineData() throws UnsupportedEncodingException { - String design = "<v-table footer-visible=true> "// + public void testInlineData() { + String design = "<v-table> "// + " <table>" // + " <colgroup>" - + " <col property-id='foo' width=150 />" + + " <col property-id='foo' />" + " <col property-id='bar' />" + " <col property-id='baz' />" // + " </colgroup>" @@ -147,6 +148,7 @@ public class TableDeclarativeTest extends TableDeclarativeTestBase { + " </tfoot>" // + " </table>" // + "</v-table>"; + Table table = new Table(); table.addContainerProperty("foo", String.class, null); table.addContainerProperty("bar", String.class, null); @@ -158,8 +160,8 @@ public class TableDeclarativeTest extends TableDeclarativeTestBase { table.addItem(new Object[] { "r1c1", "r1c2", "r1c3" }, null); table.addItem(new Object[] { "r2c1", "r2c2", "r2c3" }, null); table.setFooterVisible(true); - testRead(design, table); - // testWrite(design, table); + testRead(design, table); + testWrite(design, table, true); } } diff --git a/server/tests/src/com/vaadin/tests/server/component/table/TableDeclarativeTestBase.java b/server/tests/src/com/vaadin/tests/server/component/table/TableDeclarativeTestBase.java index 7f803c7f8b..65c9475684 100644 --- a/server/tests/src/com/vaadin/tests/server/component/table/TableDeclarativeTestBase.java +++ b/server/tests/src/com/vaadin/tests/server/component/table/TableDeclarativeTestBase.java @@ -25,7 +25,7 @@ public class TableDeclarativeTestBase extends DeclarativeTestBase<Table> { @Override public Table testRead(String design, Table expected) { Table read = super.testRead(design, expected); - compareFooter(read, expected); + compareColumns(read, expected); compareBody(read, expected); return read; } @@ -43,9 +43,20 @@ public class TableDeclarativeTestBase extends DeclarativeTestBase<Table> { } } - private void compareFooter(Table read, Table expected) { + private void compareColumns(Table read, Table expected) { for (Object pid : expected.getVisibleColumns()) { - assertEquals(expected.getColumnFooter(pid), + String col = "column '" + pid + "'"; + assertEquals(col + " width", expected.getColumnWidth(pid), + read.getColumnWidth(pid)); + assertEquals(col + " expand ratio", + expected.getColumnExpandRatio(pid), + read.getColumnExpandRatio(pid)); + assertEquals(col + " collapsible", + expected.isColumnCollapsible(pid), + read.isColumnCollapsible(pid)); + assertEquals(col + " collapsed", expected.isColumnCollapsed(pid), + read.isColumnCollapsed(pid)); + assertEquals(col + " footer", expected.getColumnFooter(pid), read.getColumnFooter(pid)); } } |