View Javadoc

1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
17   */
18  package org.jadira.dependencynavigator.gui;
19  
20  import org.jadira.dependencynavigator.model.Artifact;
21  
22  import javax.swing.event.TreeModelListener;
23  import javax.swing.tree.TreeModel;
24  import javax.swing.tree.TreePath;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  public class DependencyTreeModel implements TreeModel {
29      private Artifact root;
30      private List<TreeModelListener> listeners;
31  
32      public DependencyTreeModel(Artifact root) {
33          this.root = root;
34          listeners = new ArrayList<TreeModelListener>();
35      }
36  
37      public Artifact getRoot() {
38          return root;
39      }
40  
41      public int getChildCount(Object parent) {
42          Artifact parentArtifact = (Artifact) parent;
43          if (parentArtifact.isLeaf()) {
44              return 0;
45          }
46          return parentArtifact.getDependencies().size();
47      }
48  
49      public boolean isLeaf(Object node) {
50          Artifact nodeArtifact = (Artifact) node;
51          return nodeArtifact.isLeaf();
52      }
53  
54      public void addTreeModelListener(TreeModelListener l) {
55          listeners.add(l);
56      }
57  
58      public void removeTreeModelListener(TreeModelListener l) {
59          listeners.remove(l);
60      }
61  
62      public Object getChild(Object parent, int index) {
63          Artifact parentArtifact = (Artifact) parent;
64          return parentArtifact.getDependencies().get(index);
65      }
66  
67      public int getIndexOfChild(Object parent, Object child) {
68          Artifact parentArtifact = (Artifact) parent;
69          return parentArtifact.getDependencies().indexOf(child);
70      }
71  
72      public void valueForPathChanged(TreePath path, Object newValue) {
73          throw new UnsupportedOperationException();
74      }
75  }