1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.Comparator;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.ServiceConfigurationError;
|
---|
12 |
|
---|
13 | import javax.swing.filechooser.FileFilter;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.gui.MapView;
|
---|
17 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
18 | import org.openstreetmap.josm.io.AllFormatsImporter;
|
---|
19 | import org.openstreetmap.josm.io.FileExporter;
|
---|
20 | import org.openstreetmap.josm.io.FileImporter;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * A file filter that filters after the extension. Also includes a list of file
|
---|
25 | * filters used in JOSM.
|
---|
26 | * @since 32
|
---|
27 | */
|
---|
28 | public class ExtensionFileFilter extends FileFilter implements java.io.FileFilter {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * List of supported formats for import.
|
---|
32 | * @since 4869
|
---|
33 | */
|
---|
34 | public static final ArrayList<FileImporter> importers;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * List of supported formats for export.
|
---|
38 | * @since 4869
|
---|
39 | */
|
---|
40 | public static final ArrayList<FileExporter> exporters;
|
---|
41 |
|
---|
42 | // add some file types only if the relevant classes are there.
|
---|
43 | // this gives us the option to painlessly drop them from the .jar
|
---|
44 | // and build JOSM versions without support for these formats
|
---|
45 |
|
---|
46 | static {
|
---|
47 |
|
---|
48 | importers = new ArrayList<>();
|
---|
49 |
|
---|
50 | final List<Class<? extends FileImporter>> importerNames = Arrays.asList(
|
---|
51 | org.openstreetmap.josm.io.OsmImporter.class,
|
---|
52 | org.openstreetmap.josm.io.OsmGzipImporter.class,
|
---|
53 | org.openstreetmap.josm.io.OsmZipImporter.class,
|
---|
54 | org.openstreetmap.josm.io.OsmChangeImporter.class,
|
---|
55 | org.openstreetmap.josm.io.GpxImporter.class,
|
---|
56 | org.openstreetmap.josm.io.NMEAImporter.class,
|
---|
57 | org.openstreetmap.josm.io.NoteImporter.class,
|
---|
58 | org.openstreetmap.josm.io.OsmBzip2Importer.class,
|
---|
59 | org.openstreetmap.josm.io.JpgImporter.class,
|
---|
60 | org.openstreetmap.josm.io.WMSLayerImporter.class,
|
---|
61 | org.openstreetmap.josm.io.AllFormatsImporter.class,
|
---|
62 | org.openstreetmap.josm.io.session.SessionImporter.class
|
---|
63 | );
|
---|
64 |
|
---|
65 | for (final Class<? extends FileImporter> importerClass : importerNames) {
|
---|
66 | try {
|
---|
67 | FileImporter importer = importerClass.newInstance();
|
---|
68 | importers.add(importer);
|
---|
69 | MapView.addLayerChangeListener(importer);
|
---|
70 | } catch (Exception e) {
|
---|
71 | if (Main.isDebugEnabled()) {
|
---|
72 | Main.debug(e.getMessage());
|
---|
73 | }
|
---|
74 | } catch (ServiceConfigurationError e) {
|
---|
75 | // error seen while initializing WMSLayerImporter in plugin unit tests:
|
---|
76 | // -
|
---|
77 | // ServiceConfigurationError: javax.imageio.spi.ImageWriterSpi:
|
---|
78 | // Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi could not be instantiated
|
---|
79 | // Caused by: java.lang.IllegalArgumentException: vendorName == null!
|
---|
80 | // at javax.imageio.spi.IIOServiceProvider.<init>(IIOServiceProvider.java:76)
|
---|
81 | // at javax.imageio.spi.ImageReaderWriterSpi.<init>(ImageReaderWriterSpi.java:231)
|
---|
82 | // at javax.imageio.spi.ImageWriterSpi.<init>(ImageWriterSpi.java:213)
|
---|
83 | // at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi.<init>(CLibJPEGImageWriterSpi.java:84)
|
---|
84 | // -
|
---|
85 | // This is a very strange behaviour of JAI:
|
---|
86 | // http://thierrywasyl.wordpress.com/2009/07/24/jai-how-to-solve-vendorname-null-exception/
|
---|
87 | // -
|
---|
88 | // that can lead to various problems, see #8583 comments
|
---|
89 | Main.error(e);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | exporters = new ArrayList<>();
|
---|
94 |
|
---|
95 | final List<Class<? extends FileExporter>> exporterClasses = Arrays.asList(
|
---|
96 | org.openstreetmap.josm.io.GpxExporter.class,
|
---|
97 | org.openstreetmap.josm.io.OsmExporter.class,
|
---|
98 | org.openstreetmap.josm.io.OsmGzipExporter.class,
|
---|
99 | org.openstreetmap.josm.io.OsmBzip2Exporter.class,
|
---|
100 | org.openstreetmap.josm.io.GeoJSONExporter.CurrentProjection.class, // needs to be considered earlier than GeoJSONExporter
|
---|
101 | org.openstreetmap.josm.io.GeoJSONExporter.class,
|
---|
102 | org.openstreetmap.josm.io.WMSLayerExporter.class,
|
---|
103 | org.openstreetmap.josm.io.NoteExporter.class
|
---|
104 | );
|
---|
105 |
|
---|
106 | for (final Class<? extends FileExporter> exporterClass : exporterClasses) {
|
---|
107 | try {
|
---|
108 | FileExporter exporter = exporterClass.newInstance();
|
---|
109 | exporters.add(exporter);
|
---|
110 | MapView.addLayerChangeListener(exporter);
|
---|
111 | } catch (Exception e) {
|
---|
112 | if (Main.isDebugEnabled()) {
|
---|
113 | Main.debug(e.getMessage());
|
---|
114 | }
|
---|
115 | } catch (ServiceConfigurationError e) {
|
---|
116 | // see above in importers initialization
|
---|
117 | Main.error(e);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | private final String extensions;
|
---|
123 | private final String description;
|
---|
124 | private final String defaultExtension;
|
---|
125 |
|
---|
126 | protected static void sort(List<ExtensionFileFilter> filters) {
|
---|
127 | Collections.sort(
|
---|
128 | filters,
|
---|
129 | new Comparator<ExtensionFileFilter>() {
|
---|
130 | private AllFormatsImporter all = new AllFormatsImporter();
|
---|
131 | @Override
|
---|
132 | public int compare(ExtensionFileFilter o1, ExtensionFileFilter o2) {
|
---|
133 | if (o1.getDescription().equals(all.filter.getDescription())) return 1;
|
---|
134 | if (o2.getDescription().equals(all.filter.getDescription())) return -1;
|
---|
135 | return o1.getDescription().compareTo(o2.getDescription());
|
---|
136 | }
|
---|
137 | }
|
---|
138 | );
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Updates the {@link AllFormatsImporter} that is contained in the importers list. If
|
---|
143 | * you do not use the importers variable directly, you don’t need to call this.
|
---|
144 | * <p>
|
---|
145 | * Updating the AllFormatsImporter is required when plugins add new importers that
|
---|
146 | * support new file extensions. The old AllFormatsImporter doesn’t include the new
|
---|
147 | * extensions and thus will not display these files.
|
---|
148 | *
|
---|
149 | * @since 5131
|
---|
150 | */
|
---|
151 | public static void updateAllFormatsImporter() {
|
---|
152 | for (int i = 0; i < importers.size(); i++) {
|
---|
153 | if (importers.get(i) instanceof AllFormatsImporter) {
|
---|
154 | importers.set(i, new AllFormatsImporter());
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Replies an ordered list of {@link ExtensionFileFilter}s for importing.
|
---|
161 | * The list is ordered according to their description, an {@link AllFormatsImporter}
|
---|
162 | * is append at the end.
|
---|
163 | *
|
---|
164 | * @return an ordered list of {@link ExtensionFileFilter}s for importing.
|
---|
165 | * @since 2029
|
---|
166 | */
|
---|
167 | public static List<ExtensionFileFilter> getImportExtensionFileFilters() {
|
---|
168 | updateAllFormatsImporter();
|
---|
169 | List<ExtensionFileFilter> filters = new LinkedList<>();
|
---|
170 | for (FileImporter importer : importers) {
|
---|
171 | filters.add(importer.filter);
|
---|
172 | }
|
---|
173 | sort(filters);
|
---|
174 | return filters;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Replies an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
|
---|
179 | * The list is ordered according to their description, an {@link AllFormatsImporter}
|
---|
180 | * is append at the end.
|
---|
181 | *
|
---|
182 | * @return an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
|
---|
183 | * @since 2029
|
---|
184 | */
|
---|
185 | public static List<ExtensionFileFilter> getExportExtensionFileFilters() {
|
---|
186 | List<ExtensionFileFilter> filters = new LinkedList<>();
|
---|
187 | for (FileExporter exporter : exporters) {
|
---|
188 | if (filters.contains(exporter.filter) || !exporter.isEnabled()) {
|
---|
189 | continue;
|
---|
190 | }
|
---|
191 | filters.add(exporter.filter);
|
---|
192 | }
|
---|
193 | sort(filters);
|
---|
194 | return filters;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Replies the default {@link ExtensionFileFilter} for a given extension
|
---|
199 | *
|
---|
200 | * @param extension the extension
|
---|
201 | * @return the default {@link ExtensionFileFilter} for a given extension
|
---|
202 | * @since 2029
|
---|
203 | */
|
---|
204 | public static ExtensionFileFilter getDefaultImportExtensionFileFilter(String extension) {
|
---|
205 | if (extension == null) return new AllFormatsImporter().filter;
|
---|
206 | for (FileImporter importer : importers) {
|
---|
207 | if (extension.equals(importer.filter.getDefaultExtension()))
|
---|
208 | return importer.filter;
|
---|
209 | }
|
---|
210 | return new AllFormatsImporter().filter;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Replies the default {@link ExtensionFileFilter} for a given extension
|
---|
215 | *
|
---|
216 | * @param extension the extension
|
---|
217 | * @return the default {@link ExtensionFileFilter} for a given extension
|
---|
218 | * @since 2029
|
---|
219 | */
|
---|
220 | public static ExtensionFileFilter getDefaultExportExtensionFileFilter(String extension) {
|
---|
221 | if (extension == null) return new AllFormatsImporter().filter;
|
---|
222 | for (FileExporter exporter : exporters) {
|
---|
223 | if (extension.equals(exporter.filter.getDefaultExtension()))
|
---|
224 | return exporter.filter;
|
---|
225 | }
|
---|
226 | return new AllFormatsImporter().filter;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
|
---|
231 | * file chooser for selecting a file for reading.
|
---|
232 | *
|
---|
233 | * @param fileChooser the file chooser
|
---|
234 | * @param extension the default extension
|
---|
235 | * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
|
---|
236 | * If false, only the file filters that include {@code extension} will be proposed
|
---|
237 | * @since 5438
|
---|
238 | */
|
---|
239 | public static void applyChoosableImportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
|
---|
240 | for (ExtensionFileFilter filter: getImportExtensionFileFilters()) {
|
---|
241 | if (allTypes || filter.acceptName("file."+extension)) {
|
---|
242 | fileChooser.addChoosableFileFilter(filter);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | fileChooser.setFileFilter(getDefaultImportExtensionFileFilter(extension));
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
|
---|
250 | * file chooser for selecting a file for writing.
|
---|
251 | *
|
---|
252 | * @param fileChooser the file chooser
|
---|
253 | * @param extension the default extension
|
---|
254 | * @param allTypes If true, all the files types known by JOSM will be proposed in the "file type" combobox.
|
---|
255 | * If false, only the file filters that include {@code extension} will be proposed
|
---|
256 | * @since 5438
|
---|
257 | */
|
---|
258 | public static void applyChoosableExportFileFilters(AbstractFileChooser fileChooser, String extension, boolean allTypes) {
|
---|
259 | for (ExtensionFileFilter filter: getExportExtensionFileFilters()) {
|
---|
260 | if (allTypes || filter.acceptName("file."+extension)) {
|
---|
261 | fileChooser.addChoosableFileFilter(filter);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | fileChooser.setFileFilter(getDefaultExportExtensionFileFilter(extension));
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Construct an extension file filter by giving the extension to check after.
|
---|
269 | * @param extension The comma-separated list of file extensions
|
---|
270 | * @param defaultExtension The default extension
|
---|
271 | * @param description A short textual description of the file type
|
---|
272 | * @since 1169
|
---|
273 | */
|
---|
274 | public ExtensionFileFilter(String extension, String defaultExtension, String description) {
|
---|
275 | this.extensions = extension;
|
---|
276 | this.defaultExtension = defaultExtension;
|
---|
277 | this.description = description;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Returns true if this file filter accepts the given filename.
|
---|
282 | * @param filename The filename to check after
|
---|
283 | * @return true if this file filter accepts the given filename (i.e if this filename ends with one of the extensions)
|
---|
284 | * @since 1169
|
---|
285 | */
|
---|
286 | public boolean acceptName(String filename) {
|
---|
287 | return Utils.hasExtension(filename, extensions.split(","));
|
---|
288 | }
|
---|
289 |
|
---|
290 | @Override
|
---|
291 | public boolean accept(File pathname) {
|
---|
292 | if (pathname.isDirectory())
|
---|
293 | return true;
|
---|
294 | return acceptName(pathname.getName());
|
---|
295 | }
|
---|
296 |
|
---|
297 | @Override
|
---|
298 | public String getDescription() {
|
---|
299 | return description;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Replies the comma-separated list of file extensions of this file filter.
|
---|
304 | * @return the comma-separated list of file extensions of this file filter, as a String
|
---|
305 | * @since 5131
|
---|
306 | */
|
---|
307 | public String getExtensions() {
|
---|
308 | return extensions;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Replies the default file extension of this file filter.
|
---|
313 | * @return the default file extension of this file filter
|
---|
314 | * @since 2029
|
---|
315 | */
|
---|
316 | public String getDefaultExtension() {
|
---|
317 | return defaultExtension;
|
---|
318 | }
|
---|
319 |
|
---|
320 | @Override
|
---|
321 | public int hashCode() {
|
---|
322 | final int prime = 31;
|
---|
323 | int result = 1;
|
---|
324 | result = prime * result + ((defaultExtension == null) ? 0 : defaultExtension.hashCode());
|
---|
325 | result = prime * result + ((description == null) ? 0 : description.hashCode());
|
---|
326 | result = prime * result + ((extensions == null) ? 0 : extensions.hashCode());
|
---|
327 | return result;
|
---|
328 | }
|
---|
329 |
|
---|
330 | @Override
|
---|
331 | public boolean equals(Object obj) {
|
---|
332 | if (this == obj)
|
---|
333 | return true;
|
---|
334 | if (obj == null)
|
---|
335 | return false;
|
---|
336 | if (getClass() != obj.getClass())
|
---|
337 | return false;
|
---|
338 | ExtensionFileFilter other = (ExtensionFileFilter) obj;
|
---|
339 | if (defaultExtension == null) {
|
---|
340 | if (other.defaultExtension != null)
|
---|
341 | return false;
|
---|
342 | } else if (!defaultExtension.equals(other.defaultExtension))
|
---|
343 | return false;
|
---|
344 | if (description == null) {
|
---|
345 | if (other.description != null)
|
---|
346 | return false;
|
---|
347 | } else if (!description.equals(other.description))
|
---|
348 | return false;
|
---|
349 | if (extensions == null) {
|
---|
350 | if (other.extensions != null)
|
---|
351 | return false;
|
---|
352 | } else if (!extensions.equals(other.extensions))
|
---|
353 | return false;
|
---|
354 | return true;
|
---|
355 | }
|
---|
356 | }
|
---|