View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package no.sintef.file;
22  
23  import java.io.File;
24  import java.util.HashMap;
25  import java.util.StringTokenizer;
26  
27  import javax.swing.filechooser.FileFilter;
28  
29  /***
30   * FileFilter implementation which takes a list of file suffixes.
31   * 
32   * @author Fredrik Vraalsen
33   */
34  public class FileSuffixFilter extends FileFilter {
35  	
36  	private final HashMap suffixes = new HashMap();
37  	private String description;
38  	
39  	/***
40  	 * @param description filter description
41  	 * @param suffixList list of suffixes to filter on
42  	 */
43  	public FileSuffixFilter(String description, String suffixList) {
44  		super();
45  		this.description = description;
46  		if (suffixList == null) {
47  			return;
48  		}
49  		StringTokenizer tokenizer = new StringTokenizer(suffixList, ",");
50  		boolean first = true;
51  		while (tokenizer.hasMoreTokens()) {
52  			String suffix = tokenizer.nextToken().trim().toLowerCase();
53  			suffixes.put(suffix, suffix);
54  			description += (first ? "" : ", ") + "*." + suffix;
55  			first = false;
56  		}
57  	}
58  
59  	/***
60  	 * Check whether filter accepts file.
61  	 * 
62  	 * @param file
63  	 *            file to check
64  	 * @return true if filter accepts file, false otherwise
65  	 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
66  	 */
67  	public boolean accept(File file) {
68  		if (file != null && file.isDirectory()) {
69  			return true;
70  		}
71  		String suffix = getSuffix(file != null ? file.getName() : null);
72  		return suffixes.get(suffix) != null;
73  	}
74  	
75  	/***
76  	 * Get filter description.
77  	 * 
78  	 * @return filter description
79  	 * @see javax.swing.filechooser.FileFilter#getDescription()
80  	 */
81  	public String getDescription() {
82  		return description;
83  	}
84  	
85  	/***
86  	 * Extract suffix from filename.
87  	 * 
88  	 * @param filename
89  	 *            the filename
90  	 * @return the filename suffix
91  	 */
92  	public static final String getSuffix(String filename) {
93  		if (filename == null) {
94  			return null;
95  		}
96  		int index = filename.lastIndexOf('.');
97  		if (index == -1 || index == filename.length() - 1) {
98  			return null;
99  		}
100 		return filename.substring(index + 1);
101 	}
102 	
103 	/***
104 	 * Extract prefix from filename.
105 	 * 
106 	 * @param filename
107 	 *            the filename
108 	 * @return the filename prefix
109 	 */
110 	public static final String getPrefix(String filename) {
111 		if (filename == null) {
112 			return null;
113 		}
114 		int index = filename.lastIndexOf('.');
115 		if (index == -1) {
116 			return filename;
117 		}
118 		return filename.substring(0, index);
119 	}
120 	
121 }