1 7 8 package org.jdesktop.swing.treetable; 9 10 import java.io.File ; 11 12 import javax.swing.tree.TreeNode ; 13 14 import org.jdesktop.swing.treetable.DefaultTreeTableModel; 15 16 21 public class FileSystemModel extends DefaultTreeTableModel { 22 protected boolean asksAllowsChildren; 23 24 public FileSystemModel() { 25 this(new FileNode(new File (File.separator))); 26 } 27 28 public FileSystemModel(TreeNode root) { 29 this(root, false); 30 } 31 32 public FileSystemModel(TreeNode root, boolean asksAllowsChildren) { 33 super(root); 34 this.asksAllowsChildren = asksAllowsChildren; 35 } 36 37 public Object getChild(Object parent, int index) { 38 try { 39 return ((FileNode)parent).getChildren().get(index); 40 } 41 catch (Exception ex) { 42 return super.getChild(parent, index); 43 } 44 } 45 46 public int getChildCount(Object parent) { 47 try { 48 return ((FileNode)parent).getChildren().size(); 49 } 50 catch (Exception ex) { 51 return super.getChildCount(parent); 52 } 53 } 54 55 public int getColumnCount() { 56 57 return 4; 58 } 59 60 public String getColumnName(int column) { 61 switch(column) { 62 case 0: 63 return "Name"; 64 case 1: 65 return "Size"; 66 case 2: 67 return "Directory"; 68 case 3: 69 return "Modification Date"; 70 default: 71 return "Column " + column; 72 } 73 } 74 75 public Object getValueAt(Object node, int column) { 76 final File file = ((FileNode)node).getFile(); 77 try { 78 switch(column) { 79 case 0: 80 return file.getName(); 81 case 1: 82 return file.isFile() ? new Integer ((int)file.length()) : ZERO; 83 case 2: 84 return new Boolean (!file.isFile()); 85 case 3: 86 return new java.util.Date (file.lastModified()); 87 } 88 } 89 catch (Exception ex) { 90 ex.printStackTrace(); 91 } 92 93 return null; 94 } 95 96 private static final Integer ZERO = new Integer (0); 98 } 99 | Popular Tags |