-
Notifications
You must be signed in to change notification settings - Fork 297
Context
Zbigniew Ruchała edited this page May 13, 2014
·
1 revision
The context library is used to locate components,models and models' objects declaratively with @Context annotation.
public class MyLink extends AjaxLink<Void> {
@Context private MyPanel myPanel;
public MyLink(String pId) {
super(pId);
}
@Override
public void onClick(AjaxRequestTarget pTarget) {
myPanel.clickedLink(pTarget);
}
}
Access to the required components while initialization:
public class Section extends Panel {
@Context private SectionContainer container;
@Context private IModel<Data> data;
public Section(String pId, IModel<Data> pModel) {
super(pId, pModel);
}
@Override
protected void onInitialize() {
super.onInitialize();
ContextProvider.get().inject(this);
container.invokeSomeLogic();
if (data.getObject().isImportant()) {
...
}
}
}
The default: Traversal#UP only parent components are being visited. It stops if the required object is found.
Traversal#TOP_DOWN It starts from the page level then components and their children until the target is found. The component tree is visited only once, no matter how many objects are annotated with Context.
@Context(traversal=UP) private Div1 div1;
@Context(traversal=TOP_DOWN) private Set<Label> labels;
// set once, hold the reference to the resolved object
@Context private MyPanel panel;
// on demand, the Instance does not keep the reference to the object.
@Context private Instance<MyPanel> panel;
Instance#get() returns the target. This approach allows dealing with dynamic hierarchies. It doesn not keep the reference to the target.
@Context Component component;
@Context SomeInterface componentThatImplementsTheInterface;
@Context Set<Component> setOfComponents;
@Context List<Component> listOfComponents;
@Context Set<SomeInterface> setOfComponents;
@Context List<SomeInterface> setOfComponents;
@Context(qualifier="imp") Set<Label> impLabels;
@Context IModel<Data> model;
@Context Data data;
// all above can be combined with the `Instance`:
@Context Instance<Component> component;
@Context Instance<IModel<Data>> data;
@Context(qualifier="userData") Set<Component> components;
// the qualifier is appended to the component
add(new UserPanel("userPanel", model).add(new Qualifier("userData"));
public class WicketApplication extends WebApplication {
public void init() {
super.init();
getComponentInitializationListeners().add(new ContextProvider());
}
}
<dependency>
<groupId>org.wicketstuff</groupId>
<artifactId>wicketstuff-context</artifactId>
<version>6.0-SNAPSHOT</version>
</dependency>
licensed under Apache 2.0.
Zbigniew Ruchala zruchala at gmail (dot) com