KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MyTableModel


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 import javax.swing.table.AbstractTableModel JavaDoc;
22 import java.io.File JavaDoc;
23
24
25 /**
26  * JDIC API demo class.
27  * <p>
28  * A redefined TableModel class.
29  */

30
31 public class MyTableModel extends AbstractTableModel JavaDoc {
32     // Object columnNames[] = {"Name", "Size", "Type", "Modified"};
33

34     Object JavaDoc columnNamesFile[] = {"Name", "Size", "Type", "Modified"};
35     // ===
36
// For Windows "My Computer" node only.
37
// ===
38
Object JavaDoc columnNamesMyComputer[] = {"Name", "Type", "Size", "FreeSpace"};
39     // Object columnNames[] = getColumnNames();
40
Object JavaDoc columnNames[] = columnNamesFile;
41
42     Object JavaDoc data[][] = getTableData();
43
44     public int getRowCount() {
45         return (data == null) ? 0 : data.length;
46     }
47
48     public int getColumnCount() {
49         return columnNames.length;
50     }
51
52     public String JavaDoc getColumnName(int col) {
53         return columnNames[col].toString();
54     }
55
56     public Class JavaDoc getColumnClass(int column) {
57         return getValueAt(0, column).getClass();
58     }
59
60     public Object JavaDoc getValueAt(int row, int col) {
61         return data[row][col];
62     }
63
64     public void setValueAt(Object JavaDoc value, int row, int col) {
65         data[row][col] = value;
66         fireTableCellUpdated(row, col);
67     }
68
69     // Make the table uneditable.
70
public boolean isCellEditable(int row, int column) {
71         return false;
72     }
73
74     // ===
75
// For Windows "My Computer" node only.
76
// ===
77
public void setColumnNames() {
78         columnNames = getColumnNames();
79     }
80
81     public void setTableData() {
82         data = getTableData();
83     }
84
85     // Gets the column names
86
private Object JavaDoc[] getColumnNames() {
87         MyTreeNode selectedTreeNode = FileExplorer.selectedTreeNode;
88
89         if (selectedTreeNode == null) {
90             return null;
91         }
92
93         File JavaDoc selectedDir = (File JavaDoc) selectedTreeNode.getUserObject();
94
95         if (selectedDir.equals(new File JavaDoc(FileExplorer.MY_COMPUTER_FOLDER_PATH))) {
96             return columnNamesMyComputer;
97         } else {
98             return columnNamesFile;
99         }
100     }
101
102     // Gets the table data from the left tree.
103
private Object JavaDoc[][] getTableData() {
104         MyTreeNode selectedTreeNode = FileExplorer.selectedTreeNode;
105
106         if (selectedTreeNode == null) {
107             return null;
108         }
109
110         File JavaDoc selectedDir = (File JavaDoc) selectedTreeNode.getUserObject();
111
112         // ===
113
// For Windows "My Computer" node only.
114
// ===
115
if (selectedDir.equals(new File JavaDoc(FileExplorer.MY_COMPUTER_FOLDER_PATH))) {
116             File JavaDoc[] drivers = MyUtility.getRoots();
117             int driverNum = drivers.length;
118             Object JavaDoc data[][] = new Object JavaDoc[driverNum][columnNames.length];
119
120             // Remove A: drive from the initial list of drives, since whenever the
121
// JTree is repaint, it tries to read floppy drive A:\.
122
int firstDriverNum = 0;
123
124             if (drivers[firstDriverNum].getPath().toLowerCase().startsWith("a:")) {
125                 firstDriverNum = 1;
126             }
127       
128             int curDriverNum = 0;
129
130             for (int i = firstDriverNum; i < driverNum; i++) {
131                 data[curDriverNum][0] = new DiskObject(drivers[i].getAbsolutePath(),
132                         DiskObject.TYPE_DRIVER);
133                 data[curDriverNum][1] = "";
134                 data[curDriverNum][2] = " " + DiskObject.TYPE_DRIVER;
135                 data[curDriverNum][3] = (new java.util.Date JavaDoc(drivers[i].lastModified())).toString();
136                 curDriverNum++;
137             }
138
139             return data;
140         } else {
141
142             File JavaDoc[] files = selectedDir.listFiles();
143
144             // The selected dir or driver might have no children.
145
if (files == null) {
146                 return null;
147             }
148
149             int fileNum = files.length;
150             Object JavaDoc data[][] = new Object JavaDoc[fileNum][columnNames.length];
151             int curFileNum = 0;
152
153             // Filter out the files and put ahead of the directories.
154
for (int i = 0; i < fileNum; i++) {
155                 File JavaDoc file = files[i];
156
157                 if (!file.isDirectory()) {
158                     data[curFileNum][0] = new DiskObject(file.getName(),
159                             DiskObject.TYPE_FILE);
160                     data[curFileNum][1] = MyUtility.length2KB(file.length());
161                     data[curFileNum][2] = " " + DiskObject.TYPE_FILE;
162                     data[curFileNum][3] = (new java.util.Date JavaDoc(file.lastModified())).toString();
163                     curFileNum++;
164                 }
165             }
166
167             for (int i = 0; i < fileNum; i++) {
168                 File JavaDoc file = files[i];
169
170                 if (file.isDirectory()) {
171                     data[curFileNum][0] = new DiskObject(file.getName(),
172                             DiskObject.TYPE_FOLDER);
173                     data[curFileNum][1] = "";
174                     data[curFileNum][2] = " " + DiskObject.TYPE_FOLDER;
175                     data[curFileNum][3] = (new java.util.Date JavaDoc(file.lastModified())).toString();
176                     curFileNum++;
177                 }
178             }
179
180             return data;
181         }
182     }
183 }
184
Popular Tags