- Timestamp:
- 2009-12-29T23:31:57+01:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/DiskAccessAction.java
r2070 r2702 35 35 } 36 36 37 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 37 38 fc.setMultiSelectionEnabled(multiple); 38 39 fc.setAcceptAllFileFilterUsed(false); 39 System.out.println("opening fc for extension " + extension);40 //System.out.println("opening fc for extension " + extension); 40 41 ExtensionFileFilter.applyChoosableImportFileFilters(fc, extension); 41 42 -
trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
r2512 r2702 15 15 import javax.swing.filechooser.FileFilter; 16 16 17 import org.openstreetmap.josm.io.AllFormatsImporter; 17 18 import org.openstreetmap.josm.io.FileExporter; 18 19 import org.openstreetmap.josm.io.FileImporter; 19 import org.openstreetmap.josm.io.GpxExporter;20 import org.openstreetmap.josm.io.GpxImporter;21 import org.openstreetmap.josm.io.NMEAImporter;22 import org.openstreetmap.josm.io.OsmBzip2Exporter;23 import org.openstreetmap.josm.io.OsmBzip2Importer;24 import org.openstreetmap.josm.io.OsmExporter;25 import org.openstreetmap.josm.io.OsmGzipExporter;26 import org.openstreetmap.josm.io.OsmGzipImporter;27 import org.openstreetmap.josm.io.OsmImporter;28 20 29 21 /** … … 55 47 "org.openstreetmap.josm.io.NMEAImporter", 56 48 "org.openstreetmap.josm.io.OsmBzip2Importer", 49 "org.openstreetmap.josm.io.JpgImporter", 57 50 "org.openstreetmap.josm.io.AllFormatsImporter" 58 51 }; … … 110 103 LinkedList<ExtensionFileFilter> filters = new LinkedList<ExtensionFileFilter>(); 111 104 for (FileImporter importer : importers) { 112 if (filters.contains(importer.filter)) {113 continue;114 }115 105 filters.add(importer.filter); 116 106 } … … 225 215 public String getDefaultExtension() { 226 216 return defaultExtension; 227 }228 229 /**230 * Dummy importer that adds the "All Formats"-Filter when opening files231 */232 public static class AllFormatsImporter extends FileImporter {233 public AllFormatsImporter() {234 super(235 new ExtensionFileFilter("osm,xml,osm.gz,osm.bz2,osm.bz,gpx,gpx.gz,nmea,nme,nma,txt,wms", "", tr("All Formats")236 + " (*.gpx *.osm *.nmea ...)"));237 }238 @Override public boolean acceptFile(File pathname) {239 return false;240 }241 217 } 242 218 -
trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java
r2563 r2702 9 9 import java.io.File; 10 10 import java.io.IOException; 11 import java.util.ArrayList; 11 12 import java.util.Arrays; 13 import java.util.Collections; 12 14 import java.util.List; 13 15 14 16 import javax.swing.JFileChooser; 15 17 import javax.swing.JOptionPane; 18 import javax.swing.filechooser.FileFilter; 16 19 17 20 import org.openstreetmap.josm.Main; 18 21 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 22 import org.openstreetmap.josm.io.AllFormatsImporter; 19 23 import org.openstreetmap.josm.io.FileImporter; 20 24 import org.openstreetmap.josm.io.IllegalDataException; 21 25 import org.openstreetmap.josm.io.OsmTransferException; 26 import org.openstreetmap.josm.tools.MultiMap; 22 27 import org.openstreetmap.josm.tools.Shortcut; 23 28 import org.xml.sax.SAXException; … … 46 51 return; 47 52 File[] files = fc.getSelectedFiles(); 48 OpenFileTask task = new OpenFileTask(Arrays.asList(files) );53 OpenFileTask task = new OpenFileTask(Arrays.asList(files), fc.getFileFilter()); 49 54 Main.worker.submit(task); 50 55 } … … 55 60 } 56 61 57 static public void openFile(File f) throws IOException, IllegalDataException {58 for (FileImporter importer : ExtensionFileFilter.importers)59 if (importer.acceptFile(f)) {60 importer.importData(f);61 }62 }63 64 62 static public class OpenFileTask extends PleaseWaitRunnable { 65 63 private List<File> files; 64 private FileFilter fileFilter; 66 65 private boolean cancelled; 67 66 68 public OpenFileTask(List<File> files ) {67 public OpenFileTask(List<File> files, FileFilter fileFilter) { 69 68 super(tr("Opening files"), false /* don't ignore exception */); 70 this.files = files; 69 this.files = new ArrayList<File>(files); 70 this.fileFilter = fileFilter; 71 71 } 72 72 @Override … … 83 83 protected void realRun() throws SAXException, IOException, OsmTransferException { 84 84 if (files == null || files.isEmpty()) return; 85 86 /** 87 * Find the importer with the chosen file filter 88 */ 89 FileImporter chosenImporter = null; 90 for (FileImporter importer : ExtensionFileFilter.importers) { 91 if (fileFilter == importer.filter) { 92 chosenImporter = importer; 93 } 94 } 95 /** 96 * If the filter wasn't changed in the dialog, chosenImporter is null now. 97 * When the filter was expicitly set to AllFormatsImporter, treat this the same. 98 */ 99 if (chosenImporter instanceof AllFormatsImporter) { 100 chosenImporter = null; 101 } 85 102 getProgressMonitor().setTicks(files.size()); 86 for (File f : files) { 103 104 if (chosenImporter != null) { // The importer was expicitely chosen, so use it. 105 //System.err.println("Importer: " +chosenImporter.getClass().getName()); 106 for (File f : files) { 107 if (!chosenImporter.acceptFile(f)) { 108 if (f.isDirectory()) { 109 JOptionPane.showMessageDialog( 110 Main.parent, 111 tr("<html>Cannot open directory.<br>Please select a file!"), 112 tr("Open file"), 113 JOptionPane.INFORMATION_MESSAGE 114 ); 115 return; 116 } else 117 throw new IllegalStateException(); 118 } 119 } 120 importData(chosenImporter, files); 121 } 122 else { // find apropriate importer 123 MultiMap<FileImporter, File> map = new MultiMap<FileImporter, File>(); 124 while (! files.isEmpty()) { 125 File f = files.get(0); 126 for (FileImporter importer : ExtensionFileFilter.importers) { 127 if (importer.acceptFile(f)) { 128 map.add(importer, f); 129 files.remove(f); 130 } 131 } 132 if (files.contains(f)) 133 throw new RuntimeException(); // no importer found 134 } 135 List<FileImporter> ims = new ArrayList<FileImporter>(map.keySet()); 136 Collections.sort(ims); 137 Collections.reverse(ims); 138 for (FileImporter importer : ims) { 139 //System.err.println("Using "+importer.getClass().getName()); 140 List<File> files = map.get(importer); 141 //System.err.println("for files: "+files); 142 importData(importer, files); 143 } 144 } 145 } 146 147 public void importData(FileImporter importer, List<File> files) { 148 if (importer.isBatchImporter()) { 87 149 if (cancelled) return; 88 getProgressMonitor().indeterminateSubTask(tr("Opening file ''{0}'' ...", f.getAbsolutePath())); 89 try { 90 System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)"); 91 openFile(f); 92 } catch (Exception e) { 93 e.printStackTrace(); 94 JOptionPane.showMessageDialog( 95 Main.parent, 96 tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()), 97 tr("Error"), 98 JOptionPane.ERROR_MESSAGE 99 ); 150 String msg; 151 if (files.size() == 1) { 152 msg = tr("Opening 1 file..."); 153 } else { 154 msg = tr("Opening {0} files...", files.size()); 100 155 } 101 getProgressMonitor().worked(1); 156 getProgressMonitor().indeterminateSubTask(msg); 157 importer.importDataHandleExceptions(files); 158 getProgressMonitor().worked(files.size()); 159 } else { 160 for (File f : files) { 161 if (cancelled) return; 162 getProgressMonitor().indeterminateSubTask(tr("Opening file ''{0}'' ...", f.getAbsolutePath())); 163 importer.importDataHandleExceptions(f); 164 getProgressMonitor().worked(1); 165 } 102 166 } 103 167 } -
trunk/src/org/openstreetmap/josm/gui/FileDrop.java
r2676 r2702 77 77 public void filesDropped( java.io.File[] files ){ 78 78 // start asynchronous loading of files 79 OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(Arrays.asList(files) );79 OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(Arrays.asList(files), null); 80 80 Main.worker.submit(task); 81 81 } -
trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java
r2635 r2702 64 64 import org.openstreetmap.josm.gui.dialogs.LayerListDialog; 65 65 import org.openstreetmap.josm.gui.dialogs.LayerListPopup; 66 import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;67 66 import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker; 68 67 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 69 68 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 69 import org.openstreetmap.josm.io.JpgImporter; 70 70 import org.openstreetmap.josm.tools.AudioUtil; 71 71 import org.openstreetmap.josm.tools.DateUtils; … … 326 326 fc.setMultiSelectionEnabled(true); 327 327 fc.setAcceptAllFileFilterUsed(false); 328 fc.setFileFilter(new FileFilter() { 329 @Override 330 public boolean accept(File f) { 331 return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg"); 332 } 333 334 @Override 335 public String getDescription() { 336 return tr("JPEG images (*.jpg)"); 337 } 338 }); 328 JpgImporter importer = new JpgImporter(GpxLayer.this); 329 fc.setFileFilter(importer.filter); 339 330 fc.showOpenDialog(Main.parent); 331 LinkedList<File> files = new LinkedList<File>(); 340 332 File[] sel = fc.getSelectedFiles(); 341 333 if (sel == null || sel.length == 0) 342 334 return; 343 LinkedList<File> files = new LinkedList<File>();344 335 addRecursiveFiles(files, sel); 345 Main.pref.put("geoimage.lastdirectory", fc.getCurrentDirectory().getPath()); 346 GeoImageLayer.create(files, GpxLayer.this); 336 importer.importDataHandleExceptions(files); 347 337 } 348 338 -
trunk/src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java
r2676 r2702 1074 1074 Object item = cbGpx.getSelectedItem(); 1075 1075 1076 if (item == null || ! (item instanceof GpxDataWrapper)) {1076 if (item == null || ((GpxDataWrapper) item).file == null) { 1077 1077 if (complain) { 1078 1078 JOptionPane.showMessageDialog(Main.parent, tr("You should select a GPX track"), -
trunk/src/org/openstreetmap/josm/io/FileImporter.java
r2181 r2702 6 6 import java.io.File; 7 7 import java.io.IOException; 8 import java.util.List; 8 9 10 import javax.swing.JOptionPane; 11 12 import org.openstreetmap.josm.Main; 9 13 import org.openstreetmap.josm.actions.ExtensionFileFilter; 10 14 11 public abstract class FileImporter {15 public abstract class FileImporter implements Comparable<FileImporter> { 12 16 13 17 public ExtensionFileFilter filter; … … 21 25 } 22 26 27 /** 28 * A batch importer is a file importer that preferes to read multiple files at the same time. 29 */ 30 public boolean isBatchImporter() { 31 return false; 32 } 33 34 /** 35 * Needs to be implemented if isBatchImporter() returns false. 36 */ 23 37 public void importData(File file) throws IOException, IllegalDataException { 24 throw new IOException(tr("Could not read''{0}''.", file.getName()));38 throw new IOException(tr("Could not import ''{0}''.", file.getName())); 25 39 } 26 40 41 /** 42 * Needs to be implemented if isBatchImporter() returns true. 43 */ 44 public void importData(List<File> files) throws IOException, IllegalDataException { 45 throw new IOException(tr("Could not import Files.")); 46 } 47 48 /** 49 * Wrapper to give meaningful output if things go wrong. 50 */ 51 public void importDataHandleExceptions(File f) { 52 try { 53 System.out.println("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)"); 54 importData(f); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 JOptionPane.showMessageDialog( 58 Main.parent, 59 tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()), 60 tr("Error"), 61 JOptionPane.ERROR_MESSAGE 62 ); 63 } 64 } 65 public void importDataHandleExceptions(List<File> files) { 66 try { 67 System.out.println("Open "+files.size()+" files"); 68 importData(files); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 JOptionPane.showMessageDialog( 72 Main.parent, 73 tr("<html>Could not read files.<br> Error is: <br>{0}</html>", e.getMessage()), 74 tr("Error"), 75 JOptionPane.ERROR_MESSAGE 76 ); 77 } 78 } 79 80 /** 81 * If multiple files (with multiple file formats) are selected, 82 * they are opened in the order of their priorities. 83 * Highest priority comes first. 84 */ 85 public double getPriority() { 86 return 0; 87 } 88 89 public int compareTo(FileImporter other) { 90 return (new Double(this.getPriority())).compareTo(other.getPriority()); 91 } 27 92 }
Note:
See TracChangeset
for help on using the changeset viewer.