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

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Normal Normal
    • 6.0.0
    • 6.0.0, Freshly
    • Components

      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.

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

              Created:
              Updated:
              Resolved: