ajax richfaces tree problem
mglowacki Oct 9, 2009 5:18 AMI keep trying to use richfaces tree component with ajax toggle style. I want to build 3 level tree and I managed to, but with two problems.
1.When I expand nodes on lev1 and lev2 (so the leaves on 3rd lev are displayed) and then I collapse lev1 and then expand again - lev2 shows it is expanded, but no leaves on 3rd lev are displayed - I need to collapse&expand lev2 again.
2.When I click on 3rd lev leaf (NodeSelectedEvent is raised), collapse lev1, and then try to expand it again I got: java.lang.IllegalStateException: No tree element available or row key not set!
Please take a look at my code:
<h:form id="form1"> <rich:tree style="width:300px" changeExpandListener="#{categoryTree.processToggle}" nodeSelectListener="#{categoryTree.processSelection}" reRender="selectedNode" ajaxSubmitSelection="true" switchType="ajax" id="categoriesTree" value="#{categoryTree.treeNode}" var="item" stateVar="true"> <rich:treeNode> <h:outputText value="#{item}" /> </rich:treeNode> </rich:tree> <h:outputText escape="false" value="Selected Node: #{categoryTree.nodeTitle}" id="selectedNode" /> </h:form>
@Name("categoryTree") @Scope(ScopeType.PAGE) public class CategoryTree { @In(create=true) private Categories categories; private Log log = Logging.getLog(CategoryTree.class); private CategoryTreeNode rootNode = null; private String nodeTitle; private Integer selectedCategoryId; private CategoryTreeCache cache; public CategoryTree() { try { cache = CategoryTreeCache.getInstance(); } catch(Exception ex) { log.error("Can't obtain " + cache.getClass().getName() + " instance", ex); } } private void addNode(TreeNode node, Category category) { CategoryTreeNode nodeImpl = new CategoryTreeNode(); nodeImpl.setData(category); node.addChild(category.getCategoryId(), nodeImpl); } private void addNodes(TreeNode node, Integer parentCategory) { List<Category> categoryList = null; if(cache.isInCache(parentCategory.toString())) { categoryList = cache.getCategories(parentCategory); } else { categoryList = categories.getCategories(parentCategory); } cache.addCategories(parentCategory, categoryList); Iterator<Category> it = categoryList.iterator(); while(it.hasNext()) { addNode(node, it.next()); } } public void processSelection(NodeSelectedEvent event) { HtmlTree tree = (HtmlTree) event.getComponent(); nodeTitle = (String)tree.getRowData(); selectedCategoryId = ((CategoryTreeNode)tree.getModelTreeNode(tree.getRowKey())).getCategoryId(); } public void processToggle(NodeExpandedEvent event) { HtmlTree tree = (HtmlTree) event.getComponent(); CategoryTreeNode currentNode = (CategoryTreeNode)tree.getModelTreeNode(tree.getRowKey()); addNodes(currentNode, currentNode.getCategoryId()); } public CategoryTreeNode getTreeNode() { if (rootNode == null) { rootNode = new CategoryTreeNode(); rootNode.setData(new Category(0, "Root", -1, (short)0, false)); addNodes(rootNode, 0); } return rootNode; } public String getNodeTitle() { return nodeTitle; } public void setNodeTitle(String nodeTitle) { this.nodeTitle = nodeTitle; } }