View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
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
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package ui.util;
21  
22  import java.io.File;
23  import java.io.FilenameFilter;
24  
25  import javax.swing.filechooser.FileFilter;
26  
27  public class FileExtensionFilter extends FileFilter implements FilenameFilter {
28  
29  	/***
30  	 * Comment for <code>extension</code> The file's extension.
31  	 */
32  	private final String extension;
33  
34  	/***
35  	 * Comment for <code>description</code> The extension's descriptor, eg.
36  	 * "xml" = eXtensible MarkUp Language.
37  	 */
38  	private String description = ""; //$NON-NLS-1$
39  
40  	/***
41  	 * Comment for <code>DOT</code> It stores the static value for the dot:
42  	 * ".".
43  	 */
44  	private static String DOT = "."; //$NON-NLS-1$
45  
46  	/***
47  	 * @param extension
48  	 *            The file's extension;
49  	 */
50  	public FileExtensionFilter(String extension) {
51  		this.extension = extension.toLowerCase();
52  	}
53  
54  	/***
55  	 * @param extension
56  	 *            The file's extension;
57  	 * @param description
58  	 *            The file extension's description.
59  	 */
60  	public FileExtensionFilter(String extension, String description) {
61  		this.extension = extension.toLowerCase();
62  		this.description = description;
63  	}
64  
65  	/*
66  	 * (non-Javadoc)
67  	 * 
68  	 * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
69  	 */
70  	public boolean accept(File file, String extension) {
71  		return extension.endsWith(this.extension.toLowerCase())
72  				|| extension.endsWith(this.extension.toUpperCase());
73  	}
74  
75  	public boolean validExtension(String extension) {
76  		return extension.startsWith(DOT)
77  				&& (extension.endsWith(this.extension.toLowerCase()) || extension
78  						.endsWith(this.extension.toUpperCase()));
79  	}
80  
81  	/***
82  	 * @return Returns the extension.
83  	 */
84  	public String getExtension() {
85  		return extension;
86  	}
87  
88  	/***
89  	 * This method just adds a DOT to the extension and returns it.
90  	 * 
91  	 * @return Returns the dotted extension, eg. ".jpeg" or ".xml".
92  	 */
93  	public String getDottedExtension() {
94  		return DOT + extension;
95  	}
96  
97  	@Override
98  	public boolean accept(File file) {
99  		if (file.isDirectory())
100 			return true;
101 		if (!file.getName().contains(DOT))
102 			return false;
103 		int dotIndex = file.getName().indexOf(DOT);
104 		int length = file.getName().length();
105 		return validExtension(file.getName().substring(dotIndex, length));
106 	}
107 
108 	@Override
109 	public String getDescription() {
110 		return description.equals("") ? extension.toUpperCase() : description; //$NON-NLS-1$
111 	}
112 
113 	/***
114 	 * @param description
115 	 *            The description to set.
116 	 */
117 	public void setDescription(String description) {
118 		this.description = description;
119 	}
120 
121 }