Changeset 1637 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2009-06-06T13:43:30+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/DiskAccessAction.java
r1415 r1637 5 5 6 6 import java.io.File; 7 8 7 import javax.swing.JFileChooser; 9 8 import org.openstreetmap.josm.Main; 10 9 import org.openstreetmap.josm.gui.ExtendedDialog; 11 10 import org.openstreetmap.josm.tools.Shortcut; 11 import org.openstreetmap.josm.io.FileImporter; 12 12 13 13 /** … … 25 25 curDir = "."; 26 26 JFileChooser fc = new JFileChooser(new File(curDir)); 27 if (title != null)27 if (title != null) 28 28 fc.setDialogTitle(title); 29 29 30 30 fc.setMultiSelectionEnabled(multiple); 31 for (int i = 0; i < ExtensionFileFilter.filters.length; ++i) 32 fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]); 31 for (FileImporter imExporter: ExtensionFileFilter.importers) { 32 fc.addChoosableFileFilter(imExporter.filter); 33 } 34 33 35 fc.setAcceptAllFileFilterUsed(true); 34 36 -
trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
r1369 r1637 2 2 package org.openstreetmap.josm.actions; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr;5 6 4 import java.io.File; 5 import java.util.ArrayList; 6 import java.util.Arrays; 7 7 8 8 import javax.swing.filechooser.FileFilter; 9 10 import org.openstreetmap.josm.io.FileImporter; 11 import org.openstreetmap.josm.io.GpxImporter; 12 import org.openstreetmap.josm.io.NMEAImporter; 13 import org.openstreetmap.josm.io.OsmImporter; 9 14 10 15 /** … … 19 24 public final String defaultExtension; 20 25 21 public static final int OSM = 0; 22 public static final int GPX = 1; 23 public static final int NMEA = 2; 24 25 public static ExtensionFileFilter[] filters = { 26 new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files")+ " (*.osm *.xml)"), 27 new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"), 28 new ExtensionFileFilter("nmea,nme,nma,txt", "nmea", tr("NMEA-0183 Files") + " (*.nmea *.nme *.nma *.txt)"), 29 }; 26 public static ArrayList<FileImporter> importers = new ArrayList<FileImporter>(Arrays.asList(new OsmImporter(), 27 new GpxImporter(), new NMEAImporter())); 30 28 31 29 /** -
trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java
r1573 r1637 6 6 import java.awt.event.ActionEvent; 7 7 import java.awt.event.KeyEvent; 8 import java.io.InputStream;9 8 import java.io.File; 10 import java.io.FileInputStream;11 import java.io.FileNotFoundException;12 9 import java.io.IOException; 13 import java.util.zip.GZIPInputStream;14 10 15 11 import javax.swing.JFileChooser; … … 17 13 18 14 import org.openstreetmap.josm.Main; 19 import org.openstreetmap.josm.data.osm.DataSet; 20 import org.openstreetmap.josm.gui.layer.GpxLayer; 21 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 22 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 23 import org.openstreetmap.josm.io.GpxReader; 24 import org.openstreetmap.josm.io.NmeaReader; 25 import org.openstreetmap.josm.io.OsmReader; 26 import org.xml.sax.SAXException; 15 import org.openstreetmap.josm.io.FileImporter; 27 16 import org.openstreetmap.josm.tools.Shortcut; 28 17 29 18 /** 30 * Open a file chooser dialog and select an file to import. Then call the gpx-import 31 * driver. Finallyopen an internal frame into the main window with the gpx data shown.19 * Open a file chooser dialog and select an file to import. Then call the gpx-import driver. Finally 20 * open an internal frame into the main window with the gpx data shown. 32 21 * 33 22 * @author imi … … 58 47 try { 59 48 System.out.println("Open file: " + file.getAbsolutePath() + " (" + file.length() + " bytes)"); 60 if (asGpxData(file.getName())) 61 openFileAsGpx(file); 62 else if (asNmeaData(file.getName())) 63 openFileAsNmea(file); 64 else 65 openAsData(file); 66 } catch (SAXException x) { 67 x.printStackTrace(); 68 JOptionPane.showMessageDialog(Main.parent, tr("Error while parsing {0}",file.getName())+": "+x.getMessage()); 49 for (FileImporter importer : ExtensionFileFilter.importers) 50 if (importer.acceptFile(file)) 51 importer.importData(file); 69 52 } catch (IOException x) { 70 53 x.printStackTrace(); 71 JOptionPane.showMessageDialog(Main.parent, tr("Could not read \"{0}\"",file.getName())+"\n"+x.getMessage()); 54 JOptionPane.showMessageDialog(Main.parent, tr("Could not read \"{0}\"", file.getName()) + "\n" 55 + x.getMessage()); 72 56 } 73 57 } 74 58 75 private void openAsData(File file) throws SAXException, IOException, FileNotFoundException {76 String fn = file.getName();77 if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {78 OsmReader osm = OsmReader.parseDataSetOsm(new FileInputStream(file), null, Main.pleaseWaitDlg);79 DataSet dataSet = osm.getDs();80 OsmDataLayer layer = new OsmDataLayer(dataSet, file.getName(), file);81 Main.main.addLayer(layer);82 layer.fireDataChange();83 if (osm.getParseNotes().length() != 0) {84 /* display at most five lines */85 String notes = osm.getParseNotes();86 int j = 0;87 for (int i = 0; i < 5; i++) {88 j = notes.indexOf('\n', j + 1);89 }90 j = j >= 0 ? j : notes.length();91 JOptionPane.showMessageDialog(Main.parent, notes.substring(0, j));92 }93 }94 else95 JOptionPane.showMessageDialog(Main.parent, fn+": "+tr("Unknown file extension: {0}", fn.substring(file.getName().lastIndexOf('.')+1)));96 }97 98 private void openFileAsGpx(File file) throws SAXException, IOException, FileNotFoundException {99 String fn = file.getName();100 if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {101 GpxReader r = null;102 InputStream is;103 if (file.getName().endsWith(".gpx.gz")) {104 is = new GZIPInputStream(new FileInputStream(file));105 } else {106 is = new FileInputStream(file);107 }108 // Workaround for SAX BOM bug109 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835110 if(!((is.read()==0xef)&&(is.read()==0xbb)&&(is.read()==0xbf))) {111 is.close();112 if (file.getName().endsWith(".gpx.gz")) {113 is = new GZIPInputStream(new FileInputStream(file));114 } else {115 is = new FileInputStream(file);116 }117 }118 r = new GpxReader(is,file.getAbsoluteFile().getParentFile());119 r.data.storageFile = file;120 GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);121 Main.main.addLayer(gpxLayer);122 if (Main.pref.getBoolean("marker.makeautomarkers", true)) {123 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);124 if (ml.data.size() > 0) {125 Main.main.addLayer(ml);126 }127 }128 129 } else {130 throw new IllegalStateException();131 }132 }133 134 private void showNmeaInfobox(boolean success, NmeaReader r) {135 String msg = tr("Coordinates imported: ") + r.getNumberOfCoordinates() + "\n" +136 tr("Malformed sentences: ") + r.getParserMalformed() + "\n" +137 tr("Checksum errors: ") + r.getParserChecksumErrors() + "\n";138 if(!success) // don't scare the user unneccessarily139 msg += tr("Unknown sentences: ") + r.getParserUnknown() + "\n";140 msg += tr("Zero coordinates: ") + r.getParserZeroCoordinates();141 if(success) {142 JOptionPane.showMessageDialog(143 Main.parent, msg,144 tr("NMEA import success"),JOptionPane.INFORMATION_MESSAGE);145 } else {146 JOptionPane.showMessageDialog(147 Main.parent, msg,148 tr("NMEA import faliure!"),JOptionPane.ERROR_MESSAGE);149 }150 }151 152 private void openFileAsNmea(File file) throws IOException, FileNotFoundException {153 String fn = file.getName();154 if (ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn)) {155 NmeaReader r = new NmeaReader(new FileInputStream(file), file.getAbsoluteFile().getParentFile());156 if(r.getNumberOfCoordinates()>0) {157 r.data.storageFile = file;158 GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);159 Main.main.addLayer(gpxLayer);160 if (Main.pref.getBoolean("marker.makeautomarkers", true)) {161 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);162 if (ml.data.size() > 0) {163 Main.main.addLayer(ml);164 }165 }166 }167 showNmeaInfobox(r.getNumberOfCoordinates()>0, r);168 } else {169 throw new IllegalStateException();170 }171 }172 173 private boolean asGpxData(String fn) {174 return ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn);175 }176 177 private boolean asNmeaData(String fn) {178 return ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn);179 }180 181 182 59 } -
trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java
r1525 r1637 22 22 import org.openstreetmap.josm.gui.layer.Layer; 23 23 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 24 import org.openstreetmap.josm.io.GpxImporter; 24 25 import org.openstreetmap.josm.io.GpxWriter; 26 import org.openstreetmap.josm.io.OsmImporter; 25 27 import org.openstreetmap.josm.io.OsmWriter; 26 28 import org.openstreetmap.josm.tools.Shortcut; … … 150 152 File tmpFile = null; 151 153 try { 152 if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(file.getPath())) { 154 GpxImporter gpxImExporter = new GpxImporter(); 155 OsmImporter osmImExporter = new OsmImporter(); 156 if (gpxImExporter.acceptFile(file)) 153 157 GpxExportAction.exportGpx(file, layer); 154 } else if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(file.getPath())) {158 else if (osmImExporter.acceptFile(file)) { 155 159 // use a tmp file because if something errors out in the 156 160 // process of writing the file, we might just end up with … … 194 198 File tmpFile = null; 195 199 try { 196 if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(file.getPath())) { 200 GpxImporter gpxImExporter = new GpxImporter(); 201 if (gpxImExporter.acceptFile(file)) { 197 202 198 203 // use a tmp file because if something errors out in the
Note:
See TracChangeset
for help on using the changeset viewer.