You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/10/27 16:49:37 UTC

svn commit: r830231 - in /incubator/pivot/trunk: core/src/org/apache/pivot/collections/Sequence.java wtk/src/org/apache/pivot/wtk/TreeView.java

Author: tvolkert
Date: Tue Oct 27 15:49:36 2009
New Revision: 830231

URL: http://svn.apache.org/viewvc?rev=830231&view=rev
Log:
Added Sequence.Tree.traverseDepthFirst(), TreeView.expandAll(), TreeView.collapseAll()

Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TreeView.java

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java?rev=830231&r1=830230&r2=830231&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java Tue Oct 27 15:49:36 2009
@@ -17,6 +17,7 @@
 package org.apache.pivot.collections;
 
 import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 import org.apache.pivot.util.ImmutableIterator;
 
@@ -166,6 +167,119 @@
         }
 
         /**
+         * Nested sequence item iterator iterface.
+         */
+        public interface ItemIterator<T> extends Iterator<T> {
+            /**
+             * Gets the path within the nested sequence to the item nost
+             * recently returned by a call to <tt>next()</tt>.
+             *
+             * @return
+             * The path (from the root sequence) to the current item.
+             *
+             * @throws IllegalStateException
+             * If <tt>next()</tt> has not yet been called on this iterator.
+             */
+            public Path getPath();
+        }
+
+        private static class DepthFirstItemIterator<T> implements ItemIterator<T> {
+            private ArrayStack<Sequence<T>> stack = new ArrayStack<Sequence<T>>();
+            private Path previousPath = null;
+            private Path nextPath = new Path();
+
+            public DepthFirstItemIterator(Sequence<T> sequence) {
+                stack.push(sequence);
+                nextPath.add(0);
+                normalize();
+            }
+
+            /**
+             * {@inheritDoc}
+             */
+            @Override
+            public boolean hasNext() {
+                return (stack.peek() != null);
+            }
+
+            /**
+             * {@inheritDoc}
+             */
+            @Override
+            @SuppressWarnings("unchecked")
+            public T next() {
+                Sequence<T> sequence = stack.peek();
+
+                if (sequence == null) {
+                    throw new NoSuchElementException();
+                }
+
+                previousPath = new Path(nextPath);
+
+                int n = nextPath.getLength();
+                int index = nextPath.get(n - 1);
+
+                T item = sequence.get(index);
+
+                if (item instanceof Sequence<?>) {
+                    stack.push((Sequence<T>)item);
+                    nextPath.add(0);
+                } else {
+                    nextPath.update(n - 1, index + 1);
+                }
+
+                normalize();
+
+                return item;
+            }
+
+            /**
+             * Normalizes <tt>stack</tt> and <tt>nextPath</tt> such that the
+             * iterator is pointing to a valid item or the end of the nested
+             * sequence.
+             */
+            private void normalize() {
+                Sequence<T> sequence = stack.peek();
+
+                int n = nextPath.getLength();
+                int index = nextPath.get(n - 1);
+
+                while (sequence != null
+                    && index >= sequence.getLength()) {
+                    stack.pop();
+                    sequence = stack.peek();
+
+                    nextPath.remove(--n, 1);
+
+                    if (n > 0) {
+                        index = nextPath.get(n - 1);
+                        nextPath.update(n - 1, ++index);
+                    }
+                }
+            }
+
+            /**
+             * {@inheritDoc}
+             */
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+
+            /**
+             * {@inheritDoc}
+             */
+            @Override
+            public Path getPath() {
+                if (previousPath == null) {
+                    throw new IllegalStateException();
+                }
+
+                return previousPath;
+            }
+        }
+
+        /**
          * Adds an item to a nested sequence.
          *
          * @param sequence
@@ -367,6 +481,14 @@
         }
 
         /**
+         * Returns an iterator that will perform a depth-first traversal of the
+         * nested sequence.
+         */
+        public static <T> ItemIterator<T> taverseDepthFirst(Sequence<T> sequence) {
+            return new DepthFirstItemIterator<T>(sequence);
+        }
+
+        /**
          * Determines whether the path represented by the second argument is
          * a descendant of the path represented by the first argument.
          *

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TreeView.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TreeView.java?rev=830231&r1=830230&r2=830231&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TreeView.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TreeView.java Tue Oct 27 15:49:36 2009
@@ -1917,6 +1917,27 @@
     }
 
     /**
+     * Expands all branches in the tree view.
+     */
+    @SuppressWarnings("unchecked")
+    public final void expandAll() {
+        Sequence.Tree.ItemIterator<Object> itemIterator =
+            Sequence.Tree.taverseDepthFirst((List<Object>)treeData);
+
+        while (itemIterator.hasNext()) {
+            Object node = itemIterator.next();
+
+            if (node instanceof List<?>) {
+                Path path = itemIterator.getPath();
+
+                if (path.getLength() > 0) {
+                    expandBranch(path);
+                }
+            }
+        }
+    }
+
+    /**
      * Collapses the branch at the specified path. If the branch is already
      * collapsed, nothing happens.
      *
@@ -1928,6 +1949,27 @@
     }
 
     /**
+     * Collapses all branches in the tree view.
+     */
+    @SuppressWarnings("unchecked")
+    public final void collapseAll() {
+        Sequence.Tree.ItemIterator<Object> itemIterator =
+            Sequence.Tree.taverseDepthFirst((List<Object>)treeData);
+
+        while (itemIterator.hasNext()) {
+            Object node = itemIterator.next();
+
+            if (node instanceof List<?>) {
+                Path path = itemIterator.getPath();
+
+                if (path.getLength() > 0) {
+                    collapseBranch(path);
+                }
+            }
+        }
+    }
+
+    /**
      * Ensures that this tree view is listening for list events on every branch
      * node along the specified path.
      *



Re: svn commit: r830231 - in /incubator/pivot/trunk: core/src/org/apache/pivot/collections/Sequence.java wtk/src/org/apache/pivot/wtk/TreeView.java

Posted by Todd Volkert <tv...@gmail.com>.
Glad you like it :)

I actually just renamed it to depthFirstIterator() to be more consistent
with our naming.

Cheers,
-T

On Tue, Oct 27, 2009 at 1:21 PM, Sandro Martini <sa...@gmail.com>wrote:

> Hi Todd,
> > Added Sequence.Tree.traverseDepthFirst(), TreeView.expandAll(),
> TreeView.collapseAll()
> thanks, I'll use them !!
>
> Bye
>

Re: svn commit: r830231 - in /incubator/pivot/trunk: core/src/org/apache/pivot/collections/Sequence.java wtk/src/org/apache/pivot/wtk/TreeView.java

Posted by Sandro Martini <sa...@gmail.com>.
Hi Todd,
> Added Sequence.Tree.traverseDepthFirst(), TreeView.expandAll(), TreeView.collapseAll()
thanks, I'll use them !!

Bye