View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  It is licensed under the following two licenses as alternatives:
6    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7    *    2. Apache License (the "AL") Version 2.0
8    *
9    *  You may not use this file except in compliance with at least one of
10   *  the above two licenses.
11   *
12   *  You may obtain a copy of the LGPL at
13   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14   *
15   *  You may obtain a copy of the AL at
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the LGPL or the AL for the specific language governing permissions and
22   *  limitations.
23   */
24   
25  package org.microemu.app.ui.swing;
26  
27  import java.io.File;
28  import java.util.Hashtable;
29  
30  import javax.swing.filechooser.FileFilter;
31  
32  
33  public class ExtensionFileFilter extends FileFilter 
34  {
35    
36    String description;
37    
38    Hashtable extensions = new Hashtable(); 
39    
40  
41    public ExtensionFileFilter(String description)
42    {
43      this.description = description;
44    }
45    
46    
47    public boolean accept(File file) 
48    {
49      if(file != null) {
50  	    if(file.isDirectory()) {
51          return true;
52  	    }
53        String ext = getExtension(file);
54        if(ext != null && extensions.get(ext) != null) {
55          return true;
56        }
57      }
58      
59    	return false;
60    }
61    
62    
63    public void addExtension(String extension)
64    {
65      extensions.put(extension.toLowerCase(), this);
66    }
67  
68    
69    public String getDescription() 
70    {
71      return description;
72    }
73  
74    
75    String getExtension(File file) 
76    {
77      if (file != null) {
78  	    String filename = file.getName();
79  	    int i = filename.lastIndexOf('.');
80  	    if (i > 0 && i < filename.length() - 1) {
81          return filename.substring(i + 1).toLowerCase();
82  	    }
83      }
84    
85      return null;
86    }
87    
88  }