1 Reply Latest reply on May 13, 2009 9:46 AM by Lionel Meroni

    DnD fail when using TreeMap instead of LinkedHashMap with Tr

    Lionel Meroni Newbie

      Hi,

      I have an implimentation of TreeNode wich work fine with a java.util.LinkedHashMap to store the children.

      I want to sort the nodes so I change this by a java.util.TreeMap to have the node sorted.

      It's Ok, the nodes are sorted, but it don't work completely with TreeMap.... :(
      In fact, the problem is with the drag'n'drop. When I use TreeMap drag from the tree don't work...
      Nothing happen, no error, the dragListerner and dropListener aren't triggered when I drag from the tree.
      If i drag something from outside the tree to the tree, the dropListener is triggered.
      If i drag something from into the tree to the tree or outside, the dropListener is not triggered.

      Here the code of my TreeNode implementation :

      package fr.sncm.gsmsecu.ui.tree;
      
      import java.io.Serializable;
      import java.util.Iterator;
      import java.util.Map;
      import java.util.TreeMap;
      import java.util.Map.Entry;
      
      import org.richfaces.model.TreeNode;
      
      public abstract class NodeGeneric implements IdentifierTreeNode, TreeNode<NodeGeneric>, Serializable {
      
       private static final long serialVersionUID = 6665422957818929545L;
      
       private NodeKey nodeKey;
      
       private String code;
      
       private String libelle;
      
       private boolean valid;
      
       // private Map<Object, TreeNode<NodeGeneric>> enfants = new LinkedHashMap<Object, TreeNode<NodeGeneric>>();
       private Map<Object, TreeNode<NodeGeneric>> enfants = new TreeMap<Object, TreeNode<NodeGeneric>>();
      
       private NodeGeneric parent;
      
       /**
       * @param id
       */
       public NodeGeneric(long id) {
       super();
       this.nodeKey = new NodeKey(id);
       this.valid = true;
       }
      
       /**
       * @param id
       * @param code
       * @param libelle
       * @param order
       */
       public NodeGeneric(long id, String code, String libelle, Integer order) {
       super();
       this.nodeKey = new NodeKey(id, order);
       this.code = code;
       this.libelle = libelle;
       this.valid = true;
       }
      
       public abstract String getIcon();
      
       public abstract String getLeafIcon();
      
       public abstract String getType();
      
       /**
       * @return the valid
       */
       public boolean isValid() {
       return valid;
       }
      
       /**
       * @param valid
       * the valid to set
       */
       public void setValid(boolean valid) {
       this.valid = valid;
       }
      
       /**
       * @return the id
       */
       public long getId() {
       return this.nodeKey.getKey();
       }
      
       /**
       * @param id
       * the id to set
       */
       public void setId(long id) {
       this.nodeKey.setKey(id);
       }
      
       /**
       * @return the code
       */
       public String getCode() {
       return code;
       }
      
       /**
       * @return the libelle
       */
       public String getLibelle() {
       return libelle;
       }
      
       /**
       * @param code
       * the code to set
       */
       public void setCode(String code) {
       this.code = code;
       }
      
       /**
       * @param libelle
       * the libelle to set
       */
       public void setLibelle(String libelle) {
       this.libelle = libelle;
       }
      
       public void addChild(Object identifier, TreeNode<NodeGeneric> child) {
       child.setParent(this);
       enfants.put(identifier, child);
       }
      
       public TreeNode<NodeGeneric> getChild(Object identifier) {
       return (TreeNode<NodeGeneric>) enfants.get(identifier);
       }
      
       public void setParent(TreeNode<NodeGeneric> parent) {
       this.parent = (NodeGeneric) parent;
       }
      
       public TreeNode<NodeGeneric> getParent() {
       return parent;
       }
      
       public boolean isLeaf() {
       return enfants.isEmpty();
       }
      
       public void removeChild(Object identifier) {
       enfants.remove(identifier);
       }
      
       public Iterator<Entry<Object, TreeNode<NodeGeneric>>> getChildren() {
       return enfants.entrySet().iterator();
       }
      
       public NodeGeneric getData() {
       return this;
       }
      
       public void setData(NodeGeneric data) {
       }
      
       /*
       * (non-Javadoc)
       * @see java.lang.Object#hashCode()
       */
       @Override
       public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((nodeKey == null) ? 0 : nodeKey.hashCode());
       return result;
       }
      
       /*
       * (non-Javadoc)
       * @see java.lang.Object#equals(java.lang.Object)
       */
       @Override
       public boolean equals(Object obj) {
       if (this == obj)
       return true;
       if (obj == null)
       return false;
       if (!(obj instanceof NodeGeneric))
       return false;
       NodeGeneric other = (NodeGeneric) obj;
       if (nodeKey == null) {
       if (other.nodeKey != null)
       return false;
       } else if (!nodeKey.equals(other.nodeKey))
       return false;
       return true;
       }
      
       // ++ LMI 140409
       /**
       * @see java.lang.Object#toString()
       */
       @Override
       public String toString() {
       if (this.code == null) {
       return super.toString();
       }
      
       StringBuffer buffer = new StringBuffer("[").append(this.code).append("]");
       if (this.libelle != null) {
       buffer.append(" ").append(this.libelle);
       }
      
       if (this.nodeKey != null && this.nodeKey.getOrder() != null) {
       buffer.append(" (").append(this.nodeKey.getOrder()).append(")");
       }
      
       return buffer.toString();
       }
      
       /**
       * méthodes qui ne sont pas implémentées par TreeNode
       *
       * @param children
       * @return void
       */
       public void addChildrenToMe(NodeGeneric children) {
       addChild(children.getNodeKey(), children);
       children.setParent(this);
       }
      
       public Object getMyParent() {
       return parent;
       }
      
       @SuppressWarnings("unchecked")
       public void setMyParent(Object parent) {
       this.setParent((TreeNode<NodeGeneric>) parent);
       }
      
       /**
       * @return the nodeKey
       */
       public NodeKey getNodeKey() {
       return nodeKey;
       }
      
       /**
       * @param nodeKey
       * the nodeKey to set
       */
       public void setNodeKey(NodeKey nodeKey) {
       this.nodeKey = nodeKey;
       }
      
      
      }
      


        • 1. Re: DnD fail when using TreeMap instead of LinkedHashMap wit
          Lionel Meroni Newbie

          I have made a temporary solution.

          Since TreeMap doesn't completly work, I keep the LinkedHashMap in the TreeNode implementation. and I put this in the getChildren method :

          public Iterator<Entry<Object, TreeNode<NodeGeneric>>> getChildren() {
          
           Map<Object, TreeNode<NodeGeneric>> sortedMap = new TreeMap<Object, TreeNode<NodeGeneric>>();
           sortedMap.putAll(enfants);
          
           return sortedMap.entrySet().iterator();
           }


          This is not a groovy and powerful solution but it's work with DnD and nodes are sorted ! :)