To reduce the workload of users, TWaver has predefined Renderer and Editor as default functions.
Users can use them directly, for example:
twaver.table.renderer.StringRendererTWaver's Table and Sheet has offered flexible deployment, enabling users to use customized renderer and editor, such as ValidateValueDemo.
twaver.table.renderer.StrokeRenderer
twaver.table.renderer.FontRenderer
twaver.table.renderer.EnumTypeRenderer
Now taking ValidateValueDemo as an example, we will introduce the use of Renderer and Editor as follows: (Notice that in this example there are a few tables and renderers applying xml to deploy. The effects are the same if using API.)
- Let's first see how to set renderer and editor. They are mainly in correspondence with the two ways setRendererClass and setEditorClass of ElementAttribute, such as the following XML extract:
- How to pass by parameters to self-defined renderer and editor? Sometimes we need to pass by some parameters to renderer, such as DateRenderer. Maybe we need to customize the display format. When a Renderer object is created in TWaver, strings in Renderer will be analyzed.
<attribute clientPropertyKey="MyProp3"Of course, users can also define a render, such as:
displayName="1980-03-06~2080-04-23"
icon="/demo/sheet/validate/markIcon.png"
renderer="twaver.table.renderer.DateRenderer@yyyy-MM-dd"
editor="twaver.table.editor.SpinnerDateEditor@1980-03-06|2080-04-23|yyyy-MM-dd">
<param key="nullable" value="false"/>
<param key="message" value="Date field can not be null."/>
</attribute>
See demo.table.legend.RowIDRenderer for the code of RowIDRenderer<attribute
clientPropertyKey="*"
displayName="ID"
renderer="demo.table.legend.RowIDRenderer"/>
Here is the extract:
String[] parameterVlaues = value.split("\\|");
Class[] parameterTypes = new Class[parameterVlaues.length];
for(int i=0; i<parameterVlaues.length; i++){
parameterTypes[i] = String.class;
}
Class clazz = ReflectUtils.forName(className);return clazz.getConstructor(parameterTypes).newInstance(parameterVlaues);
In this way we can pass by parameters to self-defined renderer and editor through For example,After parameters are passed by to renderer="twaver.table.renderer.DateRenderer@yyyy-MM-dd of DataRender," the format used in DateRenderer is "yyyy-MM-dd."
Another function of renderer and editor used more frequently is twaver.table.renderer.EnumTypeRenderer and twaver.table.editor.EnumTypeEditor.
The Constructor of class EnumTypeEditor is given as follows:
public EnumTypeEditor(String enumTypeName, String nullable, String alignment) {
If the parameter required to be passed by is the registered enumTypeName of EnumTypeManager, for example:
EnumTypeManager.getInstance().registerEnumTypes("City", new EnumType[] {
new EnumType("N", "New York"),
new EnumType("T", "Tokyo"),
new EnumType("L", "London")});
Then values can be passed by through the following way:
renderer="twaver.table.renderer.EnumTypeRenderer@City"
editor="twaver.table.editor.EnumTypeEditor@City">
"nullable" means that whether null is allowed to show in the drop-down list. If you would not like null to be appeared in the drop-down list, you could set the value as false:
renderer=”twaver.table.renderer.EnumTypeRenderer@City|false”If your project has applied Renderer and Editor, we recommend you to read ValidateValueDemo attentively.