diff --git a/README.md b/README.md index 86382c1..f083023 100644 --- a/README.md +++ b/README.md @@ -81,12 +81,20 @@ Unless changed, after setting each field according to its attributes, the object * if the value implements `HasText`, its `setText` method will be called with the text found in the element; * if the value is an `Icon`, its server-side `Icon` will be changed to reflect the one specified in the element; * if the value is a `Button` and the element has an icon, the icon will be set on the server side; -* if the value is a `Grid` and it is declared in the file as `Grid`, the field will be configured as if `new Grid<>(BeanType.class)` was called. +* if the value is a `Grid` and it is declared in the file as `Grid`, the field will be configured as if `new Grid<>(BeanType.class)` was called; in addition columns can be added (see below). The default configurators can be skipped by: * passing a `false` flag to the `SuperTemplate` constructor - for that particular template only; * calling `TemplateFieldConfigurators.DEFAULT_CONFIGURATORS.clear()` - for all templates (does not apply retroactively to templates that were created before executing that code). +### Adding columns to `Grid` + +Under certain conditions columns can be specified in the design. First of all, the `Grid` must be typed in Java (so `@Id("id-of-grid") private Grid grid`). Then: +* (**recommended**) columns from `data-columns` attribute on `vaadin-grid`, if that attribute is present; the list should be space-separated list of properties, and for each property server-side object will use `addColumn(propertyName)` +* (**experimental**) columns from `vaadin-grid-column` are taken if they have an attribute `data-column`; again, server-side object will use `addcolumn(name)` + +Feel free to file an issue if there are problems with either of the approaches. + ## Extending default behaviour ### For all templates diff --git a/pom.xml b/pom.xml index a3a4f42..bbc010a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.vaadin.miki super-template - 0.2.0 + 0.2.1 Super Template An extension of PolymerTemplate that reads properties from the template and makes them available on the server side. Requires Vaadin 14.X. diff --git a/src/main/java/org/vaadin/miki/supertemplate/GridBeanTypeConfigurator.java b/src/main/java/org/vaadin/miki/supertemplate/GridBeanTypeConfigurator.java index 3ac32d5..c8f6811 100644 --- a/src/main/java/org/vaadin/miki/supertemplate/GridBeanTypeConfigurator.java +++ b/src/main/java/org/vaadin/miki/supertemplate/GridBeanTypeConfigurator.java @@ -4,20 +4,25 @@ import com.vaadin.flow.component.polymertemplate.PolymerTemplate; import com.vaadin.flow.data.binder.BeanPropertySet; import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; +import java.util.stream.Stream; /** - * Configures bean type of a field of type {@link Grid}. + * Configures bean type of a field of type {@link Grid} and optionally its property columns, if any are present. * @author miki * @since 2020-05-05 */ public class GridBeanTypeConfigurator implements TemplateFieldConfigurator { + public static final String DATA_COLUMNS_ATTRIBUTE = "data-columns"; + public static final String DATA_COLUMN_ATTRIBUTE = "data-column"; + private static final Logger LOGGER = LoggerFactory.getLogger(GridBeanTypeConfigurator.class); @Override @@ -34,6 +39,20 @@ public void configureFieldValue(Field field, Object value, PolymerTemplate te Field propertySetField = Grid.class.getDeclaredField("propertySet"); propertySetField.setAccessible(true); propertySetField.set(value, BeanPropertySet.get(dataType)); + + // try to get columns from the element + if(element.hasAttr(DATA_COLUMNS_ATTRIBUTE)) + Stream.of(element.attr(DATA_COLUMNS_ATTRIBUTE).split("\\s+")).forEach(((Grid) value)::addColumn); + final Elements columns = element.getElementsByTag("vaadin-grid-column"); + columns.stream().filter(column -> column.hasAttr(DATA_COLUMN_ATTRIBUTE)).forEach(column -> { + final String id = column.attr(DATA_COLUMN_ATTRIBUTE); + final int whereIsDot = id.indexOf('.'); + if(whereIsDot == -1) + ((Grid)value).addColumn(id); + else if(dataType.getSimpleName().equals(id.substring(0, whereIsDot))) + ((Grid)value).addColumn(id.substring(whereIsDot+1)); + }); + } catch(ClassCastException cce) { LOGGER.info("Could not discover bean type for field {}. Please make it Grid.", field.getName()); diff --git a/src/test/java/org/vaadin/miki/supertemplate/Dummy.java b/src/test/java/org/vaadin/miki/supertemplate/Dummy.java index 8f7331e..af5d896 100644 --- a/src/test/java/org/vaadin/miki/supertemplate/Dummy.java +++ b/src/test/java/org/vaadin/miki/supertemplate/Dummy.java @@ -9,6 +9,10 @@ public class Dummy { private String contents; + private String description; + + private boolean flagged; + public String getContents() { return contents; } @@ -17,16 +21,34 @@ public void setContents(String contents) { this.contents = contents; } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public boolean isFlagged() { + return flagged; + } + + public void setFlagged(boolean flagged) { + this.flagged = flagged; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Dummy dummy = (Dummy) o; - return Objects.equals(getContents(), dummy.getContents()); + return isFlagged() == dummy.isFlagged() && + Objects.equals(getContents(), dummy.getContents()) && + Objects.equals(getDescription(), dummy.getDescription()); } @Override public int hashCode() { - return Objects.hash(getContents()); + return Objects.hash(getContents(), getDescription(), isFlagged()); } } diff --git a/src/test/java/org/vaadin/miki/supertemplate/SuperTemplateTest.java b/src/test/java/org/vaadin/miki/supertemplate/SuperTemplateTest.java index 8fa6223..0dad21c 100644 --- a/src/test/java/org/vaadin/miki/supertemplate/SuperTemplateTest.java +++ b/src/test/java/org/vaadin/miki/supertemplate/SuperTemplateTest.java @@ -51,6 +51,10 @@ public void testSuperViewHasPropertiesFromTemplate() { Grid grid = superView.getGrid(); Assert.assertNotNull(grid); Assert.assertSame(Dummy.class, grid.getBeanType()); + // there should be two columns already in the grid: description and flagged, added in that order + Assert.assertEquals(2, grid.getColumns().size()); + Assert.assertEquals("description", grid.getColumns().get(0).getKey()); + Assert.assertEquals("flagged", grid.getColumns().get(1).getKey()); Assert.assertNotNull(grid.addColumn("contents")); // adding columns should work } diff --git a/src/test/resources/test-view.js b/src/test/resources/test-view.js index 6720ec9..16bf627 100644 --- a/src/test/resources/test-view.js +++ b/src/test/resources/test-view.js @@ -13,7 +13,9 @@ export class TestView extends PolymerElement { World - + + + `; }