01: /* Copyright (C) 2003 Finalist IT Group
02: *
03: * This file is part of JAG - the Java J2EE Application Generator
04: *
05: * JAG is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU General Public License as published by
07: * the Free Software Foundation; either version 2 of the License, or
08: * (at your option) any later version.
09: * JAG is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: * You should have received a copy of the GNU General Public License
14: * along with JAG; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16: */
17:
18: package com.finalist.jaggenerator;
19:
20: import javax.swing.filechooser.*;
21: import java.io.File;
22: import java.util.ArrayList;
23: import java.util.Arrays;
24: import java.util.Iterator;
25:
26: /**
27: * A FileFilter that accepts/rejects files depending on their extension (part of filename following the last 'dot').
28: *
29: * @author Michael O'Connor - Finalist IT Group
30: */
31: public class ExtensionsFileFilter extends FileFilter {
32:
33: private static final char DOT = '.';
34: private final ArrayList acceptFilter = new ArrayList();
35: private String description;
36:
37: /**
38: * Creates a filter with one extension.
39: * @param acceptableExtension
40: */
41: public ExtensionsFileFilter(String acceptableExtension) {
42: acceptFilter.add(acceptableExtension);
43: description = "*." + acceptableExtension;
44: }
45:
46: /**
47: * Creates a filter with possibly more than one extension.
48: * @param acceptableExtensions
49: */
50: public ExtensionsFileFilter(String[] acceptableExtensions) {
51: acceptFilter.addAll(Arrays.asList(acceptableExtensions));
52:
53: StringBuffer sb = new StringBuffer();
54: Iterator i = acceptFilter.iterator();
55: while (i.hasNext()) {
56: String extension = (String) i.next();
57: sb.append("*.");
58: sb.append(extension);
59: if (i.hasNext()) {
60: sb.append(", ");
61: }
62: }
63: description = sb.toString();
64: }
65:
66: /** @see {@link FileFilter#accept}. */
67: public boolean accept(File file) {
68: if (file.isDirectory())
69: return true;
70: String filename = file.toString().toLowerCase();
71: int lastDotPos = filename.lastIndexOf(DOT) + 1;
72: if (lastDotPos != 0) {
73: String extension = filename.substring(lastDotPos);
74: return acceptFilter.contains(extension);
75: }
76:
77: return false; //maybe this should be true..? if a file has no extension, we don't know what it is...
78: }
79:
80: /** @see {@link FileFilter#getDescription}. */
81: public String getDescription() {
82: return description;
83: }
84: }
|