Skip to content

Commit

Permalink
Merge pull request #14 from vaadin-miki/13-grid-columns
Browse files Browse the repository at this point in the history
#13 done
  • Loading branch information
vaadin-miki authored May 5, 2020
2 parents 863c36d + b1f7d93 commit d81249f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<BeanType>`, 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<BeanType>`, 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<MyBean>`

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<MyBean> 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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.vaadin.miki</groupId>
<artifactId>super-template</artifactId>
<version>0.2.0</version>
<version>0.2.1</version>

<name>Super Template</name>
<description>An extension of PolymerTemplate that reads properties from the template and makes them available on the server side. Requires Vaadin 14.X.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<YourDataType>.", field.getName());
Expand Down
26 changes: 24 additions & 2 deletions src/test/java/org/vaadin/miki/supertemplate/Dummy.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class Dummy {

private String contents;

private String description;

private boolean flagged;

public String getContents() {
return contents;
}
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void testSuperViewHasPropertiesFromTemplate() {
Grid<Dummy> 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
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/test-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export class TestView extends PolymerElement {
</style>
<vaadin-text-field id="a-text-field" placeholder="Hello" style="width: 100%;"></vaadin-text-field>
<span id="a-span">World</span>
<vaadin-grid id="a-grid"></vaadin-grid>
<vaadin-grid id="a-grid" data-columns="description">
<vaadin-grid-column data-column="flagged"></vaadin-grid-column>
</vaadin-grid>
`;
}

Expand Down

0 comments on commit d81249f

Please sign in to comment.