1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.component; |
16 | |
17 | import java.io.File; |
18 | import java.util.Arrays; |
19 | |
20 | import javax.swing.filechooser.FileFilter; |
21 | |
22 | /** |
23 | * @author Jean-Francois Poilpret |
24 | */ |
25 | public class ExtensionsFileFilter extends FileFilter |
26 | { |
27 | public ExtensionsFileFilter(String[] extensions) |
28 | { |
29 | _extensions = extensions; |
30 | StringBuffer desc = new StringBuffer("All Image Files ("); |
31 | for (int i = 0; i < extensions.length - 1; i++) |
32 | { |
33 | desc.append("*.").append(extensions[i]).append(", "); |
34 | } |
35 | desc.append("*.").append(extensions[extensions.length - 1]).append(")"); |
36 | _description = desc.toString(); |
37 | } |
38 | |
39 | @Override public boolean accept(File f) |
40 | { |
41 | if (f.isDirectory()) |
42 | { |
43 | return true; |
44 | } |
45 | int index = f.getName().lastIndexOf("."); |
46 | if (index == -1) |
47 | { |
48 | return false; |
49 | } |
50 | String ext = f.getName().substring(index + 1).toLowerCase(); |
51 | return Arrays.binarySearch(_extensions, ext) >= 0; |
52 | } |
53 | |
54 | @Override public String getDescription() |
55 | { |
56 | return _description; |
57 | } |
58 | |
59 | private final String[] _extensions; |
60 | private final String _description; |
61 | } |