1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.jguiraffe.examples.tutorial.viewset;
17
18 import java.io.File;
19 import java.io.FileFilter;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Comparator;
30 import java.util.Date;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Locale;
34 import java.util.Set;
35
36 import net.sf.jguiraffe.examples.tutorial.model.FileData;
37 import net.sf.jguiraffe.gui.builder.components.Color;
38 import net.sf.jguiraffe.gui.builder.components.ColorHelper;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class ViewSettings implements Serializable
59 {
60
61 public static final String VIEW_SETTINGS_FILE = ".viewsettings";
62
63
64
65
66 public static final String CTX_NAME = "viewSettingsModel";
67
68
69
70
71 private static final long serialVersionUID = 20100111L;
72
73
74 private static final Log LOG = LogFactory.getLog(ViewSettings.class);
75
76
77 private Color backgroundColor;
78
79
80 private Color foregroundColor;
81
82
83 private Color selectionBackground;
84
85
86 private Color selectionForeground;
87
88
89 private Integer sortColumn;
90
91
92 private Integer sortDirectories;
93
94
95 private boolean sortDescending;
96
97
98 private boolean filterTypes;
99
100
101 private String[] fileTypes;
102
103
104 private boolean filterSize;
105
106
107 private Integer minFileSize;
108
109
110 private boolean filterDate;
111
112
113 private Date fileDateFrom;
114
115
116 private Date fileDateTo;
117
118
119
120
121
122 public ViewSettings()
123 {
124 initDefaults();
125 }
126
127
128
129
130
131
132
133
134 public void save(File directory) throws IOException
135 {
136 File settingsFile = new File(directory, VIEW_SETTINGS_FILE);
137 ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(
138 settingsFile));
139
140 try
141 {
142 os.writeObject(this);
143 LOG.info("Saved settings file: " + settingsFile);
144 }
145 finally
146 {
147 os.close();
148 }
149 }
150
151
152
153
154
155
156
157
158
159
160 public static ViewSettings forDirectory(File directory)
161 {
162 File settingsFile = new File(directory, VIEW_SETTINGS_FILE);
163
164 if (settingsFile.exists())
165 {
166 LOG.info("Trying to load view settings from " + settingsFile);
167 ObjectInputStream is = null;
168 try
169 {
170 is = new ObjectInputStream(new FileInputStream(settingsFile));
171 return (ViewSettings) is.readObject();
172 }
173 catch (IOException ioex)
174 {
175 LOG.error("Error when reading view settings!", ioex);
176 }
177 catch (ClassNotFoundException cfex)
178 {
179 LOG.error("Class not found when reading view settings!", cfex);
180 }
181 finally
182 {
183 if (is != null)
184 {
185 try
186 {
187 is.close();
188 }
189 catch (IOException ioex)
190 {
191 LOG.warn("Error when closing stream.", ioex);
192 }
193 }
194 }
195 }
196
197 return new ViewSettings();
198 }
199
200 public Color getBackgroundColor()
201 {
202 return backgroundColor;
203 }
204
205 public void setBackgroundColor(Color backgroundColor)
206 {
207 this.backgroundColor = backgroundColor;
208 }
209
210 public Color getForegroundColor()
211 {
212 return foregroundColor;
213 }
214
215 public void setForegroundColor(Color foregroundColor)
216 {
217 this.foregroundColor = foregroundColor;
218 }
219
220 public Color getSelectionBackground()
221 {
222 return selectionBackground;
223 }
224
225 public void setSelectionBackground(Color selectionBackground)
226 {
227 this.selectionBackground = selectionBackground;
228 }
229
230 public Color getSelectionForeground()
231 {
232 return selectionForeground;
233 }
234
235 public void setSelectionForeground(Color selectionForeground)
236 {
237 this.selectionForeground = selectionForeground;
238 }
239
240 public Integer getSortColumn()
241 {
242 return sortColumn;
243 }
244
245 public void setSortColumn(Integer sortColumn)
246 {
247 this.sortColumn = sortColumn;
248 }
249
250 public Integer getSortDirectories()
251 {
252 return sortDirectories;
253 }
254
255 public void setSortDirectories(Integer sortDirectories)
256 {
257 this.sortDirectories = sortDirectories;
258 }
259
260 public boolean isSortDescending()
261 {
262 return sortDescending;
263 }
264
265 public void setSortDescending(boolean sortDescending)
266 {
267 this.sortDescending = sortDescending;
268 }
269
270 public boolean isFilterTypes()
271 {
272 return filterTypes;
273 }
274
275 public void setFilterTypes(boolean filterTypes)
276 {
277 this.filterTypes = filterTypes;
278 }
279
280 public String[] getFileTypes()
281 {
282 return fileTypes;
283 }
284
285 public void setFileTypes(String[] fileTypes)
286 {
287 this.fileTypes = fileTypes;
288 }
289
290 public boolean isFilterSize()
291 {
292 return filterSize;
293 }
294
295 public void setFilterSize(boolean filterSize)
296 {
297 this.filterSize = filterSize;
298 }
299
300 public Integer getMinFileSize()
301 {
302 return minFileSize;
303 }
304
305 public void setMinFileSize(Integer minFileSize)
306 {
307 this.minFileSize = minFileSize;
308 }
309
310 public boolean isFilterDate()
311 {
312 return filterDate;
313 }
314
315 public void setFilterDate(boolean filterDate)
316 {
317 this.filterDate = filterDate;
318 }
319
320 public Date getFileDateFrom()
321 {
322 return fileDateFrom;
323 }
324
325 public void setFileDateFrom(Date fileDateFrom)
326 {
327 this.fileDateFrom = fileDateFrom;
328 }
329
330 public Date getFileDateTo()
331 {
332 return fileDateTo;
333 }
334
335 public void setFileDateTo(Date fileDateTo)
336 {
337 this.fileDateTo = fileDateTo;
338 }
339
340
341
342
343
344
345
346 public Comparator<FileData> createComparator()
347 {
348 int sortCol = (getSortColumn() == null) ? 0 : getSortColumn()
349 .intValue();
350 Comparator<FileData> fileComp;
351 switch (sortCol)
352 {
353 case 2:
354 fileComp = new FileSizeComparator();
355 break;
356 case 1:
357 fileComp = new FileDateComparator();
358 break;
359 default:
360 fileComp = new FileNameComparator();
361 }
362
363 int dirComp = (getSortDirectories() == null) ? 0 : getSortDirectories()
364 .intValue();
365 return new FileDataComparator(new FileDirComparator(dirComp), fileComp,
366 isSortDescending());
367 }
368
369
370
371
372
373
374
375
376 public FileFilter createFileFilter()
377 {
378 List<FileFilter> filters = new ArrayList<FileFilter>(3);
379
380 if (isFilterTypes())
381 {
382 filters.add(new FileTypeFilter(new HashSet<String>(Arrays
383 .asList(fileTypes))));
384 }
385 if (isFilterDate())
386 {
387 filters.add(new FileDateFilter(fileDateFrom, fileDateTo));
388 }
389 if (isFilterSize())
390 {
391 filters.add(new FileSizeFilter(minFileSize));
392 }
393
394 return new CombinedFileFilter(filters);
395 }
396
397
398
399
400
401
402 private void initDefaults()
403 {
404 setBackgroundColor(ColorHelper.NamedColor.WHITE.getColor());
405 setForegroundColor(ColorHelper.NamedColor.BLACK.getColor());
406 setSelectionBackground(ColorHelper.NamedColor.BLUE.getColor());
407 setSelectionForeground(ColorHelper.NamedColor.BLACK.getColor());
408 setSortColumn(0);
409 setSortDirectories(null);
410 setSortDescending(false);
411 setMinFileSize(1);
412 }
413
414
415
416
417
418 private static class FileNameComparator implements Comparator<FileData>
419 {
420 @Override
421 public int compare(FileData o1, FileData o2)
422 {
423 return o1.getName().compareToIgnoreCase(o2.getName());
424 }
425 }
426
427
428
429
430
431 private static class FileSizeComparator implements Comparator<FileData>
432 {
433 @Override
434 public int compare(FileData o1, FileData o2)
435 {
436 return (int) (o1.getSize() - o2.getSize());
437 }
438 }
439
440
441
442
443
444 private static class FileDateComparator implements Comparator<FileData>
445 {
446 @Override
447 public int compare(FileData o1, FileData o2)
448 {
449 return o1.getLastModified().compareTo(o2.getLastModified());
450 }
451 }
452
453
454
455
456
457 private static class FileDirComparator implements Comparator<FileData>
458 {
459
460 private final int directorySortOrder;
461
462 public FileDirComparator(int dirSortOrder)
463 {
464 directorySortOrder = dirSortOrder;
465 }
466
467 @Override
468 public int compare(FileData o1, FileData o2)
469 {
470 if (directorySortOrder == 2)
471 {
472
473 return 0;
474 }
475
476 boolean dir1 = o1.getFile().isDirectory();
477 boolean dir2 = o2.getFile().isDirectory();
478 if (dir1 ^ dir2)
479 {
480
481 int result = (directorySortOrder == 0) ? -1 : 1;
482 if (dir2)
483 {
484 result = -result;
485 }
486 return result;
487 }
488 else
489 {
490 return 0;
491 }
492 }
493 }
494
495
496
497
498
499
500 private static class FileDataComparator implements Comparator<FileData>
501 {
502
503 private final Comparator<FileData> fileDirComparator;
504
505
506 private final Comparator<FileData> fileComparator;
507
508
509 private final boolean reverseOrder;
510
511 public FileDataComparator(Comparator<FileData> dirComp,
512 Comparator<FileData> fileComp, boolean reverse)
513 {
514 fileDirComparator = dirComp;
515 fileComparator = fileComp;
516 reverseOrder = reverse;
517 }
518
519 @Override
520 public int compare(FileData o1, FileData o2)
521 {
522 int c = fileDirComparator.compare(o1, o2);
523 if (c != 0)
524 {
525 return c;
526 }
527
528 c = fileComparator.compare(o1, o2);
529 if (reverseOrder)
530 {
531 c = -c;
532 }
533
534 return c;
535 }
536 }
537
538
539
540
541
542 private static class FileDateFilter implements FileFilter
543 {
544
545 private final Date dtFrom;
546
547
548 private final Date dtTo;
549
550 public FileDateFilter(Date from, Date to)
551 {
552 dtFrom = from;
553 dtTo = to;
554 }
555
556 @Override
557 public boolean accept(File file)
558 {
559 Date fileDate = new Date(file.lastModified());
560 return (dtFrom == null || dtFrom.before(fileDate) || dtFrom
561 .equals(fileDate))
562 && (dtTo == null || dtTo.after(fileDate));
563 }
564 }
565
566
567
568
569
570 private static class FileSizeFilter implements FileFilter
571 {
572
573 private final long minimumSize;
574
575
576
577
578
579
580
581 public FileSizeFilter(Integer minSize)
582 {
583 minimumSize = 1024L * minSize.longValue();
584 }
585
586 @Override
587 public boolean accept(File file)
588 {
589 return file.length() >= minimumSize;
590 }
591 }
592
593
594
595
596
597
598 private static class FileTypeFilter implements FileFilter
599 {
600
601 private final Set<String> extensions;
602
603
604
605
606
607
608
609 public FileTypeFilter(Set<String> exts)
610 {
611 extensions = exts;
612 }
613
614 @Override
615 public boolean accept(File file)
616 {
617 int extpos = file.getName().lastIndexOf('.');
618 if (extpos < 0)
619 {
620
621 return false;
622 }
623
624 String ext = file.getName().substring(extpos + 1).toLowerCase(
625 Locale.ENGLISH);
626 return extensions.contains(ext);
627 }
628 }
629
630
631
632
633
634
635
636 private static class CombinedFileFilter implements FileFilter
637 {
638
639 private final Collection<FileFilter> subFilters;
640
641
642
643
644
645
646
647 public CombinedFileFilter(Collection<FileFilter> subs)
648 {
649 subFilters = subs;
650 }
651
652 @Override
653 public boolean accept(File file)
654 {
655 for (FileFilter filter : subFilters)
656 {
657 if (!filter.accept(file))
658 {
659 return false;
660 }
661 }
662
663 return true;
664 }
665 }
666 }