View Javadoc

1   package de.matthias_burbach.deputy.swing;
2   
3   import java.util.Enumeration;
4   import java.util.Iterator;
5   
6   import javax.swing.tree.DefaultTreeModel;
7   
8   import de.matthias_burbach.deputy.core.project.Project;
9   import de.matthias_burbach.deputy.core.project.ProjectChangeListener;
10  import de.matthias_burbach.deputy.core.project.ProjectQualifier;
11  
12  /***
13   * Is a dependencies node in the project tree.
14   * <pre>
15   *     myartifactid-myversion
16   *      |
17   *      + used by
18   * ***  + dependencies ***
19   * </pre>
20   * @author Matthias Burbach
21   */
22  public class DependenciesTreeNode
23      extends BaseMutableTreeNode
24      implements ProjectChangeListener {
25      /***
26       * The project whose dependencies to display as child nodes.
27       */
28      private Project project;
29  
30      /***
31       * The model of the whole tree. Used to delegate change operations to.
32       */
33      private DefaultTreeModel treeModel;
34  
35      /***
36       * @param project The project whose dependencies to display as child nodes.
37       * @param treeModel The model of the whole tree.
38       *                  Used to delegate change operations to.
39       */
40      public DependenciesTreeNode(
41              final Project project,
42              final DefaultTreeModel treeModel) {
43          this.project = project;
44          if (this.project.isRootProject()) {
45              this.project.addChangeListener(this);
46          }
47          this.treeModel = treeModel;
48      }
49  
50      /*
51       * (non-Javadoc)
52       * @see de.matthias_burbach.deputy.swing.BaseMutableTreeNode#initChildren()
53       */
54      /***
55       * {@inheritDoc}
56       */
57      protected void initChildren() {
58          for (Iterator iter = project.getDependencies(); iter.hasNext();) {
59              Project childProject = (Project) iter.next();
60              ProjectQualifier cause =
61                  (ProjectQualifier)
62                      project.getDependencyCauses().get(childProject);
63              String suffix = getSuffix(cause);
64              ProjectTreeNode childNode =
65                  new ProjectTreeNode(childProject, treeModel, suffix);
66              add(childNode);
67          }
68      }
69  
70      /*(non-Javadoc)
71       * @see de.matthias_burbach.deputy.core.ProjectChangeListener
72       *          #addedDependency(de.matthias_burbach.deputy.core.Project, int)
73       */
74      /***
75       * {@inheritDoc}
76       */
77      public void addedDependency(
78              final Project addedDependency, final int index) {
79          if (childrenAreInitialized()) {
80              ProjectTreeNode childNode =
81                  new ProjectTreeNode(addedDependency, treeModel);
82              treeModel.insertNodeInto(childNode, this, index);
83          }
84      }
85  
86      /*(non-Javadoc)
87       * @see de.matthias_burbach.deputy.core.ProjectChangeListener
88       *          #removedDependency(de.matthias_burbach.deputy.core.Project)
89       */
90      /***
91       * {@inheritDoc}
92       */
93      public void removedDependency(final Project removedDependency) {
94          if (childrenAreInitialized()) {
95              ProjectTreeNode childNode
96                  = findChildNodeForDependency(removedDependency);
97              treeModel.removeNodeFromParent(childNode);
98          }
99      }
100 
101     /*(non-Javadoc)
102      * @see de.matthias_burbach.deputy.core.ProjectChangeListener
103      *          #removedIndirectDependency(
104      *              de.matthias_burbach.deputy.core.Project)
105      */
106     /***
107      * {@inheritDoc}
108      */
109     public void removedIndirectDependency(final Project removedDependency) {
110         // ignore, this node does not display indirect dependencies
111     }
112 
113     /***
114      * Tries to find the child node representing the dependency passed in.
115      *
116      * @param dependency The dependency to logically find among the child nodes.
117      * @return The tree node representing the dependency or <code>null</code>.
118      */
119     private ProjectTreeNode findChildNodeForDependency(
120             final Project dependency) {
121         ProjectTreeNode result = null;
122         for (Enumeration nodes = children(); nodes.hasMoreElements();) {
123             ProjectTreeNode childNode =
124                 (ProjectTreeNode) nodes.nextElement();
125             if (childNode.getProject().equals(dependency)) {
126                 result = childNode;
127                 break;
128             }
129         }
130         return result;
131     }
132 
133     /*(non-Javadoc)
134      * @see java.lang.Object#toString()
135      */
136     /***
137      * {@inheritDoc}
138      */
139     public String toString() {
140         return "dependencies";
141     }
142 }