-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from vaadin-miki/11-grid-constructor
#11 done
- Loading branch information
Showing
9 changed files
with
120 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/org/vaadin/miki/supertemplate/GridBeanTypeConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<YourDataType>.", field.getName()); | ||
} catch (IllegalAccessException | NoSuchFieldException e) { | ||
LOGGER.warn("Could not access Grid.beanType and Grid.propertySet. Field {} will not work properly.", field.getName()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters