| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.actions; |
|---|
| 3 | |
|---|
| 4 | import java.io.File; |
|---|
| 5 | import java.util.ArrayList; |
|---|
| 6 | import java.util.Collections; |
|---|
| 7 | import java.util.Comparator; |
|---|
| 8 | import java.util.LinkedList; |
|---|
| 9 | import java.util.List; |
|---|
| 10 | |
|---|
| 11 | import javax.swing.JFileChooser; |
|---|
| 12 | import javax.swing.filechooser.FileFilter; |
|---|
| 13 | |
|---|
| 14 | import org.openstreetmap.josm.io.AllFormatsImporter; |
|---|
| 15 | import org.openstreetmap.josm.io.FileExporter; |
|---|
| 16 | import org.openstreetmap.josm.io.FileImporter; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * A file filter that filters after the extension. Also includes a list of file |
|---|
| 20 | * filters used in JOSM. |
|---|
| 21 | * |
|---|
| 22 | */ |
|---|
| 23 | public class ExtensionFileFilter extends FileFilter { |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * list of supported formats |
|---|
| 27 | */ |
|---|
| 28 | public static final ArrayList<FileImporter> importers; |
|---|
| 29 | |
|---|
| 30 | public static final ArrayList<FileExporter> exporters; |
|---|
| 31 | |
|---|
| 32 | // add some file types only if the relevant classes are there; |
|---|
| 33 | // this gives us the option to painlessly drop them from the .jar |
|---|
| 34 | // and build JOSM versions without support for these formats |
|---|
| 35 | |
|---|
| 36 | static { |
|---|
| 37 | |
|---|
| 38 | importers = new ArrayList<FileImporter>(); |
|---|
| 39 | |
|---|
| 40 | String[] importerNames = { |
|---|
| 41 | "org.openstreetmap.josm.io.OsmImporter", |
|---|
| 42 | "org.openstreetmap.josm.io.OsmGzipImporter", |
|---|
| 43 | "org.openstreetmap.josm.io.OsmChangeImporter", |
|---|
| 44 | "org.openstreetmap.josm.io.GpxImporter", |
|---|
| 45 | "org.openstreetmap.josm.io.NMEAImporter", |
|---|
| 46 | "org.openstreetmap.josm.io.OsmBzip2Importer", |
|---|
| 47 | "org.openstreetmap.josm.io.JpgImporter", |
|---|
| 48 | "org.openstreetmap.josm.io.AllFormatsImporter" |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | for (String classname : importerNames) { |
|---|
| 52 | try { |
|---|
| 53 | Class<?> klass = Class.forName(classname); |
|---|
| 54 | importers.add((FileImporter) klass.newInstance()); |
|---|
| 55 | } catch (Exception e) {} |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | exporters = new ArrayList<FileExporter>(); |
|---|
| 59 | |
|---|
| 60 | String[] exporterNames = { |
|---|
| 61 | "org.openstreetmap.josm.io.GpxExporter", |
|---|
| 62 | "org.openstreetmap.josm.io.OsmExporter", |
|---|
| 63 | "org.openstreetmap.josm.io.OsmGzipExporter", |
|---|
| 64 | "org.openstreetmap.josm.io.OsmBzip2Exporter", |
|---|
| 65 | "org.openstreetmap.josm.io.GeoJSONExporter", |
|---|
| 66 | }; |
|---|
| 67 | |
|---|
| 68 | for (String classname : exporterNames) { |
|---|
| 69 | try { |
|---|
| 70 | Class<?> klass = Class.forName(classname); |
|---|
| 71 | exporters.add((FileExporter)klass.newInstance()); |
|---|
| 72 | } catch (Exception e) {} |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | private final String extensions; |
|---|
| 77 | private final String description; |
|---|
| 78 | private final String defaultExtension; |
|---|
| 79 | |
|---|
| 80 | static protected void sort(List<ExtensionFileFilter> filters) { |
|---|
| 81 | Collections.sort( |
|---|
| 82 | filters, |
|---|
| 83 | new Comparator<ExtensionFileFilter>() { |
|---|
| 84 | private AllFormatsImporter all = new AllFormatsImporter(); |
|---|
| 85 | public int compare(ExtensionFileFilter o1, ExtensionFileFilter o2) { |
|---|
| 86 | if (o1.getDescription().equals(all.filter.getDescription())) return 1; |
|---|
| 87 | if (o2.getDescription().equals(all.filter.getDescription())) return -1; |
|---|
| 88 | return o1.getDescription().compareTo(o2.getDescription()); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | ); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Updates the {@see AllFormatsImporter} that is contained in the importers list. If |
|---|
| 96 | * you do not use the importers variable directly, you donât need to call this. |
|---|
| 97 | * |
|---|
| 98 | * Updating the AllFormatsImporter is required when plugins add new importers that |
|---|
| 99 | * support new file extensions. The old AllFormatsImporter doesnât include the new |
|---|
| 100 | * extensions and thus will not display these files. |
|---|
| 101 | */ |
|---|
| 102 | public static void updateAllFormatsImporter() { |
|---|
| 103 | for(int i=0; i < importers.size(); i++) { |
|---|
| 104 | if(importers.get(i) instanceof AllFormatsImporter) { |
|---|
| 105 | importers.set(i, new AllFormatsImporter()); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Replies an ordered list of {@see ExtensionFileFilter}s for importing. |
|---|
| 112 | * The list is ordered according to their description, an {@see AllFormatsImporter} |
|---|
| 113 | * is append at the end. |
|---|
| 114 | * |
|---|
| 115 | * @return an ordered list of {@see ExtensionFileFilter}s for importing. |
|---|
| 116 | */ |
|---|
| 117 | public static List<ExtensionFileFilter> getImportExtensionFileFilters() { |
|---|
| 118 | updateAllFormatsImporter(); |
|---|
| 119 | LinkedList<ExtensionFileFilter> filters = new LinkedList<ExtensionFileFilter>(); |
|---|
| 120 | for (FileImporter importer : importers) { |
|---|
| 121 | filters.add(importer.filter); |
|---|
| 122 | } |
|---|
| 123 | sort(filters); |
|---|
| 124 | return filters; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Replies an ordered list of {@see ExtensionFileFilter}s for exporting. |
|---|
| 129 | * The list is ordered according to their description, an {@see AllFormatsImporter} |
|---|
| 130 | * is append at the end. |
|---|
| 131 | * |
|---|
| 132 | * @return an ordered list of {@see ExtensionFileFilter}s for exporting. |
|---|
| 133 | */ |
|---|
| 134 | public static List<ExtensionFileFilter> getExportExtensionFileFilters() { |
|---|
| 135 | LinkedList<ExtensionFileFilter> filters = new LinkedList<ExtensionFileFilter>(); |
|---|
| 136 | for (FileExporter exporter : exporters) { |
|---|
| 137 | if (filters.contains(exporter.filter)) { |
|---|
| 138 | continue; |
|---|
| 139 | } |
|---|
| 140 | filters.add(exporter.filter); |
|---|
| 141 | } |
|---|
| 142 | sort(filters); |
|---|
| 143 | return filters; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Replies the default {@see ExtensionFileFilter} for a given extension |
|---|
| 148 | * |
|---|
| 149 | * @param extension the extension |
|---|
| 150 | * @return the default {@see ExtensionFileFilter} for a given extension |
|---|
| 151 | */ |
|---|
| 152 | public static ExtensionFileFilter getDefaultImportExtensionFileFilter(String extension) { |
|---|
| 153 | if (extension == null) return new AllFormatsImporter().filter; |
|---|
| 154 | for (FileImporter importer : importers) { |
|---|
| 155 | if (extension.equals(importer.filter.getDefaultExtension())) |
|---|
| 156 | return importer.filter; |
|---|
| 157 | } |
|---|
| 158 | return new AllFormatsImporter().filter; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Replies the default {@see ExtensionFileFilter} for a given extension |
|---|
| 163 | * |
|---|
| 164 | * @param extension the extension |
|---|
| 165 | * @return the default {@see ExtensionFileFilter} for a given extension |
|---|
| 166 | */ |
|---|
| 167 | public static ExtensionFileFilter getDefaultExportExtensionFileFilter(String extension) { |
|---|
| 168 | if (extension == null) return new AllFormatsImporter().filter; |
|---|
| 169 | for (FileExporter exporter : exporters) { |
|---|
| 170 | if (extension.equals(exporter.filter.getDefaultExtension())) |
|---|
| 171 | return exporter.filter; |
|---|
| 172 | } |
|---|
| 173 | return new AllFormatsImporter().filter; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * Applies the choosable {@see FileFilter} to a {@see JFileChooser} before using the |
|---|
| 178 | * file chooser for selecting a file for reading. |
|---|
| 179 | * |
|---|
| 180 | * @param fileChooser the file chooser |
|---|
| 181 | * @param extension the default extension |
|---|
| 182 | */ |
|---|
| 183 | public static void applyChoosableImportFileFilters(JFileChooser fileChooser, String extension) { |
|---|
| 184 | for (ExtensionFileFilter filter: getImportExtensionFileFilters()) { |
|---|
| 185 | fileChooser.addChoosableFileFilter(filter); |
|---|
| 186 | } |
|---|
| 187 | fileChooser.setFileFilter(getDefaultImportExtensionFileFilter(extension)); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | /** |
|---|
| 191 | * Applies the choosable {@see FileFilter} to a {@see JFileChooser} before using the |
|---|
| 192 | * file chooser for selecting a file for writing. |
|---|
| 193 | * |
|---|
| 194 | * @param fileChooser the file chooser |
|---|
| 195 | * @param extension the default extension |
|---|
| 196 | */ |
|---|
| 197 | public static void applyChoosableExportFileFilters(JFileChooser fileChooser, String extension) { |
|---|
| 198 | for (ExtensionFileFilter filter: getExportExtensionFileFilters()) { |
|---|
| 199 | fileChooser.addChoosableFileFilter(filter); |
|---|
| 200 | } |
|---|
| 201 | fileChooser.setFileFilter(getDefaultExportExtensionFileFilter(extension)); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | /** |
|---|
| 205 | * Construct an extension file filter by giving the extension to check after. |
|---|
| 206 | */ |
|---|
| 207 | public ExtensionFileFilter(String extension, String defaultExtension, String description) { |
|---|
| 208 | this.extensions = extension; |
|---|
| 209 | this.defaultExtension = defaultExtension; |
|---|
| 210 | this.description = description; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | public boolean acceptName(String filename) { |
|---|
| 214 | String name = filename.toLowerCase(); |
|---|
| 215 | for (String ext : extensions.split(",")) |
|---|
| 216 | if (name.endsWith("."+ext)) |
|---|
| 217 | return true; |
|---|
| 218 | return false; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | @Override public boolean accept(File pathname) { |
|---|
| 222 | if (pathname.isDirectory()) |
|---|
| 223 | return true; |
|---|
| 224 | return acceptName(pathname.getName()); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | @Override public String getDescription() { |
|---|
| 228 | return description; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | public String getExtensions() { |
|---|
| 232 | return extensions; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | public String getDefaultExtension() { |
|---|
| 236 | return defaultExtension; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | @Override |
|---|
| 240 | public int hashCode() { |
|---|
| 241 | final int prime = 31; |
|---|
| 242 | int result = 1; |
|---|
| 243 | result = prime * result + ((defaultExtension == null) ? 0 : defaultExtension.hashCode()); |
|---|
| 244 | result = prime * result + ((description == null) ? 0 : description.hashCode()); |
|---|
| 245 | result = prime * result + ((extensions == null) ? 0 : extensions.hashCode()); |
|---|
| 246 | return result; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | @Override |
|---|
| 250 | public boolean equals(Object obj) { |
|---|
| 251 | if (this == obj) |
|---|
| 252 | return true; |
|---|
| 253 | if (obj == null) |
|---|
| 254 | return false; |
|---|
| 255 | if (getClass() != obj.getClass()) |
|---|
| 256 | return false; |
|---|
| 257 | ExtensionFileFilter other = (ExtensionFileFilter) obj; |
|---|
| 258 | if (defaultExtension == null) { |
|---|
| 259 | if (other.defaultExtension != null) |
|---|
| 260 | return false; |
|---|
| 261 | } else if (!defaultExtension.equals(other.defaultExtension)) |
|---|
| 262 | return false; |
|---|
| 263 | if (description == null) { |
|---|
| 264 | if (other.description != null) |
|---|
| 265 | return false; |
|---|
| 266 | } else if (!description.equals(other.description)) |
|---|
| 267 | return false; |
|---|
| 268 | if (extensions == null) { |
|---|
| 269 | if (other.extensions != null) |
|---|
| 270 | return false; |
|---|
| 271 | } else if (!extensions.equals(other.extensions)) |
|---|
| 272 | return false; |
|---|
| 273 | return true; |
|---|
| 274 | } |
|---|
| 275 | } |
|---|