David Sonnenfeld
David Sonnenfeld

Reputation: 698

Wicket - Reusable panels with java-subclasses

  1. I have a java class:
    public Task {

        private int id;
        private Company sender;
        private Company receiver;

        //Getter and Setter
        ...
    }

As you can see, I have 2 other custom classes in the task class. And a company has for example Adress and Directory (see screenshot below).

  1. Now I have a form page with 2 sections (sender & receiver) representing a company. I don't want to make 2 seperate markups and java code for those 2 sections. Any way to avoid this duplication?

enter image description here

Upvotes: 0

Views: 470

Answers (3)

Robert Niestroj
Robert Niestroj

Reputation: 16131

Make a panel for editing the Company class and put two instances of it with different Models to the TaskPanel

public class TaskPanel extends Panel{

  public TaskPanel(String id, IModel<Task> model){
    super(id, model);
    add(new CompanyPanel("senderCompanyPanel", new PropertyModel(model, "sender")));
    add(new CompanyPanel("receiverCompanyPanel", new PropertyModel(model, "receiver")));
    ...
  }

}

Upvotes: 1

Rob Audenaerde
Rob Audenaerde

Reputation: 20029

You could create a CompanyPanel which takes a IModel<Company>. You could use a PropertyModel on your task class to get one. PropertyModel sender = new PropertyModel(myTask, "sender"). The panel then can have two TextFields for which you can use a CompoundPropertyModel on the IModel that is passed.

Reuse this panel twice on your form.

On the CompanyPanel

public class CompanyPanel extends Panel
{
    public CompanyPanel(String id, IModel<Company> model)
    {
        super(id, new CompoundPropertyModel(model));
        add( new TextField("address"));
        add( new TextField("directory"));
    }
}

Lookup the CompoundPropertyModel in the docs. It is really useful.

Upvotes: 3

tetsuo
tetsuo

Reputation: 10896

Just create a Panel or Fragment, make its model a IModel<Company>, and add to your page two instances of it, one for sender and another for receiver.

Upvotes: 0

Related Questions