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