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