You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by javanrell <ja...@gmail.com> on 2012/03/06 20:50:11 UTC
Re: Checkbox tree component
Hi all,
I made some changes based on the CheckBoxIconPanel.java posted by Doug,
basically I added behavior to update all parents or childrens when we check
or uncheck a node:
* When we check a node, we verify all its brothers, if they are all checked
then we check its parent and recursively.
* When we check a non leaf node we check all its children recursively.
* When we uncheck a node we uncheck all its parents until the root.
I hope it will useful for someone.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import javax.swing.tree.TreeNode;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LabelIconPanel;
import org.apache.wicket.model.IModel;
/**
* Simple panel that contains a link with icon and a link with a label.
*
* @author Matej Knopp
*/
public class CheckBoxIconPanel extends LabelIconPanel {
private static final long serialVersionUID = 1L;
/**
* Constructs the panel.
*
* @param id
* component id
* @param model
* model that is used to access the TreeNode
* @param tree
*/
public CheckBoxIconPanel(String id, IModel model, CheckBoxTree tree) {
super(id, model, tree);
}
/**
* @see
org.apache.wicket.markup.html.tree.LabelIconPanel#addComponents(org.apache.wicket.model.IModel,
* org.apache.wicket.markup.html.tree.BaseTree)
*/
protected void addComponents(final IModel model, final BaseTree tree) {
final CheckBoxTree cbTree = (CheckBoxTree)tree;
CheckBoxTree.ICheckCallback callback = new CheckBoxTree.ICheckCallback() {
private static final long serialVersionUID = 1L;
public void onUpdate(AjaxRequestTarget target) {
onNodeCheckUpdated((TreeNode)model.getObject(), cbTree, target);
}
};
add(newImageComponent("icon", tree, model));
IModel dataModel = cbTree.newCheckBoxModel((TreeNode)model.getObject());
MarkupContainer cb = null;
add( cb = cbTree.newCheckBox("checkbox", dataModel, callback) );
add(newContentComponent("content", tree, model));
}
/**
* Handler invoked when the checkbox is clicked. By default makes the node
selected
*
* @param node
* @param tree
* @param target
*/
protected void onNodeCheckUpdated(TreeNode node, CheckBoxTree tree,
AjaxRequestTarget target) {
if (tree.getTreeState().isNodeSelected(node)) {
deselectAllParent(node, tree);
selectAllChildren(node, tree, false);
} else {
selectAllChildren(node, tree, true);
updateSelectionIfLastSelected(node, tree);
}
tree.updateTree(target);
}
/**
* When a node of leaf is selected we search for all its brothers, if all
are selected too we select the parent and recursively until the root.
*
* @param node
* @param tree
* @param target
*/
protected void updateSelectionIfLastSelected(TreeNode node, CheckBoxTree
tree) {
TreeNode parent = node.getParent();
while(parent != null) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
if(!tree.getTreeState().isNodeSelected(parent.getChildAt(i))) {
return;
}
}
tree.getTreeState().selectNode(parent, true);
parent = parent.getParent();
}
}
/**
* When a node / leaf is deselected all its parent are deselected too.
*
* @param node
* @param tree
* @param target
*/
protected void deselectAllParent(TreeNode node, CheckBoxTree tree) {
TreeNode parent = node.getParent();
while(parent != null) {
tree.getTreeState().selectNode(parent, false);
parent = parent.getParent();
}
}
/**
* When a node is selected all its children are selected too
*
* @param node
* @param tree
* @param target
*/
protected void selectAllChildren(TreeNode node, CheckBoxTree tree, boolean
check) {
if (node.isLeaf()) {
tree.getTreeState().selectNode(node, check);
} else {
tree.getTreeState().selectNode(node, check);
int childCount = node.getChildCount();
for (int i = 0; i < childCount; i++) {
selectAllChildren(node.getChildAt(i), tree, check);
}
}
}
}
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Checkbox-tree-component-tp1892569p4451127.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org