001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.file;
020    
021    import com.mucommander.file.util.PathUtils;
022    
023    import javax.swing.tree.DefaultMutableTreeNode;
024    
025    /**
026     * Stores archive entries and organizes them in a tree structure that maps entries in the way they are organized
027     * inside the archive. An instance of <code>ArchiveEntryTree</code> also acts as the root node: all entry nodes
028     * are children of it (direct or indirect).
029     *
030     * @author Maxence Bernard
031     */
032    public class ArchiveEntryTree extends DefaultMutableTreeNode {
033    
034        /**
035         * Creates a new empty tree.
036         */
037        public ArchiveEntryTree() {
038        }
039    
040        /**
041         * Adds the given entry to the archive tree, creating parent nodes as necessary.
042         *
043         * @param entry the entry to add to the tree
044         */
045        public void addArchiveEntry(ArchiveEntry entry) {
046            String entryPath = entry.getPath();
047            int entryDepth = entry.getDepth();
048            int slashPos = 0;
049            DefaultMutableTreeNode node = this;
050            for(int d=0; d<=entryDepth; d++) {
051                if(d==entryDepth && !entry.isDirectory()) {
052                    // Create a leaf node for the entry
053                    node.add(new DefaultMutableTreeNode(entry, true));
054                    break;
055                }
056    
057                String subPath = PathUtils.removeTrailingSeparator(d==entryDepth?entryPath:entryPath.substring(0, (slashPos=entryPath.indexOf('/', slashPos)+1)), "/");
058    
059                int nbChildren = node.getChildCount();
060                DefaultMutableTreeNode childNode = null;
061                boolean matchFound = false;
062                for(int c=0; c<nbChildren; c++) {
063                    childNode = (DefaultMutableTreeNode)node.getChildAt(c);
064                    // Path comparison is 'trailing slash insensitive'
065                    if(PathUtils.removeTrailingSeparator(((ArchiveEntry)childNode.getUserObject()).getPath(), "/").equals(subPath)) {
066                        // Found a match
067                        matchFound = true;
068                        break;
069                    }
070                }
071    
072                if(matchFound) {
073                    if(d==entryDepth) {
074                        // if(com.mucommander.Debug.ON) com.mucommander.Debug.trace("Replacing entry for node "+childNode);
075                        // Replace existing entry
076                        childNode.setUserObject(entry);
077                    }
078                    else {
079                        node = childNode;
080                    }
081                }
082                else {
083                    if(d==entryDepth) {
084                        // Create a leaf node for the entry
085                        node.add(new DefaultMutableTreeNode(entry, true));
086                    }
087                    else {
088                        // if(com.mucommander.Debug.ON) com.mucommander.Debug.trace("Creating node for "+subPath);
089                        childNode = new DefaultMutableTreeNode(new ArchiveEntry(subPath, true, entry.getDate(), 0), true);
090                        node.add(childNode);
091                        node = childNode;
092                    }
093                }
094            }
095        }
096    
097    
098        /**
099         * Finds and returns the node that corresponds to the specified entry path, <code>null</code> if no entry matching
100         * the path could be found.
101         *
102         * <p>Important note: the given path's separator character must be '/' and the path must be relative to the
103         * archive's root, i.e. not start with a leading '/', otherwise the entry will not be found. Trailing separators
104         * are ignored when paths are compared, for example the path 'temp' will match the entry 'temp/'.
105         *
106         * @param entryPath the path to the entry to look up in this tree
107         * @return the node that corresponds to the specified entry path
108         */
109        public DefaultMutableTreeNode findEntryNode(String entryPath) {
110            int entryDepth = ArchiveEntry.getDepth(entryPath);
111            int slashPos = 0;
112            DefaultMutableTreeNode currentNode = this;
113            for(int d=0; d<=entryDepth; d++) {
114                // Remove any trailing slash to compare paths without trailing slashs
115                 String subPath = PathUtils.removeTrailingSeparator(d==entryDepth?entryPath:entryPath.substring(0, (slashPos=entryPath.indexOf('/', slashPos)+1)), "/");
116    
117                int nbChildren = currentNode.getChildCount();
118                DefaultMutableTreeNode matchNode = null;
119                for(int c=0; c<nbChildren; c++) {
120                    DefaultMutableTreeNode childNode = (DefaultMutableTreeNode)currentNode.getChildAt(c);
121    
122                    // Remove any trailing slash to compare paths without trailing slashs
123                    String childNodePath = PathUtils.removeTrailingSeparator(((ArchiveEntry)childNode.getUserObject()).getPath(), "/");
124    
125                    if(childNodePath.equals(subPath)) {
126                        // Found the node, let's return it
127                        matchNode = childNode;
128                        break;
129                    }
130                }
131    
132                if(matchNode==null)
133                    return null;    // No node maching the provided path, return null
134    
135                currentNode = matchNode;
136            }
137    
138            return currentNode;
139        }
140    }