Question

I'm pretty new to Wicket, so please excuse me if this is a simple task. I tried to look for examples but came up empty.

Here's the scenario. Lets say that I have a customer information panel with 25 data points (name, dob, address, phone, etc). I want to reuse this panel in many different pages on a site. Now lets say I have the following criteria on 3 different pages the panel appears on:

  • Page 1 - All fields are visible and editable
  • Page 2 - Date of Birth, Gender, Customer Name are visible but not editable, all other fields are visible and editable
  • Page 3 - Date of Birth and Gender are not visible, Address is visible and editable, all other fields are visible but not editable.

Is there a way to use the same panel (to reduce code duplication), but control its components based on the current page the panel is loaded into?

Thanks for any help!

Was it helpful?

Solution

I would suggest against changing the visibility and editability implicitly like this. Instead consider making it explicit based on constructor arguments for the component or additional methods on the custom component.

OTHER TIPS

Inside your panel you can call the getPage() method. Either you can check if the page is instance of a certain class our you can get the page class by calling getPageClass().

The visibility of your components inside the panel can be set by calling the setVisible() method or by overriding the isVisible() method. If components should be editable can be controlled by using the isEnabled() method (or overriding setEnabled()).

class YourPanel extends Panel {
    public YourPanel() {
        add(new TextField("name") {
                @Override
                boolean isVisible() {
                    return getPage().getPageClass().equals(Page2.class);
                }
        }
        TextField genderTextField = new TextField("gender");
        genderTextField.setVisible(!getPage().getPageClass().equals(Page3.class));
        add(genderTextField);
    }
}

As you can see, for many components and many cases to check the code will be quite complex. Maybe you can get cleaner code if you create custom panels, add the components according to their visibility and editability rules and control for the whole panels if they are visible and/or editable.

Per the other answer, you can call setVisible() and the like to customize the appearance.

I don't recommend you couple your panel to the displaying page, however; this will introduce circular dependency, and each time you want to reuse the panel on a new type of page, you'd have to add in another check for the new page. Instead, just make the panel configurable (best to do this at construction time by passing parameters such as boolean addressVisible).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top