Skip to content

Commit 72440e7

Browse files
author
Federico Fissore
committed
Added missing class FileNameExtensionFilter
1 parent 21ef50b commit 72440e7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package processing.app.javax.swing.filechooser;
2+
3+
import javax.swing.filechooser.FileFilter;
4+
import java.io.File;
5+
import java.util.Locale;
6+
7+
public class FileNameExtensionFilter extends FileFilter {
8+
9+
private final String description;
10+
private final String[] extensions;
11+
12+
public FileNameExtensionFilter(String description, String... exts) {
13+
this.description = description;
14+
this.extensions = new String[exts.length];
15+
for (int i = 0; i < exts.length; i++) {
16+
this.extensions[i] = exts[i].toLowerCase(Locale.ENGLISH);
17+
}
18+
}
19+
20+
@Override
21+
public boolean accept(File f) {
22+
if (f == null) {
23+
return false;
24+
}
25+
26+
if (f.isDirectory()) {
27+
return true;
28+
}
29+
30+
String fileName = f.getName();
31+
int i = fileName.lastIndexOf('.');
32+
if (i > 0 && i < fileName.length() - 1) {
33+
String fileExtension = fileName.substring(i + 1).toLowerCase(Locale.ENGLISH);
34+
for (String extension : extensions) {
35+
if (extension.equals(fileExtension)) {
36+
return true;
37+
}
38+
}
39+
}
40+
41+
return false;
42+
}
43+
44+
@Override
45+
public String getDescription() {
46+
return description;
47+
}
48+
}

0 commit comments

Comments
 (0)