001package jmri.jmrit.catalog;
002
003import java.io.File;
004import java.util.HashMap;
005import jmri.CatalogTreeNode;
006import org.apache.commons.io.FilenameUtils;
007
008/**
009 * TreeModel used by CatalogPanel to create a tree of resources.
010 * <p>
011 * Source of the tree content is the file system. Only directories are included
012 * in the tree. A filter can be set to extract particular file types.
013 *
014 * @author Pete Cressman Copyright 2009
015 *
016 */
017public class CatalogTreeFS extends AbstractCatalogTree {
018
019    String[] _filter;
020
021    public CatalogTreeFS(String sysName, String userName) {
022        super(sysName, userName);
023    }
024
025    public void setFilter(String[] filter) {
026        _filter = new String[filter.length];
027        for (int i = 0; i < filter.length; i++) {
028            _filter[i] = filter[i];
029        }
030    }
031
032    public String[] getFilter() {
033        String[] filter = new String[_filter.length];
034        for (int i = 0; i < _filter.length; i++) {
035            filter[i] = _filter[i];
036        }
037        return filter;
038    }
039
040    boolean filter(String ext) {
041        if (ext == null) {
042            return false;
043        }
044        if (_filter == null || _filter.length == 0) {
045            return true;
046        }
047        for (int i = 0; i < _filter.length; i++) {
048            if (ext.equals(_filter[i])) {
049                return true;
050            }
051        }
052        return false;
053    }
054
055    int count = 0;
056    int leafcount = 0;
057
058    /**
059     * Recursively add nodes to the tree
060     *
061     * @param pName   Name of the resource to be scanned; this is only used for
062     *                the human-readable tree
063     * @param pPath   Path to this resource, including the pName part
064     * @param pParent Node for the parent of the resource to be scanned, e.g.
065     *                where in the tree to insert it.
066     */
067    @Override
068    public void insertNodes(String pName, String pPath, CatalogTreeNode pParent) {
069        File fp = new File(pPath);
070        if (!fp.exists()) {
071            return;
072        }
073
074        // suppress overhead files
075        String filename = fp.getName();
076        if (filename.startsWith(".")) {
077            return;
078        }
079        if (filename.equals("CVS")) {
080            return;
081        }
082
083        if (fp.isDirectory()) {
084            // first, represent this one
085            CatalogTreeNode newElement = new CatalogTreeNode(pName);
086            insertNodeInto(newElement, pParent, pParent.getChildCount());
087            String[] sp = fp.list();
088
089            if (sp !=null) {
090
091                // sort list of files alphabetically
092                if (sp.length > 0) {
093                    java.util.ArrayList<String> aList = new java.util.ArrayList<>(java.util.Arrays.asList(sp));
094                    java.util.Collections.sort(aList);
095                    sp = aList.toArray(sp);
096                }
097
098                for (int i = 0; i < sp.length; i++) {
099                    log.debug("Descend into resource: {} count {}",sp[i], count++);
100                    insertNodes(sp[i], pPath + File.separator + sp[i], newElement);
101                }
102            }
103        } else /* leaf */ {
104            String ext = FilenameUtils.getExtension(fp.getName());
105            if (!filter(ext)) {
106                return;
107            }
108            int index = filename.indexOf('.');
109            if (index > 0) {
110                filename = filename.substring(0, index);
111            }
112            log.debug("add leaf: {} count {}", filename, leafcount++);
113            pParent.addLeaf(filename, pPath);
114        }
115    }
116
117    @Override
118    public void setProperty(String key, Object value) {
119        if (parameters == null) {
120            parameters = new HashMap<>();
121        }
122        parameters.put(key, value);
123    }
124
125    @Override
126    public Object getProperty(String key) {
127        if (parameters == null) {
128            parameters = new HashMap<>();
129        }
130        return parameters.get(key);
131    }
132
133    @Override
134    public java.util.Set<String> getPropertyKeys() {
135        if (parameters == null) {
136            parameters = new HashMap<>();
137        }
138        return parameters.keySet();
139    }
140
141    @Override
142    public void removeProperty(String key) {
143        if (parameters == null) {
144            return;
145        }
146        parameters.remove(key);
147    }
148
149    HashMap<String, Object> parameters = null;
150
151    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CatalogTreeFS.class);
152}