001package jmri;
002
003import java.util.ArrayList;
004import java.util.Enumeration;
005import java.util.Iterator;
006
007import jmri.util.swing.DefaultMutableTreeNode;
008import javax.swing.tree.TreeNode;
009
010/**
011 * Node of a CatalogTree.
012 * <p>
013 * Name for the node Path is info needed for leafs.
014 *
015 * @author Pete Cressman Copyright 2009
016 */
017public class CatalogTreeNode extends DefaultMutableTreeNode {
018
019    // Sorted by height for ease of display in CatalogPanel
020    private ArrayList<CatalogTreeLeaf> _leafs = new ArrayList<>();
021
022    public CatalogTreeNode(String name) {
023        super(name);
024    }
025
026    /**
027     * Append leaf to the end of the leafs list.
028     *
029     * @param leaf the leaf to add
030     */
031    public void addLeaf(CatalogTreeLeaf leaf) {
032        _leafs.add(leaf);
033    }
034
035    /**
036     * Insert leaf according to height. Dan Boudreau 10/15/2018 eliminated the
037     * check for valid icon and the sorting of the icons by height. Improves
038     * load time at initialization by an order of magnitude.
039     *
040     * @param name name of the new leaf
041     * @param path path to the new leaf
042     */
043    public void addLeaf(String name, String path) {
044        int h = 0;
045        _leafs.add(new CatalogTreeLeaf(name, path, h)); //  name is non-localized
046    }
047
048    /**
049     * Leafs can be used for many-to-many relations.
050     *
051     * @param name the leafs to remove
052     */
053    public void deleteLeaves(String name) {
054        for (Iterator<CatalogTreeLeaf> iterator = _leafs.iterator(); iterator.hasNext();) {
055            CatalogTreeLeaf leaf = iterator.next();
056            if (name.equals(leaf.getName())) {
057                iterator.remove(); // Safely remove the current element from the iterator and the list
058            }
059        }
060    }
061
062    public void deleteLeaf(String name, String path) {
063        for (int i = 0; i < _leafs.size(); i++) {
064            CatalogTreeLeaf leaf = _leafs.get(i);
065            if (name.equals(leaf.getName()) && path.equals(leaf.getPath())) {
066                _leafs.remove(i);
067                return;
068            }
069        }
070    }
071
072    public CatalogTreeLeaf getLeaf(String name, String path) {
073        for (CatalogTreeLeaf leaf : _leafs) {
074            if (name.equals(leaf.getName()) && path.equals(leaf.getPath())) {
075                return leaf;
076            }
077        }
078        return null;
079    }
080
081    /**
082     * Leafs can be used for many-to-many relations.
083     *
084     * @param name name of the leafs to get
085     * @return a list of matching leafs; an empty list if there are no matching
086     *         leafs
087     */
088    public ArrayList<CatalogTreeLeaf> getLeaves(String name) {
089        ArrayList<CatalogTreeLeaf> leafs = new ArrayList<>();
090        for (CatalogTreeLeaf leaf : _leafs) {
091            if (name.equals(leaf.getName())) {
092                leafs.add(leaf);
093            }
094        }
095        return leafs;
096    }
097
098    @Override
099    public Enumeration<TreeNode> children() { // for JDK 9 typing
100        return super.children();
101    }
102
103    public ArrayList<CatalogTreeLeaf> getLeaves() {
104        return _leafs;
105    }
106
107    public int getNumLeaves() {
108        return _leafs.size();
109    }
110
111    public void setLeaves(ArrayList<CatalogTreeLeaf> leafs) {
112        _leafs = leafs;
113    }
114
115    //    private final static Logger log = LoggerFactory.getLogger(CatalogTreeNode.class);
116}