Source code: com/barteo/emulator/app/util/ExtensionFileFilter.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001-2003 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package com.barteo.emulator.app.util;
21
22 import java.io.File;
23 import java.io.FilenameFilter;
24 import java.util.Hashtable;
25
26
27 public class ExtensionFileFilter implements FilenameFilter
28 {
29
30 String description;
31
32 Hashtable extensions = new Hashtable();
33
34
35 public ExtensionFileFilter(String description)
36 {
37 this.description = description;
38 }
39
40
41 public boolean accept(File dir, String name)
42 {
43 if(dir != null) {
44 if(dir.isDirectory()) {
45 return true;
46 }
47 String ext = getExtension(dir);
48 if(ext != null && extensions.get(ext) != null) {
49 return true;
50 }
51 }
52
53 return false;
54 }
55
56
57 public void addExtension(String extension)
58 {
59 extensions.put(extension.toLowerCase(), this);
60 }
61
62
63 public String getDescription()
64 {
65 return description;
66 }
67
68
69 String getExtension(File file)
70 {
71 if (file != null) {
72 String filename = file.getName();
73 int i = filename.lastIndexOf('.');
74 if (i > 0 && i < filename.length() - 1) {
75 return filename.substring(i + 1).toLowerCase();
76 }
77 }
78
79 return null;
80 }
81
82 }