1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package wjhk.jupload2.gui.filepanel;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
28
29 import javax.swing.table.AbstractTableModel;
30
31 import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles;
32 import wjhk.jupload2.filedata.DefaultFileData;
33 import wjhk.jupload2.filedata.FileData;
34 import wjhk.jupload2.policies.UploadPolicy;
35
36
37
38
39
40
41
42
43
44
45
46
47 public class FilePanelFlatDataModel2 extends AbstractTableModel {
48
49
50 private static final long serialVersionUID = 1473262424494858913L;
51
52
53
54
55 public final static int COLINDEX_NAME = 0;
56
57
58
59
60 public final static int COLINDEX_SIZE = 1;
61
62
63
64
65 public final static int COLINDEX_DIRECTORY = 2;
66
67
68
69
70 public final static int COLINDEX_MODIFIED = 3;
71
72
73
74
75 private UploadPolicy uploadPolicy = null;
76
77
78
79
80
81 private String COL_NAME = null;
82
83 private String COL_SIZE = null;
84
85 private String COL_DIRECTORY = null;
86
87 private String COL_MODIFIED = null;
88
89 private String COL_CHECKED = "";
90
91 protected String[] columnNames = null;
92
93
94
95
96
97
98
99 protected int[] columnSizePercentage = null;
100
101
102
103
104 protected boolean[] columnEditable = null;
105
106 protected Class<?> columnClasses[] = null;
107
108
109
110
111 private List<FileData> rows = new ArrayList<FileData>();
112
113
114
115
116 public FilePanelFlatDataModel2(UploadPolicy uploadPolicy) {
117
118
119 super();
120
121 this.uploadPolicy = uploadPolicy;
122
123
124 this.COL_NAME = uploadPolicy.getLocalizedString("colName");
125 this.COL_SIZE = uploadPolicy.getLocalizedString("colSize");
126 this.COL_DIRECTORY = uploadPolicy.getLocalizedString("colDirectory");
127 this.COL_MODIFIED = uploadPolicy.getLocalizedString("colModified");
128
129 this.columnNames = new String[] {
130 this.COL_NAME, this.COL_SIZE, this.COL_DIRECTORY, this.COL_MODIFIED, this.COL_CHECKED
131
132 };
133
134
135 this.columnSizePercentage = new int[] {
136 29, 11, 35, 20, 5
137 };
138
139
140 this.columnEditable = new boolean[] {
141 false, false, false, false, true
142 };
143
144
145 int total = 0;
146 for (int i = 0; i < this.columnSizePercentage.length; i += 1) {
147 total += this.columnSizePercentage[i];
148 }
149 if (total != 100) {
150 throw new java.lang.AssertionError("Total sum of '" + this.getClass().getName()
151 + ".columnSizePercentage' should be 100% (but was " + total + ")");
152 }
153
154 this.columnClasses = new Class[] {
155 String.class, Long.class, String.class, Date.class, Boolean.class, Boolean.class
156 };
157 }
158
159
160
161
162
163
164
165 public FileData contains(String absolutePath) {
166 FileData foundFileData = null;
167
168 for (FileData fd : rows) {
169 if (absolutePath.equals(fd.getAbsolutePath())) {
170 foundFileData = fd;
171 break;
172 }
173 }
174
175 return foundFileData;
176 }
177
178
179
180
181
182
183
184
185 public FileData addFile(File file) throws JUploadExceptionStopAddingFiles {
186 synchronized (this.rows) {
187 FileData foundFileData = contains(file.getAbsolutePath());
188 if (foundFileData != null) {
189 this.uploadPolicy.displayWarn("File " + file.getName() + " already exists");
190 return foundFileData;
191 } else {
192
193
194
195
196
197
198 FileData fd = this.uploadPolicy.createFileData(file);
199 if (fd != null) {
200
201 this.rows.add(fd);
202
203
204
205 }
206 return fd;
207 }
208 }
209 }
210
211
212
213
214
215
216
217 public FileData getFileDataAt(int row) {
218 if (row >= 0) {
219 try {
220 return this.rows.get(row);
221 } catch (ArrayIndexOutOfBoundsException e) {
222
223
224 this.uploadPolicy.displayWarn(e.getClass().getName() + " in FilePanelDataModel2.getFileDataAt(" + row
225 + ")");
226 }
227 }
228 return null;
229 }
230
231
232
233
234
235
236 public void removeRow(int row) {
237 this.rows.remove(row);
238 fireTableDataChanged();
239 }
240
241
242
243
244
245
246
247 public int getRow(FileData fileData) {
248 synchronized (this.rows) {
249 for (int i = 0; i < this.rows.size(); i += 1) {
250 if (rows.get(i).getAbsolutePath().equals(fileData.getAbsolutePath())) {
251 return i;
252 }
253 }
254 return -1;
255 }
256 }
257
258
259 public int getColumnCount() {
260 return this.columnNames.length;
261 }
262
263
264 public int getRowCount() {
265 return this.rows.size();
266 }
267
268
269
270
271
272
273 @Override
274 public boolean isCellEditable(int arg0, int arg1) {
275 return columnEditable[arg1];
276 }
277
278
279
280
281
282
283
284 public void sortColumn(int col, boolean ascending) {
285 synchronized (this.rows) {
286 Collections.sort(this.rows, new ColumnComparator(col, ascending));
287 }
288 fireTableDataChanged();
289 }
290
291
292
293
294
295
296
297 public boolean isSortable(int col) {
298 return (Boolean.class != getColumnClass(col));
299 }
300
301
302
303
304 @Override
305 public Class<?> getColumnClass(int arg0) {
306 return this.columnClasses[arg0];
307 }
308
309
310
311
312 public Object getValueAt(int row, int col) {
313 FileData fileData = getFileDataAt(row);
314 if (fileData != null) {
315 String colName = getColumnName(col);
316
317
318 if (colName.equals(this.COL_NAME)) {
319 return fileData.getFileName();
320 } else if (colName.equals(this.COL_SIZE)) {
321 return Long.valueOf(fileData.getFileLength());
322 } else if (colName.equals(this.COL_DIRECTORY)) {
323 return fileData.getDirectory();
324 } else if (colName.equals(this.COL_MODIFIED)) {
325 return fileData.getLastModified();
326 } else if (colName.equals(this.COL_CHECKED)) {
327 return fileData.getUploadFlag();
328 } else {
329 this.uploadPolicy.displayErr("Unknown column in " + this.getClass().getName() + ": " + colName);
330 return null;
331 }
332 } else {
333 return null;
334 }
335 }
336
337
338
339
340
341
342 @Override
343 public void setValueAt(Object aValue, int row, int col) {
344 if (!columnEditable[col]) {
345 this.uploadPolicy.displayWarn(this.getClass().getName() + ".setValueAt: no action");
346 } else {
347 FileData fd = getFileDataAt(row);
348 if (!(aValue instanceof Boolean)) {
349 this.uploadPolicy.displayErr("Internal error in " + this.getClass().getName()
350 + ": o should be a Boolean but is a " + aValue.getClass().getName());
351 } else {
352 fd.setUploadFlag((Boolean) aValue);
353 }
354 }
355 }
356
357
358
359
360 @Override
361 public String getColumnName(int arg0) {
362 return this.columnNames[arg0];
363 }
364
365
366
367
368
369
370
371 public int getColumnSizePercentage(int col) {
372 return this.columnSizePercentage[col];
373 }
374
375
376
377
378
379
380
381 public File getFileRoot() {
382 return DefaultFileData.getRoot(rows);
383 }
384
385 public List<FileData> getFiles() {
386 return rows;
387 }
388 }