diff --git a/README.md b/README.md
index dde1d6d..86382c1 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# SuperTemplate
-This is an extension of Vaadin 14.X `PolymerTemplate` class. It enables server-side access to properties defined in the template file, which is normally not possible.
+This is an extension of Vaadin 14.X `PolymerTemplate` class. It enables server-side access to properties defined in the template file, which is normally not possible. It also attempts to solve a few general issues with templates.
## Limitations
@@ -19,7 +19,7 @@ Maven dependency:
org.vaadin.miki
super-template
- 0.1.0
+ 0.2.0
```
@@ -77,10 +77,11 @@ For each attribute:
## Default behaviour
-Unless changed, after setting each field according to its attributes, the object will go through extra configuration. This is defined by `TemplateFieldConfigurator` and by default three of those are available:
+Unless changed, after setting each field according to its attributes, the object will go through extra configuration. This is defined by `TemplateFieldConfigurator` and by default those are available:
* 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 `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.
The default configurators can be skipped by:
* passing a `false` flag to the `SuperTemplate` constructor - for that particular template only;
diff --git a/pom.xml b/pom.xml
index 84fda70..a3a4f42 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
org.vaadin.miki
super-template
- 0.2-SNAPSHOT
+ 0.2.0
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
new file mode 100644
index 0000000..3ac32d5
--- /dev/null
+++ b/src/main/java/org/vaadin/miki/supertemplate/GridBeanTypeConfigurator.java
@@ -0,0 +1,45 @@
+package org.vaadin.miki.supertemplate;
+
+import com.vaadin.flow.component.grid.Grid;
+import com.vaadin.flow.component.polymertemplate.PolymerTemplate;
+import com.vaadin.flow.data.binder.BeanPropertySet;
+import org.jsoup.nodes.Element;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.ParameterizedType;
+
+/**
+ * Configures bean type of a field of type {@link Grid}.
+
+ * @author miki
+ * @since 2020-05-05
+ */
+public class GridBeanTypeConfigurator implements TemplateFieldConfigurator {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(GridBeanTypeConfigurator.class);
+
+ @Override
+ public void configureFieldValue(Field field, Object value, PolymerTemplate> template, Element element) {
+ if(value instanceof Grid) {
+ try {
+ Class> dataType = (Class>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
+ LOGGER.info("Discovered field {} to be of type Grid<{}>", field.getName(), dataType.getName());
+
+ Field beanTypeField = Grid.class.getDeclaredField("beanType");
+ if(beanTypeField.trySetAccessible())
+ beanTypeField.set(value, dataType);
+
+ Field propertySetField = Grid.class.getDeclaredField("propertySet");
+ propertySetField.setAccessible(true);
+ propertySetField.set(value, BeanPropertySet.get(dataType));
+ }
+ catch(ClassCastException cce) {
+ LOGGER.info("Could not discover bean type for field {}. Please make it Grid.", field.getName());
+ } catch (IllegalAccessException | NoSuchFieldException e) {
+ LOGGER.warn("Could not access Grid.beanType and Grid.propertySet. Field {} will not work properly.", field.getName());
+ }
+ }
+ }
+}
diff --git a/src/main/java/org/vaadin/miki/supertemplate/TemplateFieldConfigurators.java b/src/main/java/org/vaadin/miki/supertemplate/TemplateFieldConfigurators.java
index 4f16d5f..f80a8fa 100644
--- a/src/main/java/org/vaadin/miki/supertemplate/TemplateFieldConfigurators.java
+++ b/src/main/java/org/vaadin/miki/supertemplate/TemplateFieldConfigurators.java
@@ -45,12 +45,17 @@ private TemplateFieldConfigurators() {} // instances not allowed
// buttons require extra processing, they may have an icon
public static final TemplateFieldConfigurator UPDATE_BUTTON_ICON = new UpdateButtonIconConfigurator();
+ /**
+ * Updates a grid to have a bean type, if declared in the Java class through generics.
+ */
+ public static final TemplateFieldConfigurator GRID_BEAN_TYPE = new GridBeanTypeConfigurator();
+
/**
* Contains all of the {@link TemplateFieldConfigurator}s defined as {@code public static final} fields in this class.
* This array can be modified and it will affect all templates.
*/
public static final Collection DEFAULT_CONFIGURATORS = new ArrayList<>(Arrays.asList(
- SET_TEXT, CREATE_STANDALONE_ICON, UPDATE_BUTTON_ICON
+ SET_TEXT, CREATE_STANDALONE_ICON, UPDATE_BUTTON_ICON, GRID_BEAN_TYPE
));
}
diff --git a/src/test/java/org/vaadin/miki/supertemplate/Dummy.java b/src/test/java/org/vaadin/miki/supertemplate/Dummy.java
new file mode 100644
index 0000000..8f7331e
--- /dev/null
+++ b/src/test/java/org/vaadin/miki/supertemplate/Dummy.java
@@ -0,0 +1,32 @@
+package org.vaadin.miki.supertemplate;
+
+import java.util.Objects;
+
+/**
+ * A dummy data type.
+ */
+public class Dummy {
+
+ private String contents;
+
+ public String getContents() {
+ return contents;
+ }
+
+ public void setContents(String contents) {
+ this.contents = contents;
+ }
+
+ @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());
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(getContents());
+ }
+}
diff --git a/src/test/java/org/vaadin/miki/supertemplate/SuperTemplateTest.java b/src/test/java/org/vaadin/miki/supertemplate/SuperTemplateTest.java
index ceda69a..8fa6223 100644
--- a/src/test/java/org/vaadin/miki/supertemplate/SuperTemplateTest.java
+++ b/src/test/java/org/vaadin/miki/supertemplate/SuperTemplateTest.java
@@ -2,6 +2,7 @@
import com.github.mvysny.kaributesting.v10.MockNpmTemplateParser;
import com.github.mvysny.kaributesting.v10.MockVaadin;
+import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.textfield.TextField;
import org.junit.After;
@@ -47,6 +48,10 @@ public void testSuperViewHasPropertiesFromTemplate() {
Assert.assertNotNull(span);
Assert.assertEquals("a-span", span.getId().orElse(null));
Assert.assertEquals("World", span.getText());
+ Grid grid = superView.getGrid();
+ Assert.assertNotNull(grid);
+ Assert.assertSame(Dummy.class, grid.getBeanType());
+ Assert.assertNotNull(grid.addColumn("contents")); // adding columns should work
}
@Test
@@ -59,4 +64,13 @@ public void testVaadinViewDoesNotHavePropertiesFromTemplate() {
Assert.assertFalse(vaadinView.getSpan().getId().isPresent());
}
+ @Test(expected = UnsupportedOperationException.class)
+ public void testVaadinViewDoesNotHaveProperGrid() {
+ VaadinView vaadinView = new VaadinView();
+ Assert.assertNotNull(vaadinView);
+ Assert.assertNotNull(vaadinView.getGrid());
+ Assert.assertNull(vaadinView.getGrid().getBeanType());
+ vaadinView.getGrid().addColumn("contents");
+ }
+
}
\ No newline at end of file
diff --git a/src/test/java/org/vaadin/miki/supertemplate/TestView.java b/src/test/java/org/vaadin/miki/supertemplate/TestView.java
index 71b6964..ac0f890 100644
--- a/src/test/java/org/vaadin/miki/supertemplate/TestView.java
+++ b/src/test/java/org/vaadin/miki/supertemplate/TestView.java
@@ -2,6 +2,7 @@
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
+import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.polymertemplate.Id;
import com.vaadin.flow.component.textfield.TextField;
@@ -17,6 +18,9 @@ public class TestView extends SuperTemplate {
@Id("a-span")
private Span span;
+ @Id("a-grid")
+ private Grid grid;
+
TextField getTextField() {
return textField;
}
@@ -25,6 +29,10 @@ Span getSpan() {
return span;
}
+ Grid getGrid() {
+ return grid;
+ }
+
public interface TestModel extends TemplateModel {} // nothing here
}
diff --git a/src/test/java/org/vaadin/miki/supertemplate/VaadinView.java b/src/test/java/org/vaadin/miki/supertemplate/VaadinView.java
index 0b98407..93db239 100644
--- a/src/test/java/org/vaadin/miki/supertemplate/VaadinView.java
+++ b/src/test/java/org/vaadin/miki/supertemplate/VaadinView.java
@@ -2,6 +2,7 @@
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
+import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.polymertemplate.Id;
import com.vaadin.flow.component.polymertemplate.PolymerTemplate;
@@ -18,6 +19,9 @@ public class VaadinView extends PolymerTemplate {
@Id("a-span")
private Span span;
+ @Id("a-grid")
+ private Grid grid;
+
TextField getTextField() {
return textField;
}
@@ -26,6 +30,10 @@ Span getSpan() {
return span;
}
+ Grid getGrid() {
+ return grid;
+ }
+
public interface TestModel extends TemplateModel {} // nothing here
}
diff --git a/src/test/resources/test-view.js b/src/test/resources/test-view.js
index 4af8dd2..6720ec9 100644
--- a/src/test/resources/test-view.js
+++ b/src/test/resources/test-view.js
@@ -13,6 +13,7 @@ export class TestView extends PolymerElement {
World
+
`;
}