public LinkPage()
{
// power to the annonymous classes!
// first create a simple value holder object
final Count count1 = new Count();
// add a link which, when clicked, increases our counter when a link is clicked, its onClick
// method is called
Link link1 = new Link("link1")
{
@Override
public void onClick()
{
count1.increment();
}
};
add(link1);
// add a counter label to the link so that we can display it in the body of the link
link1.add(new Label("label1", new Model<String>()
{
@Override
public String getObject()
{
return Integer.toString(count1.getCount());
}
}));
final Count count2 = new Count();
// Same idea as above, but now we record a state change. Note that the
// URL will change because of this, and pressing the back button and
// clicking the link again would revert to the older value.
// The same thing could have been achieved by using setModelObject,
// which implicitly registers a state change (of type
// ComponentModelChange).
Link linkWithStateChange = new Link("linkWithStateChange")
{
@Override
public void onClick()
{
final int count = count2.getCount();
count2.increment();
addStateChange(new Change()
{
@Override
public void undo()
{
// revert
count2.decrement();
}
});
}
};
add(linkWithStateChange);
linkWithStateChange.add(new Label("label", new PropertyModel<Integer>(count2, "count")));
// we can attach Link components to any HTML tag we want. If it is an anchor (<a href...),
// the url to this component is put in the href attribute. For other components, a onclick
// javascript event handler is created that triggers the round trip
// it is of course possible to - instead of the above approach - hide as
// much of the component as possible within a class.
class CustomLink extends Link
{
final Count count2;
/**
* Construct.
*
* @param id
*/
public CustomLink(String id)
{
super(id);
count2 = new Count();
add(new ClickCountLabel("label2", count2));
}
@Override
public void onClick()
{
count2.increment();
}
}
add(new CustomLink("link2"));
// and if we know we are going to attach it to a <input type="button> tag, we shouldn't use
// a label, but an AttributeModifier instead.
class ButtonLink extends Link
{
final Count count3;
/**
* Construct.
*
* @param id
*/
public ButtonLink(String id)
{
super(id);
count3 = new Count();
add(new AttributeModifier("value", new Model<String>()
{
@Override
public String getObject()
{