Forms – wicket – reusable panel with Java inheritance
>I have the following Java classes:
>I have two form components that map to type1task and type2task using compoundpropertymodel:
Problem: duplicate code I want the D and e fields to map to the task, not the task type When I have Java inheritance, is there any way to make this reusable?
If so, can you provide me with tutorials or references?
Additional information: tasks can contain timers (threads) If you click the submit button, the timer will start
thank you!
Solution
You can copy the same class hierarchy in the UI
public class TaskFormPanel<T extends Task> extends Panel { public TaskFormPanel(String id,IModel<T> model) super(id,new CompoundPropertyModel(model)); add(new TextField("d")); add(new TextField("e)); add(new Button("submit) { (...) } } } public class Task1FormPanel extends TaskFormPanel<Task1> { public TaskFormPanel(String id,IModel<Task1> model) super(id,model); add(new TextField("a")); add(new TextField("b)); add(new TextField("c")); } } public class Task2FormPanel extends TaskFormPanel<Task2> { public TaskFormPanel(String id,model); add(new TextField("x")); add(new TextField("y)); add(new TextField("z")); } }
And HTML files:
TaskFormPanel:
<wicket:panel> <wicket:child/> <label>d</label> <input wicket:id="d"> <label>e</label> <input wicket:id="e"> <input type="submit" wicket:id="submit"/> </wicket:panel>
Task1Panel. html:
<wicket:extend> <label>a</label> <input wicket:id="a"> <label>b</label> <input wicket:id="b"> <label>c</label> <input wicket:id="c"> </wicket:extend>
Task2Panel. html:
<wicket:extend> <label>x</label> <input wicket:id="x"> <label>y</label> <input wicket:id="y"> <label>z</label> <input wicket:id="z"> </wicket:extend>
Note: if the task object contains a reference to a thread, make sure to wrap the task object with some loadabledetachablemodels, otherwise you will encounter serialization problems It is sufficient to store tasks and return their singleton registry through some random keys