Skip to content

Context

Zbigniew Ruchała edited this page May 13, 2014 · 1 revision

Context Project

The context library is used to locate components,models and models' objects declaratively with @Context annotation.

Quick Overview

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()) {
         ...
        }
    }
}

Traversal strategies

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;

Instance

// 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.

More examples

@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;

Qualifier

@Context(qualifier="userData") Set<Component> components;

// the qualifier is appended to the component
add(new UserPanel("userPanel", model).add(new Qualifier("userData"));

How to start

public class WicketApplication extends WebApplication {

    public void init() {
        super.init();
        getComponentInitializationListeners().add(new ContextProvider());
    }

}

Maven Development

<dependency>
    <groupId>org.wicketstuff</groupId>
    <artifactId>wicketstuff-context</artifactId>
    <version>6.0-SNAPSHOT</version>
</dependency>

License

licensed under Apache 2.0.

Author

Zbigniew Ruchala zruchala at gmail (dot) com

Clone this wiki locally