001 /*
002 * $Id: FileSystemModel.java,v 1.3 2005/10/26 11:44:30 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.logging.Level;
026 import java.util.logging.Logger;
027
028 import javax.swing.tree.TreeNode;
029
030 /**
031 * FileSystemModel
032 *
033 * @author Ramesh Gupta
034 */
035 public class FileSystemModel extends DefaultTreeTableModel {
036 private static final Logger LOG = Logger.getLogger(FileSystemModel.class
037 .getName());
038 protected boolean asksAllowsChildren;
039
040 public FileSystemModel() {
041 this(new FileNode(new File(File.separator)));
042 }
043
044 public FileSystemModel(TreeNode root) {
045 this(root, false);
046 }
047
048 public FileSystemModel(TreeNode root, boolean asksAllowsChildren) {
049 super(root);
050 this.asksAllowsChildren = asksAllowsChildren;
051 }
052
053 public Object getChild(Object parent, int index) {
054 try {
055 return ((FileNode)parent).getChildren().get(index);
056 }
057 catch (Exception ex) {
058 return super.getChild(parent, index);
059 }
060 }
061
062 public int getChildCount(Object parent) {
063 try {
064 return ((FileNode)parent).getChildren().size();
065 }
066 catch (Exception ex) {
067 return super.getChildCount(parent);
068 }
069 }
070
071 public int getColumnCount() {
072 /**@todo Implement this org.jdesktopx.swing.treetable.TreeTableModel abstract method*/
073 return 4;
074 }
075
076 public String getColumnName(int column) {
077 switch(column) {
078 case 0:
079 return "Name";
080 case 1:
081 return "Size";
082 case 2:
083 return "Directory";
084 case 3:
085 return "Modification Date";
086 default:
087 return "Column " + column;
088 }
089 }
090
091 public Object getValueAt(Object node, int column) {
092 final File file = ((FileNode)node).getFile();
093 try {
094 switch(column) {
095 case 0:
096 return file.getName();
097 case 1:
098 return file.isFile() ? new Integer((int)file.length()) : ZERO;
099 case 2:
100 return new Boolean(!file.isFile());
101 case 3:
102 return new java.util.Date(file.lastModified());
103 }
104 }
105 catch (Exception ex) {
106 LOG.log(Level.WARNING, "Problem accessing file", ex);
107
108 }
109
110 return null;
111 }
112
113 // The the returned file length for directories.
114 private static final Integer ZERO = new Integer(0);
115 }