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 | public static final int NMEA = 2;
|
---|
24 |
|
---|
25 | public static ExtensionFileFilter[] filters = {
|
---|
26 | new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files (*.osm *.xml)")),
|
---|
27 | new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files (*.gpx *.gpx.gz)")),
|
---|
28 | new ExtensionFileFilter("nmea,txt", "nmea", tr("NMEA-0183 Files (*.nmea *.txt)")),
|
---|
29 | };
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Construct an extension file filter by giving the extension to check after.
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public ExtensionFileFilter(String extension, String defExt, String description) {
|
---|
36 | this.extension = extension;
|
---|
37 | defaultExtension = defExt;
|
---|
38 | this.description = description;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public boolean acceptName(String filename) {
|
---|
42 | String name = filename.toLowerCase();
|
---|
43 | for (String ext : extension.split(","))
|
---|
44 | if (name.endsWith("."+ext))
|
---|
45 | return true;
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override public boolean accept(File pathname) {
|
---|
50 | if (pathname.isDirectory())
|
---|
51 | return true;
|
---|
52 | return acceptName(pathname.getName());
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override public String getDescription() {
|
---|
56 | return description;
|
---|
57 | }
|
---|
58 | }
|
---|