Source code: com/barteo/emulator/app/ui/swing/ExtensionFileFilter.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001 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.ui.swing;
21
22 import java.io.File;
23 import java.util.Hashtable;
24
25 import javax.swing.filechooser.FileFilter;
26
27
28 public class ExtensionFileFilter extends FileFilter
29 {
30
31 String description;
32
33 Hashtable extensions = new Hashtable();
34
35
36 public ExtensionFileFilter(String description)
37 {
38 this.description = description;
39 }
40
41
42 public boolean accept(File file)
43 {
44 if(file != null) {
45 if(file.isDirectory()) {
46 return true;
47 }
48 String ext = getExtension(file);
49 if(ext != null && extensions.get(ext) != null) {
50 return true;
51 }
52 }
53
54 return false;
55 }
56
57
58 public void addExtension(String extension)
59 {
60 extensions.put(extension.toLowerCase(), this);
61 }
62
63
64 public String getDescription()
65 {
66 return description;
67 }
68
69
70 String getExtension(File file)
71 {
72 if (file != null) {
73 String filename = file.getName();
74 int i = filename.lastIndexOf('.');
75 if (i > 0 && i < filename.length() - 1) {
76 return filename.substring(i + 1).toLowerCase();
77 }
78 }
79
80 return null;
81 }
82
83 }