package org.wicketTutorial.checkboxtree;
import java.util.HashSet;
import java.util.Set;
import javax.swing.tree.DefaultMutableTreeNode;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.tree.NestedTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.theme.WindowsTheme;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.util.SetModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
public class AdvancedCheckedTreePage extends HomePage {
public AdvancedCheckedTreePage(PageParameters parameters) {
super(parameters);
NestedTree<DefaultMutableTreeNode> tree = getTree();
RepeatingView selectedNodes = new RepeatingView("nodes");
final WebMarkupContainer markupContainer = new WebMarkupContainer("checkedNodes");
final Set<DefaultMutableTreeNode> checkedNodes = new HashSet<DefaultMutableTreeNode>();
tree = new NestedTree<DefaultMutableTreeNode>("tree", tree.getProvider())
{
SetModel<DefaultMutableTreeNode> checkedNodesModel = new SetModel<DefaultMutableTreeNode>(
checkedNodes);
@Override
protected Component newContentComponent(String id, IModel<DefaultMutableTreeNode> model)
{
return new AutocheckedFolder<DefaultMutableTreeNode>(id, this, model, checkedNodesModel);
}
};
tree.add(new AjaxEventBehavior("click") {
@Override
protected void onEvent(AjaxRequestTarget target) {
RepeatingView selectedNodes = new RepeatingView("nodes");
for(DefaultMutableTreeNode node :checkedNodes){
selectedNodes.add(new Label(selectedNodes.newChildId(), node.toString()));
}
if(checkedNodes.isEmpty())
markupContainer.setVisible(false);
else
markupContainer.setVisible(true);
markupContainer.replace(selectedNodes);
target.add(markupContainer);
}
});
tree.add(new WindowsTheme());
markupContainer.add(selectedNodes);
markupContainer.setVisible(false);
add(markupContainer.setOutputMarkupPlaceholderTag(true));
addOrReplace(tree);
}
}