diff options
4 files changed, 329 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index a6cac01fdc..d3578d8f98 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -37,6 +37,7 @@ import java.util.logging.Logger; import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; import com.google.gwt.thirdparty.guava.common.collect.Sets; import com.google.gwt.thirdparty.guava.common.collect.Sets.SetView; @@ -95,6 +96,7 @@ import com.vaadin.shared.ui.grid.ScrollDestination; import com.vaadin.shared.util.SharedUtil; import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.DesignException; import com.vaadin.ui.renderers.Renderer; import com.vaadin.ui.renderers.TextRenderer; import com.vaadin.util.ReflectTools; @@ -2665,6 +2667,80 @@ public class Grid extends AbstractComponent implements SelectionNotifier, public Field<?> getEditorField() { return grid.getEditorField(getPropertyId()); } + + /** + * Writes the design attributes for this column into given element. + * + * @since + * @param design + * Element to write attributes into + * @param designContext + * the design context + */ + protected void writeDesign(Element design, DesignContext designContext) { + Attributes attributes = design.attributes(); + GridColumnState def = new GridColumnState(); + // Sortable is a special attribute that depends on the container. + DesignAttributeHandler.writeAttribute("sortable", attributes, + isSortable(), null, boolean.class); + DesignAttributeHandler.writeAttribute("editable", attributes, + isEditable(), def.editable, boolean.class); + DesignAttributeHandler.writeAttribute("width", attributes, + getWidth(), def.width, Double.class); + DesignAttributeHandler.writeAttribute("min-width", attributes, + getMinimumWidth(), def.minWidth, Double.class); + DesignAttributeHandler.writeAttribute("max-width", attributes, + getMaximumWidth(), def.maxWidth, Double.class); + DesignAttributeHandler.writeAttribute("expand", attributes, + getExpandRatio(), def.expandRatio, Integer.class); + DesignAttributeHandler.writeAttribute("property-id", attributes, + getPropertyId(), null, Object.class); + } + + /** + * Reads the design attributes for this column from given element. + * + * @since + * @param design + * Element to read attributes from + * @param designContext + * the design context + */ + protected void readDesign(Element design, DesignContext designContext) { + Attributes attributes = design.attributes(); + + if (design.hasAttr("sortable")) { + setSortable(DesignAttributeHandler.readAttribute("sortable", + attributes, boolean.class)); + } + + if (design.hasAttr("editable")) { + setEditable(DesignAttributeHandler.readAttribute("editable", + attributes, boolean.class)); + } + + // Read size info where necessary. + if (design.hasAttr("width")) { + setWidth(DesignAttributeHandler.readAttribute("width", + attributes, Double.class)); + } + if (design.hasAttr("min-width")) { + setMinimumWidth(DesignAttributeHandler.readAttribute( + "min-width", attributes, Double.class)); + } + if (design.hasAttr("max-width")) { + setMaximumWidth(DesignAttributeHandler.readAttribute( + "max-width", attributes, Double.class)); + } + if (design.hasAttr("expand")) { + if (design.attr("expand").isEmpty()) { + setExpandRatio(1); + } else { + setExpandRatio(DesignAttributeHandler.readAttribute( + "expand", attributes, Integer.class)); + } + } + } } /** @@ -5132,6 +5208,31 @@ public class Grid extends AbstractComponent implements SelectionNotifier, setSelectionMode(DesignAttributeHandler.readAttribute( "selection-mode", attrs, SelectionMode.class)); } + + if (design.children().size() > 0) { + if (design.children().size() > 1 + || !design.child(0).tagName().equals("table")) { + throw new DesignException( + "Grid needs to have a table element as its only child"); + } + Element table = design.child(0); + + Elements colgroups = table.getElementsByTag("colgroup"); + if (colgroups.size() != 1) { + throw new DesignException( + "Table element in declarative Grid needs to have a" + + " colgroup defining the columns used in Grid"); + } + + int i = 0; + for (Element col : colgroups.get(0).getElementsByTag("col")) { + String propertyId = DesignAttributeHandler.readAttribute( + "property-id", col.attributes(), "property-" + i, + String.class); + addColumn(propertyId, String.class).readDesign(col, context); + ++i; + } + } } @Override @@ -5168,6 +5269,22 @@ public class Grid extends AbstractComponent implements SelectionNotifier, DesignAttributeHandler.writeAttribute("selection-mode", attrs, selectionMode, getDefaultSelectionMode(), SelectionMode.class); + if (columns.isEmpty()) { + // Empty grid. Structure not needed. + return; + } + + // Do structure. + Element tableElement = design.appendElement("table"); + Element colGroup = tableElement.appendElement("colgroup"); + + List<Column> columnOrder = getColumns(); + for (int i = 0; i < columnOrder.size(); ++i) { + Column column = columnOrder.get(i); + Element colElement = colGroup.appendElement("col"); + column.writeDesign(colElement, context); + } + } @Override diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridColumnDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridColumnDeclarativeTest.java new file mode 100644 index 0000000000..735a1ab502 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridColumnDeclarativeTest.java @@ -0,0 +1,91 @@ +/* + * 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.server.component.grid.declarative; + +import org.junit.Test; + +import com.vaadin.ui.Grid; + +public class GridColumnDeclarativeTest extends GridDeclarativeTestBase { + + @Test + public void testSimpleGridColumns() { + String design = "<v-grid><table>"// + + "<colgroup>" + + " <col sortable=true width='100' property-id='Column1'>" + + " <col sortable=false max-width='200' expand='2' property-id='Column2'>" + + " <col sortable=true editable=false min-width='15' expand='1' property-id='Column3'>" + + "</colgroup>" // + + "</table></v-grid>"; + Grid grid = new Grid(); + grid.addColumn("Column1", String.class).setWidth(100); + grid.addColumn("Column2", String.class).setMaximumWidth(200) + .setExpandRatio(2).setSortable(false); + grid.addColumn("Column3", String.class).setMinimumWidth(15) + .setExpandRatio(1).setEditable(false); + + // Use the read grid component to do another pass on write. + testRead(design, grid, true); + testWrite(design, grid); + } + + @Test + public void testReadColumnsWithoutPropertyId() { + String design = "<v-grid><table>"// + + "<colgroup>" + + " <col sortable=true width='100' property-id='Column1'>" + + " <col sortable=true max-width='200' expand='2'>" // property-id="property-1" + + " <col sortable=true min-width='15' expand='1' property-id='Column3'>" + + "</colgroup>" // + + "</table></v-grid>"; + Grid grid = new Grid(); + grid.addColumn("Column1", String.class).setWidth(100); + grid.addColumn("property-1", String.class).setMaximumWidth(200) + .setExpandRatio(2); + grid.addColumn("Column3", String.class).setMinimumWidth(15) + .setExpandRatio(1); + + testRead(design, grid); + } + + @Test + public void testReadEmptyExpand() { + String design = "<v-grid><table>"// + + "<colgroup>" + + " <col sortable=true expand />" + + "</colgroup>" // + + "</table></v-grid>"; + + Grid grid = new Grid(); + grid.addColumn("property-0", String.class).setExpandRatio(1); + + testRead(design, grid); + } + + @Test + public void testReadColumnWithNoAttributes() { + String design = "<v-grid><table>"// + + "<colgroup>" // + + " <col />" // + + "</colgroup>" // + + "</table></v-grid>"; + + Grid grid = new Grid(); + grid.addColumn("property-0", String.class); + + testRead(design, grid); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeTestBase.java b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeTestBase.java new file mode 100644 index 0000000000..56f4647e90 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeTestBase.java @@ -0,0 +1,71 @@ +/* + * 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.server.component.grid.declarative; + +import java.util.List; + +import org.junit.Assert; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.Column; + +public class GridDeclarativeTestBase extends DeclarativeTestBase<Grid> { + + @Override + public Grid testRead(String design, Grid expected) { + return testRead(design, expected, false); + } + + public Grid testRead(String design, Grid expected, boolean retestWrite) { + Grid readGrid = super.testRead(design, expected); + + compareGridColumns(expected, readGrid); + + if (retestWrite) { + testWrite(design, readGrid); + } + + return readGrid; + } + + private void compareGridColumns(Grid expected, Grid actual) { + List<Column> columns = expected.getColumns(); + List<Column> actualColumns = actual.getColumns(); + Assert.assertEquals("Different amount of columns", columns.size(), + actualColumns.size()); + for (int i = 0; i < columns.size(); ++i) { + Column col1 = columns.get(i); + Column col2 = actualColumns.get(i); + String baseError = "Error when comparing columns for property " + + col1.getPropertyId() + ": "; + assertEquals(baseError + "Property id", col1.getPropertyId(), + col2.getPropertyId()); + assertEquals(baseError + "Width", col1.getWidth(), col2.getWidth()); + assertEquals(baseError + "Maximum width", col1.getMaximumWidth(), + col2.getMaximumWidth()); + assertEquals(baseError + "Minimum width", col1.getMinimumWidth(), + col2.getMinimumWidth()); + assertEquals(baseError + "Expand ratio", col1.getExpandRatio(), + col2.getExpandRatio()); + assertEquals(baseError + "Sortable", col1.isSortable(), + col2.isSortable()); + assertEquals(baseError + "Editable", col1.isEditable(), + col2.isEditable()); + + } + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridStructureDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridStructureDeclarativeTest.java new file mode 100644 index 0000000000..dc74f46d4d --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridStructureDeclarativeTest.java @@ -0,0 +1,50 @@ +/* + * 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.server.component.grid.declarative; + +import org.junit.Test; + +import com.vaadin.ui.Grid; +import com.vaadin.ui.declarative.DesignException; + +public class GridStructureDeclarativeTest extends GridDeclarativeTestBase { + + @Test + public void testReadEmptyGrid() { + String design = "<v-grid />"; + testRead(design, new Grid(), false); + } + + @Test + public void testEmptyGrid() { + String design = "<v-grid></v-grid>"; + Grid expected = new Grid(); + testWrite(design, expected); + testRead(design, expected, true); + } + + @Test(expected = DesignException.class) + public void testMalformedGrid() { + String design = "<v-grid><v-label /></v-grid>"; + testRead(design, new Grid()); + } + + @Test(expected = DesignException.class) + public void testGridWithNoColGroup() { + String design = "<v-grid><table><thead><tr><th>Foo</tr></thead></table></v-grid>"; + testRead(design, new Grid()); + } +} |