001    /*
002     * $Id: FileNode.java,v 1.3 2005/10/13 09:00:00 kleopatra Exp $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    
022    package org.jdesktop.swingx.treetable;
023    
024    import java.io.File;
025    import java.util.List;
026    import java.util.Vector;
027    
028    import javax.swing.tree.DefaultMutableTreeNode;
029    
030    /**
031     * FileNode
032     *
033     * @author Ramesh Gupta
034     */
035    public class FileNode extends DefaultMutableTreeNode {
036        public FileNode(File file) {
037        this.file = file;
038        this.isDir = file.isDirectory();
039        }
040    
041    
042        public boolean getAllowsChildren() {
043        return isDir;
044        }
045    
046        protected List getChildren() {  // rg:changed return type
047        if (children == null) {
048            try {
049            final String[] files = file.list();
050            if (files != null) {
051                // Create an empty list of FileNodes (#elements = files.length)
052                children = new Vector(files.length);
053                final String path = file.getPath();
054                for (int i = 0; i < files.length; i++) {
055                final File childFile = new File(path, files[i]);
056                children.add(new FileNode(childFile));
057                }
058            }
059            }
060            catch (SecurityException se) {
061            }
062        }
063    
064        return children;
065        }
066    
067        public File getFile() {
068        return file;
069        }
070    
071        public boolean isLeaf() {
072        return !isDir;
073        }
074    
075        public String toString() {
076        return file.getName();
077        }
078    
079        private final File      file;
080        private final boolean       isDir;
081    }