View Javadoc

1   package net.sf.tacos.components.tree;
2   
3   import java.io.Serializable;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.Set;
7   import net.sf.tacos.model.IKeyProvider;
8   import net.sf.tacos.model.ITreeContentProvider;
9   import org.apache.hivemind.util.Defense;
10  
11  /**
12   * @author phraktle
13   */
14  public class TreeManager implements ITreeManager {
15      
16      private final Set state;
17      private final ITreeContentProvider contentProvider;
18      private final IKeyProvider keyProvider;
19      
20      /**
21       * Creates tree manager.
22       * @param state
23       * @param contentProvider
24       * @param keyProvider
25       */
26      public TreeManager(Set state, ITreeContentProvider contentProvider,
27              IKeyProvider keyProvider)
28      {
29          Defense.notNull(state, "tree state");
30          Defense.notNull(contentProvider, "tree contentProvider");
31          Defense.notNull(keyProvider, "tree keyProvider");
32          
33          this.state = state;
34          this.contentProvider = contentProvider;
35          this.keyProvider = keyProvider;
36      }
37  
38      /**
39       * 
40       * {@inheritDoc}
41       */
42      public boolean isExpanded(Object element)
43      {
44          return isExpandedKey(keyProvider.getKey(element));
45      }
46  
47      /**
48       * 
49       * {@inheritDoc}
50       */
51      public boolean isExpandedKey(Serializable key)
52      {
53          return state.contains(key);
54      }
55  
56      /**
57       * 
58       * {@inheritDoc}
59       */
60      public void setExpanded(Object element, boolean open)
61      {
62          setExpandedKey(keyProvider.getKey(element), open);
63      }
64  
65      /**
66       * 
67       * {@inheritDoc}
68       */
69      public void setExpandedKey(Serializable key, boolean open)
70      {
71          if (open) {
72              state.add(key);
73          } else {
74              state.remove(key);
75          }
76      }
77  
78      /**
79       * 
80       * @return
81       */
82      public Set getState()
83      {
84          return state;
85      }
86  
87      /**
88       * 
89       * {@inheritDoc}
90       */
91      public void collapseAll()
92      {
93          state.clear();
94      }
95      
96      /**
97       * 
98       * {@inheritDoc}
99       */
100     public Object reveal(Object element)
101     {
102         Object parent = contentProvider.getParent(element);
103         if (parent != null) {
104             setExpanded(parent, true);
105             return reveal(parent);
106         } else return element;
107     }
108 
109     /**
110      * 
111      * {@inheritDoc}
112      */
113     public void expandAll()
114     {
115         expand(contentProvider.getElements());
116     }
117 
118     /**
119      * Expands all elements
120      * @param rootElements
121      */
122     private void expand(Collection rootElements)
123     {
124         for(Iterator i = rootElements.iterator(); i.hasNext();) {
125             Object element = i.next();
126             setExpanded(element, true);
127             if (contentProvider.hasChildren(element)) {
128                 expand(contentProvider.getChildren(element));
129             }
130         }
131     }
132 
133 }