1 20 21 import javax.swing.table.AbstractTableModel ; 22 import java.io.File ; 23 24 25 30 31 public class MyTableModel extends AbstractTableModel { 32 34 Object columnNamesFile[] = {"Name", "Size", "Type", "Modified"}; 35 Object columnNamesMyComputer[] = {"Name", "Type", "Size", "FreeSpace"}; 39 Object columnNames[] = columnNamesFile; 41 42 Object 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 getColumnName(int col) { 53 return columnNames[col].toString(); 54 } 55 56 public Class getColumnClass(int column) { 57 return getValueAt(0, column).getClass(); 58 } 59 60 public Object getValueAt(int row, int col) { 61 return data[row][col]; 62 } 63 64 public void setValueAt(Object value, int row, int col) { 65 data[row][col] = value; 66 fireTableCellUpdated(row, col); 67 } 68 69 public boolean isCellEditable(int row, int column) { 71 return false; 72 } 73 74 public void setColumnNames() { 78 columnNames = getColumnNames(); 79 } 80 81 public void setTableData() { 82 data = getTableData(); 83 } 84 85 private Object [] getColumnNames() { 87 MyTreeNode selectedTreeNode = FileExplorer.selectedTreeNode; 88 89 if (selectedTreeNode == null) { 90 return null; 91 } 92 93 File selectedDir = (File ) selectedTreeNode.getUserObject(); 94 95 if (selectedDir.equals(new File (FileExplorer.MY_COMPUTER_FOLDER_PATH))) { 96 return columnNamesMyComputer; 97 } else { 98 return columnNamesFile; 99 } 100 } 101 102 private Object [][] getTableData() { 104 MyTreeNode selectedTreeNode = FileExplorer.selectedTreeNode; 105 106 if (selectedTreeNode == null) { 107 return null; 108 } 109 110 File selectedDir = (File ) selectedTreeNode.getUserObject(); 111 112 if (selectedDir.equals(new File (FileExplorer.MY_COMPUTER_FOLDER_PATH))) { 116 File [] drivers = MyUtility.getRoots(); 117 int driverNum = drivers.length; 118 Object data[][] = new Object [driverNum][columnNames.length]; 119 120 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 (drivers[i].lastModified())).toString(); 136 curDriverNum++; 137 } 138 139 return data; 140 } else { 141 142 File [] files = selectedDir.listFiles(); 143 144 if (files == null) { 146 return null; 147 } 148 149 int fileNum = files.length; 150 Object data[][] = new Object [fileNum][columnNames.length]; 151 int curFileNum = 0; 152 153 for (int i = 0; i < fileNum; i++) { 155 File 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 (file.lastModified())).toString(); 163 curFileNum++; 164 } 165 } 166 167 for (int i = 0; i < fileNum; i++) { 168 File 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 (file.lastModified())).toString(); 176 curFileNum++; 177 } 178 } 179 180 return data; 181 } 182 } 183 } 184 | Popular Tags |