View Javadoc

1   package org.lsst.ccs.command;
2   
3   import org.lsst.ccs.utilities.beanutils.Optional;
4   import org.lsst.ccs.utilities.structs.TreeBranch;
5   
6   import javax.swing.tree.TreeNode;
7   import java.util.Iterator;
8   import java.util.LinkedHashMap;
9   
10  /**
11   * Client codes can receive a complete Tree of DictionaryContext from a Subsystem.
12   * It can then build this object to read informations about available dictionaries.
13   *
14   * @author bamade
15   * @ImplSpec : is the "top node" the node for the "main" Module or for the Subsystem itself?
16   */
17  // Date: 08/01/2014
18  
19  public class DictionariesTree implements Iterable<DictionaryContext> {
20      private final TreeBranch<DictionaryContext> topNode;
21      private LinkedHashMap<String, TreeBranch<DictionaryContext>> map;
22  
23      public DictionariesTree(TreeBranch<DictionaryContext> topNode) {
24          this.topNode = topNode;
25          Iterator<TreeBranch<DictionaryContext>> iterator = topNode.nodeIterator();
26          while (iterator.hasNext()) {
27              TreeBranch<DictionaryContext> node = iterator.next();
28              DictionaryContext dictionaryContext = node.getContent();
29              map.put(dictionaryContext.getName(), node);
30          }
31      }
32  
33      /**
34       * gets a <TT>Dictionary</TT> linked to a Component.
35       *
36       * @param componentName
37       * @return an <TT>Optional</TT> Dictionary (just in case the name is wrong!)
38       */
39      public Optional<Dictionary> getDictionaryFor(String componentName) {
40          TreeBranch<DictionaryContext> node = map.get(componentName);
41          if (node != null) {
42              return Optional.of(node.getContent().getDictionary());
43          }
44          return Optional.empty();
45      }
46  
47      /**
48       * Since the <TT>TreeBranch</TT> class is a <TT>TreeNode</TT>
49       * this method is useful for GUIs in need of a JTree.
50       * or for all methods that "walk" trees.
51       * <BR/>
52       * The tree has the same structure as the subsystem description file.
53       *
54       * @return The TreeBranch corresponding to the top of the tree
55       */
56      public TreeBranch<DictionaryContext> getTopNode() {
57          return topNode;
58      }
59  
60      public TreeNode getNodeFor(String componentName) {
61          return map.get(componentName);
62      }
63  
64      /**
65       * @return all the <TT>DictionaryContext</TT> in preOrder
66       * (that is with the structure defined in the subsystem description file)
67       */
68      @Override
69      public Iterator<DictionaryContext> iterator() {
70          return topNode.iterator();
71      }
72  
73      /**
74       * @return the names of the component of the subsystem in preOrder.
75       */
76      public Iterator<String> namesIterator() {
77          return new Iterator<String>() {
78              Iterator<DictionaryContext> ctxIterator = iterator();
79  
80              @Override
81              public boolean hasNext() {
82                  return ctxIterator.hasNext();
83              }
84  
85              @Override
86              public String next() {
87                  DictionaryContext dictCtx = ctxIterator.next();
88                  return dictCtx.getName();
89              }
90  
91              @Override
92              public void remove() {
93                  throw new UnsupportedOperationException("remove in nameIterator");
94              }
95          };
96      }
97  
98  
99  }