7
  1. I have following java classes:

    enter image description here

  2. I have a two form components which are mapped to Type1Task and Type2Task with a CompoundPropertyModel:

enter image description here

Problem: Duplicate Code. I want the D and E Fields to map to a task, not the task types. Is there any way to make this reusable when I hava java inheritance?

If yes, can u provide me with tutorials or references?

Additional info: The task can contain a timer(thread). If you click on submit button the timer will be started.

Thank you!

Community
  • 1
  • 1
David Sonnenfeld
  • 688
  • 5
  • 17
  • 31
  • I'm not quite sure I understand the problem. But as an aside, if you're going to put your task objects in the model, they can't reference threads as model objects must be serializable. – biziclop Jan 24 '13 at 21:26

2 Answers2

6

You can duplicate 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, IModel<Task1> model)
     super(id, model);
     add(new TextField("x"));
     add(new TextField("y));
     add(new TextField("z"));
}

}

And the 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: make sure you use some loadabledetachablemodel to wrap the task objects in if they contain a reference to a thread otherwise you will run into serialization issues. A singleton registry that stores your tasks and can return them by some random key is enough.

Betlista
  • 10,327
  • 13
  • 69
  • 110
cserepj
  • 926
  • 6
  • 9
  • hmmm but there is a little problem... I have my form inside a Page, and there is already a . So I would have a inside another ... – David Sonnenfeld Jan 26 '13 at 00:18
  • These are panels, not pages. You can add the appropriate panel inside the form. Panels have their own markup with their own markup inheritence hierarchy. – cserepj Jan 26 '13 at 01:50
  • Thanks. I will try it out. Once I works I will mark as answered. – David Sonnenfeld Jan 26 '13 at 05:57
  • Also read very important [note](https://cwiki.apache.org/confluence/display/WICKET/Markup+inheritance#Markupinheritance-UsingMarkupInheritancewithPanels) about inheritance in wicket panels (look for isTransparentResolver) – Johnny Apr 07 '17 at 08:35
0
public class TaskPanel extends FormComponentPanel{

      private IModel<Task> taskModel;

      public TaskPanel(String id, IModel<Task> model){
      taskModel = model;
      add(new TextField("DTextField", new PropertyModel(taskModel, "D")));
      add(new TextField("ETextField", new PropertyModel(taskModel, "E")));
      }
}

Is this what you wanted, or didn't I understand correctly?

Betlista
  • 10,327
  • 13
  • 69
  • 110
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119