1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 |
|
---|
8 | import javax.swing.filechooser.FileFilter;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A file filter that filters after the extension. Also includes a list of file
|
---|
12 | * filters used in JOSM.
|
---|
13 | *
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class ExtensionFileFilter extends FileFilter {
|
---|
17 | private final String extension;
|
---|
18 | private final String description;
|
---|
19 | public final String defaultExtension;
|
---|
20 |
|
---|
21 | public static final int OSM = 0;
|
---|
22 | public static final int GPX = 1;
|
---|
23 |
|
---|
24 | public static ExtensionFileFilter[] filters = {
|
---|
25 | new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files (.osm .xml)")),
|
---|
26 | new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files (.gpx .gpx.gz)")),
|
---|
27 | };
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Construct an extension file filter by giving the extension to check after.
|
---|
31 | *
|
---|
32 | */
|
---|
33 | private ExtensionFileFilter(String extension, String defExt, String description) {
|
---|
34 | this.extension = extension;
|
---|
35 | defaultExtension = defExt;
|
---|
36 | this.description = description;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public boolean acceptName(String filename) {
|
---|
40 | String name = filename.toLowerCase();
|
---|
41 | for (String ext : extension.split(","))
|
---|
42 | if (name.endsWith("."+ext))
|
---|
43 | return true;
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override public boolean accept(File pathname) {
|
---|
48 | if (pathname.isDirectory())
|
---|
49 | return true;
|
---|
50 | return acceptName(pathname.getName());
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override public String getDescription() {
|
---|
54 | return description;
|
---|
55 | }
|
---|
56 | }
|
---|