wicketstuff/core — pull request #357 by ageery (2014-09-25 15:48:33+00:00)
Title: Models that depend on other models and behaviors for component visibility/enabling based on models

I've found that I often want to be able to transform a model of one type into a model of another type. For example, perhaps I have an IModel<String> but what I really want is a model of whether the object of the model is empty. To do this, I wrap a LDM around the IModel<String>.

The LoadableDetachableDependentModel class in this pull request is a simple base class for dealing with these situations. There are also a couple of concrete implementations of this class. For example, if I want a model of whether a field is not empty I could do:

IModel<...> model = ...
IModel notEmptyModel = new NotModel<>(new IsEmptyOrNullModel<>(model));

The other part of this pull request is related to the visibility (and "enablement") of components. The way this is currently done is one of:

1. call setVisible(...) on a component when it is created -- this won't change if the component is re-rendered
2. override onConfigure() for the component -- subclass the component
3. override onConfigure() in the container containing the component and specify setVisible(...) for the component there -- add an onConfigure() method to the container

This pull-request has a fourth way: add a behavior to a component that specifies its visibility (or "enablement").

The simplest example is the HideWhenEmptyOrNullBehavior:

new Label("label").add(HideWhenEmptyOrNullBehavior.get())

This will hide (setVisible(false)) when the model of the label is empty or null.

A more complicate example would be to be use VisibleModelBehavior:

IModel<...> model = ...
new TextField<>("field").add(VisibleModelBehavior(new NotModel<>(new IsEmptyOrNullModel<>(model))));

Change-log:

* Added a LDM class where the load()'ed value depends on another model.
* Added behaviors to hide/enable components based on a boolean model.
* Added a behavior to hide a component when the object of the component's
model is empty or null.
* Added a couple of dependent models that are useful for composing models
together.