01: /*
02: * Copyright (C) 2005 Jeff Tassin
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library 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 GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package com.jeta.swingbuilder.gui.filechooser;
20:
21: import java.io.File;
22: import java.util.Iterator;
23: import java.util.LinkedList;
24: import java.util.StringTokenizer;
25:
26: import javax.swing.filechooser.FileFilter;
27:
28: public class TSFileFilter extends FileFilter {
29: /**
30: * A list of file extensions for this filter
31: */
32: private LinkedList m_exts = new LinkedList();
33:
34: /**
35: * The description for this filter
36: */
37: private String m_description;
38:
39: /**
40: * A comma separated list of file extensions without the . e.g.
41: * "txt,gif,jpeg"
42: */
43: public TSFileFilter(String ext_tokens, String desc) {
44: try {
45: StringTokenizer st = new StringTokenizer(ext_tokens, ",");
46: while (st.hasMoreTokens()) {
47: String token = st.nextToken();
48: m_exts.add(token);
49: }
50:
51: m_description = desc;
52: } catch (Exception e) {
53: e.printStackTrace();
54: }
55: }
56:
57: public static String getExtension(File f) {
58: String ext = null;
59: String s = f.getName();
60: int i = s.lastIndexOf('.');
61:
62: if (i > 0 && i < s.length() - 1) {
63: ext = s.substring(i + 1).toLowerCase();
64: }
65: return ext;
66: }
67:
68: public boolean accept(File f) {
69: if (f.isDirectory()) {
70: return true;
71: }
72:
73: if (m_exts.size() == 0)
74: return true;
75:
76: String extension = getExtension(f);
77: if (extension != null) {
78: Iterator iter = m_exts.iterator();
79: while (iter.hasNext()) {
80: if (extension.equals(iter.next()))
81: return true;
82: }
83: }
84: return false;
85: }
86:
87: /**
88: * The description for this filter
89: */
90: public String getDescription() {
91: return m_description;
92: }
93: }
|