1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.table;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25
26 import javax.swing.DefaultCellEditor;
27 import javax.swing.JTextField;
28
29 import org.apache.log4j.Logger;
30 import org.xnap.XNap;
31 import org.xnap.gui.StatusBar;
32 import org.xnap.util.FileHelper;
33
34 /***
35 * Table model which displays repository files in its columns.
36 */
37 public class LibraryTableModel extends AbstractColumnTableModel
38 {
39
40
41
42 public static final int NAME = 0;
43 public static final int FULLNAME = 1;
44 public static final int PATH = 2;
45 public static final int TYPE = 3;
46 public static final int SIZE = 4;
47 public static final int MODIFIED = 5;
48 public static final int INFO = 6;
49
50
51
52 protected Column columns[] = new Column[] {
53 new Column("filename", XNap.tr("Name"), String.class,
54 new StringCellRenderer()),
55 new Column("fullname", XNap.tr("Fullname"), String.class,
56 new StringCellRenderer()),
57 new Column("path", XNap.tr("Path"), String.class,
58 new StringCellRenderer()),
59 new Column("type", XNap.tr("Type"), String.class),
60 new Column("size", XNap.tr("Size"), Long.class,
61 new FilesizeCellRenderer()),
62 new Column("modified", XNap.tr("Modified"), Long.class,
63 new TimeCellRenderer()),
64
65
66 };
67
68 private static Logger logger = Logger.getLogger(LibraryTableModel.class);
69
70 private ArrayList data = new ArrayList();
71 private long totalSize;
72 private int editableColumn = -1;
73 private int editableRow = -1;
74 private File currentDir;
75
76
77
78 public LibraryTableModel()
79 {
80 addColumns(columns);
81 DefaultCellEditor dce = new DefaultCellEditor(new JTextField());
82 dce.setClickCountToStart(Integer.MAX_VALUE);
83 columns[NAME].setCellEditor(dce);
84 }
85
86
87
88 public void add(File f)
89 {
90 if (f.isDirectory()) {
91 add(f.listFiles());
92 currentDir = f;
93 }
94 else {
95 addFile(f);
96 }
97 }
98
99 public void add(File[] list)
100 {
101 int firstRow = data.size();
102
103 for (int i = 0; i < list.length; i++) {
104 if (list[i].isFile()) {
105 addFile(list[i]);
106 }
107 }
108 fireTableRowsInserted(firstRow, data.size() - 1);
109 }
110
111 public void addFile(File file)
112 {
113
114
115
116 data.add(file);
117 totalSize += file.length();
118 }
119
120 public boolean delete(File f)
121 {
122 int i = data.indexOf(f);
123 if (i != -1) {
124 if (f.delete()) {
125 remove(i);
126 return true;
127 }
128 }
129 return false;
130 }
131
132 public void clear()
133 {
134 int k = data.size() - 1;
135 data.clear();
136 fireTableRowsDeleted(0, k);
137
138 totalSize = 0;
139 }
140
141
142
143
144
145
146 public File get(int i)
147 {
148 return (File)data.get(mapToIndex(i));
149 }
150
151 public int getRowCount()
152 {
153 return data.size();
154 }
155
156 public Object get(int i, int j)
157 {
158 if (i >= data.size())
159 return null;
160
161
162 File f = (File)data.get(i);
163
164 if (f == null)
165 return null;
166
167 switch (j) {
168 case NAME:
169 return f.getName();
170 case FULLNAME:
171 return f.getPath();
172 case PATH:
173 return f.getParent();
174 case SIZE:
175 return new Long(f.length());
176 case TYPE:
177 return FileHelper.extension(f.getName());
178 case MODIFIED:
179 return new Long(f.lastModified());
180 case INFO:
181
182 return null;
183 default:
184 return null;
185 }
186 }
187
188 public String getTableName()
189 {
190 return XNap.tr("Library Table");
191 }
192
193 public long getTotalSize()
194 {
195 return totalSize;
196 }
197
198 public boolean isCellEditable(int row, int column)
199 {
200 return (row == editableRow && column == editableColumn);
201 }
202
203 public void remove(int i)
204 {
205 data.remove(i);
206 fireTableRowsDeleted(i, i);
207 }
208
209 public void setCellEditable(int row, int column)
210 {
211 editableRow = row;
212 editableColumn = column;
213 }
214
215 public void setValueAt(Object obj, int row, int column)
216 {
217 if (!(obj instanceof String))
218 return;
219
220 String newName = (String)obj;
221
222 File file = get(row);
223
224 if (newName.length() > 0) {
225 try {
226 file = FileHelper.moveUnique(file, file.getParent(), newName);
227 data.set(mapToIndex(row), file);
228 fireTableRowsUpdated(row, row);
229 }
230 catch (IOException ie) {
231 logger.debug("Could not rename file", ie);
232 StatusBar.setText(XNap.tr("Could not rename file: {0}",
233 ie.getLocalizedMessage()));
234 }
235 }
236 }
237 }