Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-838

Passing a non-existing tree node to DefaultTreeModel.getPath() causes infinite loop

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: 6.0.0, Freshly
    • Fix Version/s: 6.0.0
    • Component/s: Components
    • Labels:

      Description

      Problem Description

      I accidentally create a example to set tree Model's selected item to an non-existing tree node. It means this tree node cannot be found in tree's model.
      This situation make loop in getPath() method a infinite loop.

      Root Cause

      org.zkoss.zul.DefaultTreeModel.java
      	@Override
      	public int[] getPath(TreeNode<E> child) {
      		final TreeNode<E> root = getRoot();
      		List<Integer> p = new ArrayList<Integer>();
      		while (root != child) {
      			TreeNode<E> parent = child.getParent();
      			if (parent != null) {
      				for (int i = 0, j = parent.getChildCount(); i < j; i++) {
      					if (parent.getChildAt(i) == child) {
      						p.add(0, i);
      						break;
      					}
      				}
      				child = parent;
      			}
      		}
      		final Integer[] objs = p.toArray(new Integer[p.size()]);
      		final int[] path = new int[objs.length];
      		for (int i = 0; i < objs.length; i++)
      			path[i] = objs[i].intValue();
      		return path;
      	}
      
      

      When child is a tree node that is not a child node of the tree model. child.getParent() is null, and root!= child is always true thus the loop won't stop forever.

        Attachments

          Activity

            People

            Assignee:
            jumperchen jumperchen
            Reporter:
            hawk hawk
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: