1 Reply Latest reply on Jan 28, 2009 5:04 PM by ZJ DA

    Tree Node ClassCastException

    ZJ DA Newbie

      Hi, I used rich:recursiveTreeNodesAdaptor for my tree. However, I could not figure how to refresh the affected nodes since addComponentToAjaxRender method caused exceptions due to no row key.

      Therefore, I create my class ObjectBeanNode:

       public class ObjectBeanNode implements TreeNode<ObjectBean> {
       private static final long serialVersionUID = -5498990493803705085L;
       private ObjectBean data = null;
       private TreeNode<ObjectBean> parent = null;
       private Map<Object, TreeNode<ObjectBean>> childrenMap = null;
      
       public ObjectBeanNode(ObjectBean data) {
       this.data = data;
       }
      
       public ObjectBeanNode(ObjectBean data, TreeNode<ObjectBean> parent) {
       this.data = data;
       this.parent = parent;
       }
      
       public ObjectBean getData() {
       return data;
       }
      
       public TreeNode<ObjectBean> getChild(Object identifier) {
       if (this.childrenMap == null) {
       this.loadChildren();
       }
       return childrenMap.get(identifier);
       }
      
       public void addChild(Object identifier, TreeNode<ObjectBean> child) {
       if (this.childrenMap == null) {
       this.loadChildren();
       }
       child.setParent(this);
       childrenMap.put(identifier, child);
       }
      
       public void removeChild(Object identifier) {
       if (this.childrenMap == null) {
       this.loadChildren();
       }
       TreeNode<ObjectBean> treeNode = (TreeNode<ObjectBean>) childrenMap.remove(identifier);
       if (treeNode != null) {
       treeNode.setParent(null);
       }
       }
      
       public void setData(ObjectBean data) {
       this.data = data;
       }
      
       public TreeNode<ObjectBean> getParent() {
       return parent;
       }
      
       public void setParent(TreeNode<ObjectBean> parent) {
       this.parent = parent;
       }
      
       public boolean isLeaf() {
       return !this.data.isCollectable();
       }
      
       @Override
       public Iterator<Entry<Object, TreeNode<ObjectBean>>> getChildren() {
       if (this.childrenMap == null) {
       this.loadChildren();
       }
       return this.childrenMap.entrySet().iterator();
       }
      
       public String getType() {
       return this.data.isCollectable() ? "node" : "item";
       }
      
       private void loadChildren() {
       try {
       this.childrenMap = new LinkedHashMap<Object, TreeNode<ObjectBean>>();
       ...
       } catch (ApiException e) {
       e.printStackTrace();
       }
      
       }
      
      


      and use rich:tree as the following:

       <rich:tree id="navtree" value="#{navTree.root}" treeNodeVar="treeNode"
       var="item" switchType="ajax" reRender="mainFrame" ajaxSubmitSelection="true"
       nodeFace="#{treeNode.type}" dropListener="#{navTree.dropListener}">
       <rich:treeNode type="node" acceptedTypes="mitarbeiter" dropValue="#{treeNode}">
       <h:outputText value="#{item.name}" />
       </rich:treeNode>
       <rich:treeNode type="item" dragType="mitarbeiter" dragValue="#{treeNode}">
       <rich:dndParam name="label" type="drag">#{item}</rich:dndParam>
       <h:outputText value="#{item.name}" style="color:blue"/>
       </rich:treeNode>
       </rich:tree>
      


      But, I get the exception:

      java.lang.ClassCastException: ObjectBeanNode cannot be cast to javax.swing.tree.TreeNode org.richfaces.model.SwingTreeDataModel.setWrappedData(SwingTreeDataModel.java:66)
      org.richfaces.component.UITree.createDataModel(UITree.java:692) org.ajax4jsf.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:621)
      ...

      If I use the class in the Richface demo, the tree works fine. Could anyone tell me what causes the problem.

      Thanks,
      -ZJ