1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package wjhk.jupload2.gui.filepanel;
21
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.Component;
25 import java.awt.Font;
26 import java.awt.Point;
27 import java.awt.event.ComponentEvent;
28 import java.awt.event.ComponentListener;
29 import java.io.File;
30 import java.util.List;
31
32 import javax.swing.JPanel;
33 import javax.swing.JScrollPane;
34 import javax.swing.table.TableColumnModel;
35
36 import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles;
37 import wjhk.jupload2.filedata.FileData;
38 import wjhk.jupload2.gui.JUploadPanel;
39 import wjhk.jupload2.gui.filepanel.treeview.FolderNode;
40 import wjhk.jupload2.gui.filepanel.treeview.FileDataTreeViewModel;
41 import wjhk.jupload2.gui.filepanel.treeview.MyTreeTable;
42 import wjhk.jupload2.gui.filepanel.treeview.MyTreeTableModel;
43 import wjhk.jupload2.gui.filepanel.treeview.TreeFileDataNode;
44 import wjhk.jupload2.policies.UploadPolicy;
45
46
47
48
49
50
51
52
53
54
55
56 public class FilePanelTableImp extends JPanel implements FilePanel, ComponentListener {
57
58
59 static final long serialVersionUID = -8273990467324350526L;
60
61
62 JUploadPanel juploadPanel = null;
63
64
65 FilePanelJFlatTable flatTable;
66
67
68 FilePanelFlatDataModel2 flatModel;
69
70
71 MyTreeTable treeTable;
72
73
74 MyTreeTableModel<TreeFileDataNode> treeModel;
75
76
77 JScrollPane flatScrollPane = null;
78
79
80 JScrollPane treeScrollPane = null;
81
82
83 FileListViewMode fileListViewMode = FileListViewMode.FLAT;
84
85
86 UploadPolicy uploadPolicy = null;
87
88
89
90
91
92
93
94 public FilePanelTableImp(JUploadPanel juploadPanel, UploadPolicy uploadPolicy) {
95 this.juploadPanel = juploadPanel;
96 this.uploadPolicy = uploadPolicy;
97
98 setLayout(new BorderLayout());
99 addMouseListener(juploadPanel.getMouseListener());
100 setTransferHandler(juploadPanel.getTransferHandler());
101
102
103 this.flatTable = new FilePanelJFlatTable(juploadPanel, uploadPolicy);
104 this.flatModel = new FilePanelFlatDataModel2(uploadPolicy);
105 this.flatTable.setModel(this.flatModel);
106 this.flatScrollPane = new JScrollPane(this.flatTable);
107 this.flatScrollPane.addMouseListener(juploadPanel.getMouseListener());
108
109 this.flatScrollPane.getViewport().addComponentListener(this);
110
111
112 treeModel = new FileDataTreeViewModel(uploadPolicy, this.flatModel);
113 treeTable = new MyTreeTable(treeModel);
114 treeModel.setTree(treeTable.getTree());
115 this.treeScrollPane = new JScrollPane(this.treeTable);
116 this.treeScrollPane.addMouseListener(juploadPanel.getMouseListener());
117
118 this.treeScrollPane.getViewport().addComponentListener(this);
119
120
121 this.fileListViewMode = this.uploadPolicy.getFileListViewMode();
122 switch (this.fileListViewMode) {
123 case FLAT:
124 add(this.flatScrollPane, BorderLayout.CENTER);
125 break;
126 case TREE_VIEW:
127 case INDEPENDENT_TREE_VIEW:
128 add(this.treeScrollPane, BorderLayout.CENTER);
129 break;
130 }
131 }
132
133
134 public FileListViewMode getFileListMode() {
135 return this.fileListViewMode;
136 }
137
138
139 public void setFileListViewMode(FileListViewMode fileListViewMode) {
140 if (this.fileListViewMode != fileListViewMode) {
141 switch (fileListViewMode) {
142 case FLAT:
143 remove(this.treeScrollPane);
144 add(this.flatScrollPane, BorderLayout.CENTER);
145
146
147 break;
148 case TREE_VIEW:
149 remove(this.flatScrollPane);
150 add(this.treeScrollPane, BorderLayout.CENTER);
151
152
153 break;
154 default:
155 IllegalArgumentException e = new IllegalArgumentException("Unknown value for fileListMode:"
156 + fileListViewMode.toString());
157 uploadPolicy.displayErr(e);
158 throw e;
159 }
160 this.fileListViewMode = fileListViewMode;
161 this.uploadPolicy.setFileListViewMode(fileListViewMode);
162 }
163 }
164
165
166
167
168 public final void addFiles(File[] filesToAdd) {
169
170 long startTime = System.currentTimeMillis();
171 int nbFiles = 0;
172
173 if (null == filesToAdd) {
174 String msg = "FilePanelTableImpl: filesToUpload may not be null)";
175 uploadPolicy.displayErr(msg);
176 throw new java.lang.IllegalArgumentException(msg);
177 } else {
178 try {
179 for (int i = 0; i < filesToAdd.length; i++) {
180 nbFiles += treeModel.attachObject(filesToAdd[i]);
181 }
182 } catch (JUploadExceptionStopAddingFiles e) {
183 this.uploadPolicy.displayWarn(getClass().getName() + ".addFiles() [" + e.getClass().getName() + "]: "
184 + e.getMessage());
185 } catch (Exception e) {
186 this.uploadPolicy.displayErr("Unexpected error during file adding: " + getClass().getName()
187 + ".addFiles() [" + e.getClass().getName() + "]: " + e.getMessage());
188 }
189 }
190 this.juploadPanel.updateButtonState();
191
192
193 this.flatModel.fireTableDataChanged();
194
195
196
197 if (this.uploadPolicy.getFileListViewMode().equals(FileListViewMode.FLAT)
198 || this.uploadPolicy.getFileListViewMode().equals(FileListViewMode.TREE_VIEW)) {
199 FolderNode visibleRoot = (FolderNode) treeModel.getTreePathForObject(flatModel.getFileRoot())
200 .getLastPathComponent();
201 if (visibleRoot == null) {
202 uploadPolicy.displayErr("[Internal Error] Folder Node not found for folder "
203 + flatModel.getFileRoot().getAbsolutePath());
204 } else {
205 treeModel.setRoot(visibleRoot);
206 }
207 } else {
208
209 reload();
210 }
211
212
213 long finishTime = System.currentTimeMillis();
214 uploadPolicy.displayInfo("Added " + nbFiles + " files in " + ((finishTime - startTime) / 1000) + " seconds");
215 }
216
217
218
219
220 public final List<FileData> getFiles() {
221 return this.flatModel.getFiles();
222 }
223
224
225
226
227 public final int getFilesLength() {
228 return this.flatTable.getRowCount();
229 }
230
231
232
233
234 public final void removeSelected() {
235 synchronized (this.flatModel.getFiles()) {
236 int[] rows = this.flatTable.getSelectedRows();
237 for (int i = rows.length - 1; 0 <= i; i--) {
238 removeRow(rows[i], null);
239 }
240 }
241 }
242
243
244
245
246 @Override
247 public final void removeAll() {
248 synchronized (this.flatModel.getFiles()) {
249 for (int i = getFilesLength() - 1; 0 <= i; i--) {
250 removeRow(i, null);
251 }
252 }
253 }
254
255
256 public void remove(FileData[] files) {
257 for (FileData fd : files) {
258 removeRow(null, fd);
259 }
260 this.treeModel.reload();
261 }
262
263
264 public void removeFileNotToUpload() {
265
266
267
268 int i = 0;
269 FileData fd;
270 List<FileData> files = this.flatModel.getFiles();
271 while (i < files.size()) {
272 fd = files.get(i);
273 if (fd.getUploadFlag()) {
274
275 i += 1;
276 } else {
277
278
279 removeRow(i, fd);
280 }
281 }
282 }
283
284
285
286
287
288
289 public final void remove(FileData fileData) {
290 removeRow(null, fileData);
291 }
292
293
294
295
296
297
298
299
300
301 final void removeRow(Integer rowNumberParam, FileData fileDataParam) {
302 synchronized (this.flatModel.getFiles()) {
303 if (rowNumberParam == null && fileDataParam == null) {
304 uploadPolicy
305 .displayErr("rowNumberParam and fileDataParam may not be both null (in FilePanelTableImpl.removeRow(Integer,FileData)");
306 }
307
308 Integer rowNumber = rowNumberParam;
309 FileData fileData = fileDataParam;
310 if (rowNumber == null) {
311 rowNumber = this.flatModel.getRow(fileDataParam);
312 } else if (fileData == null) {
313 fileData = this.flatModel.getFileDataAt(rowNumber);
314 }
315
316
317 if (rowNumber == null || rowNumber < 0) {
318 uploadPolicy.displayWarn("The row " + rowNumber
319 + " doesn't exist (in FilePanelTableImpl.removeRow(Integer,FileData)");
320 } else {
321 this.flatModel.removeRow(rowNumber);
322 }
323
324
325 if (fileData == null) {
326 uploadPolicy.displayWarn("The fileData for " + rowNumber
327 + " doesn't exist (in FilePanelTableImpl.removeRow(Integer,FileData)");
328 } else {
329
330 try {
331 this.treeModel.remove(fileData.getTreeFileDataNode());
332 } catch (Exception e) {
333 uploadPolicy.displayErr(e);
334 }
335
336
337
338
339
340
341
342
343
344
345 }
346 }
347 }
348
349
350
351
352 public final void clearSelection() {
353 this.flatTable.clearSelection();
354 }
355
356
357 public final void focusTable() {
358 if (0 < this.flatTable.getRowCount()) {
359 this.flatTable.requestFocus();
360 }
361 }
362
363
364 public FileData getFileDataAt(Point point) {
365 int row = this.flatTable.rowAtPoint(point);
366 return this.flatModel.getFileDataAt(row);
367 }
368
369
370
371
372
373
374
375 public Component getDropComponent() {
376 return this;
377 }
378
379
380
381
382 public void componentHidden(ComponentEvent arg0) {
383
384 }
385
386
387
388
389 public void componentMoved(ComponentEvent arg0) {
390
391 }
392
393
394
395
396 public void componentResized(ComponentEvent arg0) {
397
398 if (getWidth() > 0) {
399
400 TableColumnModel flatColumnModel = this.flatTable.getColumnModel();
401 for (int i = 0; i < this.flatModel.getColumnCount(); i++) {
402 flatColumnModel.getColumn(i)
403 .setPreferredWidth(
404 (this.flatModel.getColumnSizePercentage(i) * this.flatScrollPane.getViewport()
405 .getWidth()) / 100);
406 }
407
408
409 TableColumnModel treeviewColumnModel = this.treeTable.getColumnModel();
410 for (int i = 0; i < this.treeModel.getColumnCount(); i++) {
411 treeviewColumnModel.getColumn(i)
412 .setPreferredWidth(
413 (this.treeModel.getColumnSizePercentage(i) * this.treeScrollPane.getViewport()
414 .getWidth()) / 100);
415 }
416 }
417 }
418
419
420
421
422 public void componentShown(ComponentEvent arg0) {
423
424 }
425
426
427
428
429
430
431 public void setGridBorderColor(Color color) {
432 this.flatTable.setGridColor(color);
433 }
434
435
436
437
438
439
440 public void setTableHeaderBackColor(Color color) {
441 this.flatTable.getTableHeader().setBackground(color);
442 }
443
444
445
446
447
448
449 public void setTableHeaderFont(Font font) {
450 this.flatTable.getTableHeader().setFont(font);
451 }
452
453
454
455
456
457
458 public void setTableHeaderTextColor(Color color) {
459 this.flatTable.getTableHeader().setForeground(color);
460 }
461
462
463 public void reload() {
464 this.treeModel.reload();
465 }
466
467
468 public void cleanHierarchy() {
469 this.treeModel.cleanHierarchy();
470 reload();
471 }
472
473 }