0 Replies Latest reply on Aug 28, 2013 10:14 PM by David Lee

    rich:tree selectionChangeListener and rich:treeNode toggleListener cannot work for same node class

    David Lee Newbie

      Hi When I tried to implement selection and toggle event,

      I found a issue --

      Due to two different subject (selection and toggle),

      they seems need different base node class.

       

      For TreeToggleEvent I'll need to use node implements org.richfaces.model.TreeNode.

      But for TreeSelectionChangeEvent I'll need to implements javax.swing.tree.TreeNode.

       

      And I try to implements both of them,

      But only TreeToggleEvent can work!

       

      I need some help since I'll need them both work in one tree, thanks a lot!

       

      following is my class:

       

      ------------------------------------------------------------

       

      import java.util.ArrayList;

      import java.util.Enumeration;

      import java.util.HashMap;

      import java.util.Iterator;

      import java.util.List;

      import java.util.Map;

       

      import javax.swing.tree.TreeNode;

       

      import com.google.common.collect.Iterators;

      import com.google.common.collect.Lists;

      import com.google.common.collect.Maps;

       

      public class OriComTreeNode implements TreeNode,org.richfaces.model.TreeNode

      {

          private List<TreeNode> childList = new ArrayList<TreeNode>();

          private Map<Object,TreeNode> childMap = new HashMap<Object,TreeNode>();

          private TreeNode parent;

         

          private String type;

          private String label;

          private boolean isSelected;

          private boolean isExpand;   

          private Object value;

         

          private List<Object> keysList = null;

          private Map<Object, org.richfaces.model.TreeNode> children = null;

          private boolean leaf = false;

       

          public OriComTreeNode()

          {

              super();

              keysList = Lists.newArrayList();

              children = Maps.newHashMap();

          }

         

          //***************

          @Override

          public void addChild(Object key, org.richfaces.model.TreeNode child) {

              ((OriComTreeNode)child).setParent(this);

              keysList.add(key);

              children.put(key, child);

              childList.add((TreeNode)child);

              childMap.put(key, (TreeNode)child);       

          }

       

          public void insertChild(int idx, Object key, org.richfaces.model.TreeNode child) {

              keysList.add(idx, key);

              children.put(key, child);

          }

       

          public org.richfaces.model.TreeNode getChild(Object key) {

              if (isLeaf()) {

                  return null;

              }

       

              return children.get(key);

          }

       

          public Iterator<Object> getChildrenKeysIterator() {

              if (isLeaf()) {

                  return Iterators.emptyIterator();

              }

              return Iterators.unmodifiableIterator(keysList.iterator());

          }

       

          public int indexOf(Object key) {

              return keysList.indexOf(key);

          }

          //***************

         

          public void removeChild(Object key) {

              int delKey = getIndex(childMap.get(key));

              childList.remove(delKey);

              childMap.remove(key);

              children.remove(key);

              keysList.remove(key);

          }

         

          @Override

          public String toString() {

              return getLabel();

          }

       

          @Override

          public TreeNode getChildAt(int childIndex) {

              return childList.get(childIndex);

          }

       

          @Override

          public int getChildCount() {

              return childList.size();

          }

       

          @Override

          public TreeNode getParent() {

              return parent;

          }

       

          @Override

          public int getIndex(TreeNode node) {

              return childList.indexOf(node);

          }

       

          @Override

          public boolean getAllowsChildren() {

              return true;

          }

       

          @Override

          public boolean isLeaf() {

              if(childList.isEmpty()){

                  return true;

              }

              return false;

          }

       

          @SuppressWarnings("rawtypes")

          @Override

          public Enumeration children() {

              return Iterators.asEnumeration(childList.iterator());

          }

       

          public void setParent(TreeNode parent) {

              this.parent = parent;

          }

       

          public Iterator<TreeNode> getChildrensIterator() {

              return childList.iterator();

          }

       

          public TreeNode getChildFormMap(Object entry) {

              return (TreeNode) childMap.get(entry);

          }

         

          public String getType()

          {

              return type;

          }

       

          public void setType(String type)

          {

              this.type = type;

          }

       

          public boolean getIsSelected() {

              return isSelected();

          }

       

          public void setIsSelected(boolean isSelected) {

              this.setSelected(isSelected);

          }

       

          public String getLabel() {

              return label;

          }

       

          public void setLabel(String label) {

              this.label = label;

          }

       

          public boolean isExpand() {

              return isExpand;

          }

       

          public void setExpand(boolean isExpand) {

              this.isExpand = isExpand;

          }

       

          public boolean isSelected() {

              return isSelected;

          }

       

          public void setSelected(boolean isSelected) {

              this.isSelected = isSelected;

          }

         

          public Object getValue() {

              return value;

          }

          public void setValue(Object value) {

              this.value = value;

          }

       

          public void setLeaf(boolean leaf) {

              this.leaf = leaf;

          }

      }