Skip to content

Instantly share code, notes, and snippets.

@wizche
Last active December 19, 2015 11:59
Show Gist options
  • Save wizche/5951514 to your computer and use it in GitHub Desktop.
Save wizche/5951514 to your computer and use it in GitHub Desktop.
Simple panel to insert link inside a label. Inject the link on a string with the following link placeholder ${link}
<wicket:panel>
<span wicket:id="first"></span>
<a wicket:id="link"><span wicket:id="linkLabel"></span></a>
<span wicket:id="second"></span>
</wicket:panel>
public class LinkLabel extends Panel {
private static final long serialVersionUID = 1L;
public LinkLabel(String id, String model, String linkName,
ILinkListener linkListener) {
super(id);
setRenderBodyOnly(true);
String[] split = model.split("\\$\\{link\\}");
if (split.length == 2) {
Label first = new Label("first", split[0]);
Label second = new Label("second", split[1]);
add(first);
add(second);
add(generateLink(linkListener, linkName));
} else if (split.length == 1) {
Label first = new Label("first", split[0]);
Label second = new Label("second");
second.setVisible(false);
add(first);
add(second);
add(generateLink(linkListener, linkName));
} else {
throw new UnsupportedOperationException(
"LinkLabel needs the ${link} placeholder!");
}
}
private Link<?> generateLink(final ILinkListener linkListener,
String linkName) {
Label linkLabel = new Label("linkLabel", linkName);
Link<String> link = new Link<String>("link") {
private static final long serialVersionUID = 1L;
@Override
public void onClick() {
linkListener.onLinkClicked();
}
};
link.add(linkLabel);
return link;
}
}
LinkLabel myLabel = new LinkLabel("description",
"First ${link} second", "my link", new ILinkListener() {
private static final long serialVersionUID = 1L;
@Override
public void onLinkClicked() {
// Your desired onClick action
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment