001    /*
002     *  Licensed under the Apache License, Version 2.0 (the "License");
003     *  you may not use this file except in compliance with the License.
004     *  You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     *  Unless required by applicable law or agreed to in writing, software
009     *  distributed under the License is distributed on an "AS IS" BASIS,
010     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     *  See the License for the specific language governing permissions and
012     *  limitations under the License.
013     */
014    /*
015     * This file has been modified by Chris Pheby in accordance with Section 4.2
016     * of the Apache Software License
017     */
018    package org.jadira.dependencynavigator.gui;
019    
020    import org.jadira.dependencynavigator.model.Artifact;
021    
022    import javax.swing.event.TreeModelListener;
023    import javax.swing.tree.TreeModel;
024    import javax.swing.tree.TreePath;
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    public class DependencyTreeModel implements TreeModel {
029        private Artifact root;
030        private List<TreeModelListener> listeners;
031    
032        public DependencyTreeModel(Artifact root) {
033            this.root = root;
034            listeners = new ArrayList<TreeModelListener>();
035        }
036    
037        public Artifact getRoot() {
038            return root;
039        }
040    
041        public int getChildCount(Object parent) {
042            Artifact parentArtifact = (Artifact) parent;
043            if (parentArtifact.isLeaf()) {
044                return 0;
045            }
046            return parentArtifact.getDependencies().size();
047        }
048    
049        public boolean isLeaf(Object node) {
050            Artifact nodeArtifact = (Artifact) node;
051            return nodeArtifact.isLeaf();
052        }
053    
054        public void addTreeModelListener(TreeModelListener l) {
055            listeners.add(l);
056        }
057    
058        public void removeTreeModelListener(TreeModelListener l) {
059            listeners.remove(l);
060        }
061    
062        public Object getChild(Object parent, int index) {
063            Artifact parentArtifact = (Artifact) parent;
064            return parentArtifact.getDependencies().get(index);
065        }
066    
067        public int getIndexOfChild(Object parent, Object child) {
068            Artifact parentArtifact = (Artifact) parent;
069            return parentArtifact.getDependencies().indexOf(child);
070        }
071    
072        public void valueForPathChanged(TreePath path, Object newValue) {
073            throw new UnsupportedOperationException();
074        }
075    }