KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > treetable > FileSystemModel


1 /*
2  * $Id: FileSystemModel.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.treetable;
9
10 import java.io.File JavaDoc;
11
12 import javax.swing.tree.TreeNode JavaDoc;
13
14 import org.jdesktop.swing.treetable.DefaultTreeTableModel;
15
16 /**
17  * FileSystemModel
18  *
19  * @author Ramesh Gupta
20  */

21 public class FileSystemModel extends DefaultTreeTableModel {
22     protected boolean asksAllowsChildren;
23
24     public FileSystemModel() {
25     this(new FileNode(new File JavaDoc(File.separator)));
26     }
27
28     public FileSystemModel(TreeNode JavaDoc root) {
29     this(root, false);
30     }
31
32     public FileSystemModel(TreeNode JavaDoc root, boolean asksAllowsChildren) {
33     super(root);
34     this.asksAllowsChildren = asksAllowsChildren;
35     }
36
37     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
38     try {
39         return ((FileNode)parent).getChildren().get(index);
40     }
41     catch (Exception JavaDoc ex) {
42         return super.getChild(parent, index);
43     }
44     }
45
46     public int getChildCount(Object JavaDoc parent) {
47     try {
48         return ((FileNode)parent).getChildren().size();
49     }
50     catch (Exception JavaDoc ex) {
51         return super.getChildCount(parent);
52     }
53     }
54
55     public int getColumnCount() {
56     /**@todo Implement this org.jdesktopx.swing.treetable.TreeTableModel abstract method*/
57     return 4;
58     }
59
60     public String JavaDoc 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 JavaDoc getValueAt(Object JavaDoc node, int column) {
76     final File JavaDoc 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 JavaDoc((int)file.length()) : ZERO;
83         case 2:
84         return new Boolean JavaDoc(!file.isFile());
85         case 3:
86         return new java.util.Date JavaDoc(file.lastModified());
87         }
88     }
89     catch (Exception JavaDoc ex) {
90         ex.printStackTrace();
91     }
92
93     return null;
94     }
95
96     // The the returned file length for directories.
97
private static final Integer JavaDoc ZERO = new Integer JavaDoc(0);
98 }
99
Popular Tags