001    package net.sf.tacos.components.tree;
002    
003    import java.io.Serializable;
004    import java.util.Collection;
005    import java.util.Iterator;
006    import java.util.Set;
007    import net.sf.tacos.model.IKeyProvider;
008    import net.sf.tacos.model.ITreeContentProvider;
009    
010    /**
011     * @author phraktle
012     */
013    public class TreeManager implements ITreeManager {
014        
015        private final Set state;
016        private final ITreeContentProvider contentProvider;
017        private final IKeyProvider keyProvider;
018        
019        /**
020         * Creates tree manager.
021         * @param state
022         * @param contentProvider
023         * @param keyProvider
024         */
025        public TreeManager(Set state, ITreeContentProvider contentProvider,
026                IKeyProvider keyProvider)
027        {
028            if (state == null || contentProvider == null || keyProvider == null) { throw new IllegalArgumentException(
029                    "Null parameter passed."); }
030            this.state = state;
031            this.contentProvider = contentProvider;
032            this.keyProvider = keyProvider;
033        }
034    
035        /**
036         * 
037         * {@inheritDoc}
038         */
039        public boolean isExpanded(Object element)
040        {
041            return isExpandedKey(keyProvider.getKey(element));
042        }
043    
044        /**
045         * 
046         * {@inheritDoc}
047         */
048        public boolean isExpandedKey(Serializable key)
049        {
050            return state.contains(key);
051        }
052    
053        /**
054         * 
055         * {@inheritDoc}
056         */
057        public void setExpanded(Object element, boolean open)
058        {
059            setExpandedKey(keyProvider.getKey(element), open);
060        }
061    
062        /**
063         * 
064         * {@inheritDoc}
065         */
066        public void setExpandedKey(Serializable key, boolean open)
067        {
068            if (open) {
069                state.add(key);
070            } else {
071                state.remove(key);
072            }
073        }
074    
075        /**
076         * 
077         * @return
078         */
079        public Set getState()
080        {
081            return state;
082        }
083    
084        /**
085         * 
086         * {@inheritDoc}
087         */
088        public void collapseAll()
089        {
090            state.clear();
091        }
092        
093        /**
094         * 
095         * {@inheritDoc}
096         */
097        public Object reveal(Object element)
098        {
099            Object parent = contentProvider.getParent(element);
100            if (parent != null) {
101                setExpanded(parent, true);
102                return reveal(parent);
103            } else return element;
104        }
105    
106        /**
107         * 
108         * {@inheritDoc}
109         */
110        public void expandAll()
111        {
112            expand(contentProvider.getElements());
113        }
114    
115        /**
116         * Expands all elements
117         * @param rootElements
118         */
119        private void expand(Collection rootElements)
120        {
121            for(Iterator i = rootElements.iterator(); i.hasNext();) {
122                Object element = i.next();
123                setExpanded(element, true);
124                if (contentProvider.hasChildren(element)) {
125                    expand(contentProvider.getChildren(element));
126                }
127            }
128        }
129    
130    }