Using JavaScript with a Wicket LinkTree
I have to say that so far I really like Wicket, after years of developing J2EE Web application it’s a big relief to be able to develop Web application without having to maintain reams of URL or Action mappings in XML files.
Wicket offers a programming paradigm much closer to standard Java Swing instead which makes Web development much smoother.
There are a few pitfalls unfortunately as nothing is perfect but so far it’s been great.
One problem I’ve run into is when using Wickets LinkTree component (an Ajax based explorer like tree structure) is that it assumes that I always want to do an Ajax update from the server. But what if I want to run some JavaScript or simply redirect based on the clicked node.My solution is convoluted as I will admit that I am NOT a Wicket expert (yet) but to date I have not found another work around.
The gist of the solution is to override the newNodeComponent method which allows you to substitute your own code for the creation of tree nodes and I also overrode the onNodeClicked method as I wanted to expand and collapse tree nodes if the user clicked the nodes and not just the + and – icons.
Code:
-
-
…
-
_tree = new LinkTree("catalog", treeModel) {
-
-
@Override
-
-
// Our user object stored in each node
-
final CatalogItem item = (CatalogItem) ((DefaultMutableTreeNode) model.getObject()).getUserObject();
-
-
// Is this a leaf node?
-
// Your own condition may be different
-
if (item.getItem() != null) {
-
-
// Override standard node creation so we can setup our own component
-
// This kind of blows since if the standard LinkTree implementation changes we
-
// may be hooped, pooched, or even twaddled
-
final LinkIconPanel panel = new LinkIconPanel(id, model, this) {
-
private static final long serialVersionUID = 1L;
-
-
@Override
-
super.onNodeLinkClicked(node, tree, target);
-
onNodeLinkClicked(node, tree, target);
-
}
-
-
@Override
-
}
-
-
@Override
-
protected void addComponents(final IModelObject model, final BaseTree tree) {
-
final BaseTree.ILinkCallback callback = new BaseTree.ILinkCallback() {
-
private static final long serialVersionUID = 1L;
-
-
public void onClick(AjaxRequestTarget target) {
-
onNodeLinkClicked(model.getObject(), tree, target);
-
}
-
};
-
-
MarkupContainer link = tree.newLink("iconLink", callback);
-
add(link);
-
link.add(newImageComponent("icon", tree, model));
-
-
// link = tree.newLink("contentLink", callback);
-
// Which return an AjaxLink or we can setup our own component instead…
-
link = new WebMarkupContainer("contentLink");
-
-
// Add our own JavaScript
-
link.add(new SimpleAttributeModifier("onclick",
-
"ezprint.setSku(‘" + item.getItem().getPartnerCode() + "’);return false;"));
-
-
// Standard node creation resumes
-
link.add(newContentComponent("content", tree, model));
-
add(link);
-
}
-
};
-
-
return panel;
-
-
} else {
-
// Standard node creation
-
return super.newNodeComponent(id, model);
-
}
-
-
}
-
-
-
@Override
-
// Expand or collapse a node if the user clicks on a node (in addition to the +/- signs)
-
-
if (!node.isLeaf()) {
-
if (tree.getTreeState().isNodeExpanded(node)) {
-
_tree.getTreeState().collapseNode(node);
-
} else {
-
_tree.getTreeState().expandNode(node);
-
}
-
// and else here would handle your leaf node
-
tree.updateTree(target);
-
}
-
}
-
};
-
-