Changeset 1949 in josm
- Timestamp:
- 2009-08-10T13:50:09+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 5 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/DiskAccessAction.java
r1879 r1949 6 6 import java.io.File; 7 7 import javax.swing.JFileChooser; 8 import javax.swing.filechooser.FileFilter;9 8 import org.openstreetmap.josm.Main; 10 9 import org.openstreetmap.josm.gui.ExtendedDialog; … … 59 58 return fc; 60 59 } 61 62 public static File createAndOpenSaveFileChooser(String title, String extension) {63 String curDir = Main.pref.get("lastDirectory");64 if (curDir.equals("")) {65 curDir = ".";66 }67 JFileChooser fc = new JFileChooser(new File(curDir));68 if (title != null) {69 fc.setDialogTitle(title);70 }71 72 fc.setMultiSelectionEnabled(false);73 for (FileImporter imExporter: ExtensionFileFilter.importers) {74 fc.addChoosableFileFilter(imExporter.filter);75 }76 77 fc.setAcceptAllFileFilterUsed(true);78 79 int answer = fc.showSaveDialog(Main.parent);80 if (answer != JFileChooser.APPROVE_OPTION)81 return null;82 83 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {84 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());85 }86 87 File file = fc.getSelectedFile();88 if(extension != null){89 String fn = file.getPath();90 if(fn.indexOf('.') == -1)91 {92 FileFilter ff = fc.getFileFilter();93 if (ff instanceof ExtensionFileFilter) {94 fn += "." + ((ExtensionFileFilter)ff).defaultExtension;95 } else {96 fn += extension;97 }98 file = new File(fn);99 }100 }101 if(file == null || (file.exists() && 1 != new ExtendedDialog(Main.parent,102 tr("Overwrite"), tr("File exists. Overwrite?"),103 new String[] {tr("Overwrite"), tr("Cancel")},104 new String[] {"save_as.png", "cancel.png"}).getValue()))105 return null;106 return file;107 }108 60 } -
trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
r1653 r1949 8 8 import javax.swing.filechooser.FileFilter; 9 9 10 import org.openstreetmap.josm.io.FileExporter; 10 11 import org.openstreetmap.josm.io.FileImporter; 12 import org.openstreetmap.josm.io.GpxExporter; 11 13 import org.openstreetmap.josm.io.GpxImporter; 12 14 import org.openstreetmap.josm.io.NMEAImporter; 15 import org.openstreetmap.josm.io.OsmBzip2Exporter; 13 16 import org.openstreetmap.josm.io.OsmBzip2Importer; 17 import org.openstreetmap.josm.io.OsmExporter; 18 import org.openstreetmap.josm.io.OsmGzipExporter; 14 19 import org.openstreetmap.josm.io.OsmGzipImporter; 15 20 import org.openstreetmap.josm.io.OsmImporter; … … 28 33 public static ArrayList<FileImporter> importers = new ArrayList<FileImporter>(Arrays.asList(new OsmImporter(), 29 34 new OsmGzipImporter(), new OsmBzip2Importer(), new GpxImporter(), new NMEAImporter())); 35 36 // list of supported export formats 37 public static ArrayList<FileExporter> exporters = new ArrayList<FileExporter>(Arrays.asList(new GpxExporter(), 38 new OsmExporter(), new OsmGzipExporter(), new OsmBzip2Exporter())); 30 39 31 40 /** -
trunk/src/org/openstreetmap/josm/actions/GpxExportAction.java
r1857 r1949 2 2 package org.openstreetmap.josm.actions; 3 3 4 import static org.openstreetmap.josm.actions.SaveActionBase.createAndOpenSaveFileChooser; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 6 import java.awt.GridBagLayout;7 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener;9 import java.awt.event.KeyAdapter;10 8 import java.awt.event.KeyEvent; 11 9 import java.io.File; 12 import java.io.FileOutputStream;13 10 import java.io.IOException; 14 import java.util.Calendar;15 11 16 import javax.swing.JButton;17 import javax.swing.JCheckBox;18 import javax.swing.JFileChooser;19 import javax.swing.JLabel;20 import javax.swing.JList;21 12 import javax.swing.JOptionPane; 22 import javax.swing.JPanel;23 import javax.swing.JScrollPane;24 import javax.swing.JTextArea;25 import javax.swing.JTextField;26 import javax.swing.ListSelectionModel;27 13 28 14 import org.openstreetmap.josm.Main; 29 import org.openstreetmap.josm.data.gpx.GpxData;30 import org.openstreetmap.josm.gui.ExtendedDialog;31 15 import org.openstreetmap.josm.gui.OptionPaneUtil; 32 16 import org.openstreetmap.josm.gui.layer.GpxLayer; 33 17 import org.openstreetmap.josm.gui.layer.Layer; 34 18 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 35 import org.openstreetmap.josm.io.GpxWriter; 36 import org.openstreetmap.josm.tools.GBC; 19 import org.openstreetmap.josm.io.FileExporter; 37 20 import org.openstreetmap.josm.tools.Shortcut; 38 21 … … 42 25 public class GpxExportAction extends DiskAccessAction { 43 26 44 private final static String warningGpl = "<html><font color='red' size='-2'>"+tr("Note: GPL is not compatible with the OSM license. Do not upload GPL licensed tracks.")+"</html>";45 27 46 28 public GpxExportAction() { … … 76 58 /** 77 59 * Exports a layer to a file. Launches a file chooser to request the user to enter a file name. 78 * 60 * 79 61 * <code>layer</code> must not be null. <code>layer</code> must be an instance of 80 62 * {@see OsmDataLayer} or {@see GpxLayer}. … … 91 73 throw new IllegalArgumentException(tr("expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.", layer.getClass().getName())); 92 74 93 JFileChooser fc = createAndOpenFileChooser(false, false, null); 94 if (fc == null) 95 return; 96 File file = fc.getSelectedFile(); 75 File file = createAndOpenSaveFileChooser(tr("Export GPX file"), ".gpx"); 97 76 if (file == null) 98 77 return; 99 78 100 exportGpx(file, layer); 101 } 102 103 /** 104 * Exports a layer to a file. 105 * 106 * <code>layer</code> must not be null. <code>layer</code> must be an instance of 107 * {@see OsmDataLayer} or {@see GpxLayer}. 108 * 109 * @param layer the layer 110 * @exception IllegalArgumentException thrown if layer is null 111 * @exception IllegalArgumentException thrown if layer is neither an instance of {@see OsmDataLayer} 112 * nor of {@see GpxLayer} 113 */ 114 115 public void exportGpx(File file, Layer layer) { 116 if (layer == null) 117 throw new IllegalArgumentException(tr("paramenter ''{0}'' must not be null", "layer")); 118 if (! (layer instanceof OsmDataLayer) && ! (layer instanceof GpxLayer)) 119 throw new IllegalArgumentException(tr("expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.", layer.getClass().getName())); 120 if (file == null) 121 throw new IllegalArgumentException(tr("paramenter ''{0}'' must not be null", "file")); 122 123 String fn = file.getPath(); 124 if (fn.indexOf('.') == -1) { 125 fn += ".gpx"; 126 file = new File(fn); 127 } 128 129 // open the dialog asking for options 130 JPanel p = new JPanel(new GridBagLayout()); 131 132 p.add(new JLabel(tr("gps track description")), GBC.eol()); 133 JTextArea desc = new JTextArea(3,40); 134 desc.setWrapStyleWord(true); 135 desc.setLineWrap(true); 136 p.add(new JScrollPane(desc), GBC.eop().fill(GBC.BOTH)); 137 138 JCheckBox author = new JCheckBox(tr("Add author information"), Main.pref.getBoolean("lastAddAuthor", true)); 139 author.setSelected(true); 140 p.add(author, GBC.eol()); 141 JLabel nameLabel = new JLabel(tr("Real name")); 142 p.add(nameLabel, GBC.std().insets(10,0,5,0)); 143 JTextField authorName = new JTextField(Main.pref.get("lastAuthorName")); 144 p.add(authorName, GBC.eol().fill(GBC.HORIZONTAL)); 145 JLabel emailLabel = new JLabel(tr("E-Mail")); 146 p.add(emailLabel, GBC.std().insets(10,0,5,0)); 147 JTextField email = new JTextField(Main.pref.get("osm-server.username")); 148 p.add(email, GBC.eol().fill(GBC.HORIZONTAL)); 149 JLabel copyrightLabel = new JLabel(tr("Copyright (URL)")); 150 p.add(copyrightLabel, GBC.std().insets(10,0,5,0)); 151 JTextField copyright = new JTextField(); 152 p.add(copyright, GBC.std().fill(GBC.HORIZONTAL)); 153 JButton predefined = new JButton(tr("Predefined")); 154 p.add(predefined, GBC.eol().insets(5,0,0,0)); 155 JLabel copyrightYearLabel = new JLabel(tr("Copyright year")); 156 p.add(copyrightYearLabel, GBC.std().insets(10,0,5,5)); 157 JTextField copyrightYear = new JTextField(""); 158 p.add(copyrightYear, GBC.eol().fill(GBC.HORIZONTAL)); 159 JLabel warning = new JLabel("<html><font size='-2'> </html"); 160 p.add(warning, GBC.eol().fill(GBC.HORIZONTAL).insets(15,0,0,0)); 161 addDependencies(author, authorName, email, copyright, predefined, copyrightYear, nameLabel, emailLabel, copyrightLabel, copyrightYearLabel, warning); 162 163 // if the user name is not the email address, but the osm user name 164 // move it from the email textfield to the author textfield 165 if(!email.getText().contains("@")) { 166 authorName.setText(email.getText()); 167 email.setText(""); 168 } 169 170 p.add(new JLabel(tr("Keywords")), GBC.eol()); 171 JTextField keywords = new JTextField(); 172 p.add(keywords, GBC.eop().fill(GBC.HORIZONTAL)); 173 174 int answer = new ExtendedDialog(Main.parent, 175 tr("Export options"), 176 p, 177 new String[] {tr("Export and Save"), tr("Cancel")}, 178 new String[] {"exportgpx.png", "cancel.png"}).getValue(); 179 if (answer != 1) 180 return; 181 182 Main.pref.put("lastAddAuthor", author.isSelected()); 183 if (authorName.getText().length() != 0) { 184 Main.pref.put("lastAuthorName", authorName.getText()); 185 } 186 if (copyright.getText().length() != 0) { 187 Main.pref.put("lastCopyright", copyright.getText()); 188 } 189 190 GpxData gpxData; 191 if (layer instanceof OsmDataLayer) { 192 gpxData = ((OsmDataLayer)layer).toGpxData(); 193 } else if (layer instanceof GpxLayer) { 194 gpxData = ((GpxLayer)layer).data; 195 } else { 196 gpxData = OsmDataLayer.toGpxData(getCurrentDataSet(), file); 197 } 198 199 // add author and copyright details to the gpx data 200 if(author.isSelected()) { 201 if(authorName.getText().length() > 0) { 202 gpxData.attr.put(GpxData.META_AUTHOR_NAME, authorName.getText()); 203 gpxData.attr.put(GpxData.META_COPYRIGHT_AUTHOR, authorName.getText()); 79 for (FileExporter exporter : ExtensionFileFilter.exporters) { 80 if (exporter.acceptFile(file, layer)) { 81 try { 82 exporter.exportData(file, layer); 83 } catch (IOException e1) { 84 e1.printStackTrace(); 85 } 204 86 } 205 if(email.getText().length() > 0) {206 gpxData.attr.put(GpxData.META_AUTHOR_EMAIL, email.getText());207 }208 if(copyright.getText().length() > 0) {209 gpxData.attr.put(GpxData.META_COPYRIGHT_LICENSE, copyright.getText());210 }211 if(copyrightYear.getText().length() > 0) {212 gpxData.attr.put(GpxData.META_COPYRIGHT_YEAR, copyrightYear.getText());213 }214 }215 216 // add the description to the gpx data217 if(desc.getText().length() > 0) {218 gpxData.attr.put(GpxData.META_DESC, desc.getText());219 }220 221 // add keywords to the gpx data222 if(keywords.getText().length() > 0) {223 gpxData.attr.put(GpxData.META_KEYWORDS, keywords.getText());224 }225 226 try {227 FileOutputStream fo = new FileOutputStream(file);228 new GpxWriter(fo).write(gpxData);229 fo.flush();230 fo.close();231 } catch (IOException x) {232 x.printStackTrace();233 OptionPaneUtil.showMessageDialog(234 Main.parent,235 tr("Error while exporting {0}:\n{1}", fn,x.getMessage()),236 tr("Error"),237 JOptionPane.ERROR_MESSAGE238 );239 }240 }241 242 /**243 * Add all those listeners to handle the enable state of the fields.244 * @param copyrightYearLabel245 * @param copyrightLabel246 * @param emailLabel247 * @param nameLabel248 * @param warning249 */250 private static void addDependencies(251 final JCheckBox author,252 final JTextField authorName,253 final JTextField email,254 final JTextField copyright,255 final JButton predefined,256 final JTextField copyrightYear,257 final JLabel nameLabel,258 final JLabel emailLabel,259 final JLabel copyrightLabel,260 final JLabel copyrightYearLabel,261 final JLabel warning) {262 263 ActionListener authorActionListener = new ActionListener(){264 public void actionPerformed(ActionEvent e) {265 boolean b = author.isSelected();266 authorName.setEnabled(b);267 email.setEnabled(b);268 nameLabel.setEnabled(b);269 emailLabel.setEnabled(b);270 authorName.setText(b ? Main.pref.get("lastAuthorName") : "");271 email.setText(b ? Main.pref.get("osm-server.username") : "");272 273 boolean authorSet = authorName.getText().length() != 0;274 enableCopyright(copyright, predefined, copyrightYear, copyrightLabel, copyrightYearLabel, warning, b && authorSet);275 }276 };277 author.addActionListener(authorActionListener);278 279 KeyAdapter authorNameListener = new KeyAdapter(){280 @Override public void keyReleased(KeyEvent e) {281 boolean b = authorName.getText().length()!=0 && author.isSelected();282 enableCopyright(copyright, predefined, copyrightYear, copyrightLabel, copyrightYearLabel, warning, b);283 }284 };285 authorName.addKeyListener(authorNameListener);286 287 predefined.addActionListener(new ActionListener(){288 public void actionPerformed(ActionEvent e) {289 JList l = new JList(new String[]{"Creative Commons By-SA", "public domain", "GNU Lesser Public License (LGPL)", "BSD License (MIT/X11)"});290 l.setVisibleRowCount(4);291 l.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);292 int answer = OptionPaneUtil.showConfirmationDialog(293 Main.parent,294 new JScrollPane(l),295 tr("Choose a predefined license"),296 JOptionPane.OK_CANCEL_OPTION,297 JOptionPane.QUESTION_MESSAGE298 );299 if (answer != JOptionPane.OK_OPTION || l.getSelectedIndex() == -1)300 return;301 final String[] urls = {302 "http://creativecommons.org/licenses/by-sa/2.5",303 "public domain",304 "http://www.gnu.org/copyleft/lesser.html",305 "http://www.opensource.org/licenses/bsd-license.php"};306 String license = "";307 for (int i : l.getSelectedIndices()) {308 if (i == 1) {309 license = "public domain";310 break;311 }312 license += license.length()==0 ? urls[i] : ", "+urls[i];313 }314 copyright.setText(license);315 copyright.setCaretPosition(0);316 }317 });318 319 authorActionListener.actionPerformed(null);320 authorNameListener.keyReleased(null);321 }322 323 private static void enableCopyright(final JTextField copyright, final JButton predefined, final JTextField copyrightYear, final JLabel copyrightLabel, final JLabel copyrightYearLabel, final JLabel warning, boolean enable) {324 copyright.setEnabled(enable);325 predefined.setEnabled(enable);326 copyrightYear.setEnabled(enable);327 copyrightLabel.setEnabled(enable);328 copyrightYearLabel.setEnabled(enable);329 warning.setText(enable ? warningGpl : "<html><font size='-2'> </html");330 331 if (enable && copyrightYear.getText().length()==0) {332 copyrightYear.setText(enable ? Integer.toString(Calendar.getInstance().get(Calendar.YEAR)) : "");333 } else if (!enable) {334 copyrightYear.setText("");335 }336 337 if (enable && copyright.getText().length()==0) {338 copyright.setText(enable ? Main.pref.get("lastCopyright", "http://creativecommons.org/licenses/by-sa/2.5") : "");339 copyright.setCaretPosition(0);340 } else if (!enable) {341 copyright.setText("");342 87 } 343 88 } … … 345 90 /** 346 91 * Refreshes the enabled state 347 * 92 * 348 93 */ 349 94 @Override -
trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java
r1907 r1949 6 6 import java.awt.event.ActionEvent; 7 7 import java.io.File; 8 import java.io.FileInputStream;9 import java.io.FileNotFoundException;10 import java.io.FileOutputStream;11 8 import java.io.IOException; 12 import java.io.OutputStream; 13 import java.io.OutputStreamWriter; 14 import java.io.PrintWriter; 15 import java.io.Writer; 16 import java.util.zip.GZIPOutputStream; 17 9 10 import javax.swing.JFileChooser; 18 11 import javax.swing.JOptionPane; 19 20 import org.apache.tools.bzip2.CBZip2OutputStream; 12 import javax.swing.filechooser.FileFilter; 13 21 14 import org.openstreetmap.josm.Main; 22 15 import org.openstreetmap.josm.data.conflict.ConflictCollection; … … 27 20 import org.openstreetmap.josm.gui.layer.Layer; 28 21 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 29 import org.openstreetmap.josm.io.GpxImporter; 30 import org.openstreetmap.josm.io.GpxWriter; 31 import org.openstreetmap.josm.io.OsmBzip2Importer; 32 import org.openstreetmap.josm.io.OsmGzipImporter; 33 import org.openstreetmap.josm.io.OsmImporter; 34 import org.openstreetmap.josm.io.OsmWriter; 22 import org.openstreetmap.josm.io.FileExporter; 35 23 import org.openstreetmap.josm.tools.Shortcut; 36 24 … … 70 58 return false; 71 59 72 save(file, layer); 73 74 layer.setName(file.getName()); 75 layer.setAssociatedFile(file); 76 Main.parent.repaint(); 60 try { 61 boolean exported = false; 62 for (FileExporter exporter : ExtensionFileFilter.exporters) { 63 if (exporter.acceptFile(file, layer)) { 64 exporter.exportData(file, layer); 65 exported = true; 66 } 67 } 68 if (!exported) { 69 OptionPaneUtil.showMessageDialog(Main.parent, tr("No Exporter found! Nothing saved."), tr("Warning"), 70 JOptionPane.WARNING_MESSAGE); 71 return false; 72 } 73 layer.setName(file.getName()); 74 layer.setAssociatedFile(file); 75 Main.parent.repaint(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 return false; 79 } 77 80 return true; 78 81 } … … 132 135 } 133 136 134 private static void copy(File src, File dst) throws IOException {135 FileInputStream srcStream;136 FileOutputStream dstStream;137 try {138 srcStream = new FileInputStream(src);139 dstStream = new FileOutputStream(dst);140 } catch (FileNotFoundException e) {141 OptionPaneUtil.showMessageDialog(142 Main.parent,143 tr("Could not back up file. Exception is: {0}", e.getMessage()),144 tr("Error"),145 JOptionPane.ERROR_MESSAGE146 );147 return;148 }149 byte buf[] = new byte[1<<16];150 int len;151 while ((len = srcStream.read(buf)) != -1) {152 dstStream.write(buf, 0, len);153 }154 srcStream.close();155 dstStream.close();156 }157 158 public static void save(File file, Layer layer) {159 if (layer instanceof GpxLayer) {160 save(file, (GpxLayer)layer);161 ((GpxLayer)layer).data.storageFile = file;162 } else if (layer instanceof OsmDataLayer) {163 save(file, (OsmDataLayer)layer);164 }165 }166 167 public static void save(File file, OsmDataLayer layer) {168 File tmpFile = null;169 try {170 GpxImporter gpxImExporter = new GpxImporter();171 OsmImporter osmImExporter = new OsmImporter();172 OsmGzipImporter osmGzipImporter = new OsmGzipImporter();173 OsmBzip2Importer osmBzip2Importer = new OsmBzip2Importer();174 if (gpxImExporter.acceptFile(file)) {175 new GpxExportAction().exportGpx(file, layer);176 } else if (osmImExporter.acceptFile(file)177 || osmGzipImporter.acceptFile(file)178 || osmBzip2Importer.acceptFile(file))179 {180 // use a tmp file because if something errors out in the181 // process of writing the file, we might just end up with182 // a truncated file. That can destroy lots of work.183 if (file.exists()) {184 tmpFile = new File(file.getPath() + "~");185 copy(file, tmpFile);186 }187 188 // create outputstream and wrap it with gzip or bzip, if necessary189 OutputStream out = new FileOutputStream(file);190 if(osmGzipImporter.acceptFile(file)) {191 out = new GZIPOutputStream(out);192 } else if(osmBzip2Importer.acceptFile(file)) {193 out.write('B');194 out.write('Z');195 out = new CBZip2OutputStream(out);196 }197 Writer writer = new OutputStreamWriter(out, "UTF-8");198 199 OsmWriter w = new OsmWriter(new PrintWriter(writer), false, layer.data.version);200 w.header();201 w.writeDataSources(layer.data);202 w.writeContent(layer.data);203 w.footer();204 w.close();205 // FIXME - how to close?206 if (!Main.pref.getBoolean("save.keepbackup") && (tmpFile != null)) {207 tmpFile.delete();208 }209 } else {210 OptionPaneUtil.showMessageDialog(211 Main.parent,212 tr("Unknown file extension for file ''{0}''", file.toString()),213 tr("Error"),214 JOptionPane.ERROR_MESSAGE215 );216 return;217 }218 layer.cleanupAfterSaveToDisk();219 } catch (IOException e) {220 e.printStackTrace();221 OptionPaneUtil.showMessageDialog(222 Main.parent,223 tr("<html>An error occurred while saving.<br>Error is: <br>{0}</html>", e.getMessage()),224 tr("Error"),225 JOptionPane.ERROR_MESSAGE226 );227 228 try {229 // if the file save failed, then the tempfile will not230 // be deleted. So, restore the backup if we made one.231 if (tmpFile != null && tmpFile.exists()) {232 copy(tmpFile, file);233 }234 } catch (IOException e2) {235 e2.printStackTrace();236 OptionPaneUtil.showMessageDialog(237 Main.parent,238 tr("<html>An error occurred while restoring backup file.<br>Error is: <br>{0}</html>", e2.getMessage()),239 tr("Error"),240 JOptionPane.ERROR_MESSAGE241 );242 }243 }244 }245 246 public static void save(File file, GpxLayer layer) {247 File tmpFile = null;248 try {249 GpxImporter gpxImExporter = new GpxImporter();250 if (gpxImExporter.acceptFile(file)) {251 252 // use a tmp file because if something errors out in the253 // process of writing the file, we might just end up with254 // a truncated file. That can destroy lots of work.255 if (file.exists()) {256 tmpFile = new File(file.getPath() + "~");257 copy(file, tmpFile);258 }259 FileOutputStream fo = new FileOutputStream(file);260 new GpxWriter(fo).write(layer.data);261 fo.flush();262 fo.close();263 264 if (!Main.pref.getBoolean("save.keepbackup") && (tmpFile != null)) {265 tmpFile.delete();266 }267 } else {268 OptionPaneUtil.showMessageDialog(269 Main.parent,270 tr("Unknown file extension."),271 tr("Error"),272 JOptionPane.ERROR_MESSAGE273 );274 return;275 }276 } catch (IOException e) {277 e.printStackTrace();278 OptionPaneUtil.showMessageDialog(279 Main.parent,280 tr("An error occurred while saving. Error is: {0}", e.getMessage()),281 tr("Error"),282 JOptionPane.ERROR_MESSAGE283 );284 }285 try {286 // if the file save failed, then the tempfile will not287 // be deleted. So, restore the backup if we made one.288 if (tmpFile != null && tmpFile.exists()) {289 copy(tmpFile, file);290 }291 } catch (IOException e) {292 e.printStackTrace();293 OptionPaneUtil.showMessageDialog(294 Main.parent,295 tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>", e.getMessage()),296 tr("Error"),297 JOptionPane.ERROR_MESSAGE298 );;299 }300 }301 137 302 138 /** … … 316 152 /** 317 153 * Refreshes the enabled state 318 * 154 * 319 155 */ 320 156 @Override … … 334 170 setEnabled(layer instanceof OsmDataLayer || layer instanceof GpxLayer); 335 171 } 172 173 public static File createAndOpenSaveFileChooser(String title, String extension) { 174 String curDir = Main.pref.get("lastDirectory"); 175 if (curDir.equals("")) { 176 curDir = "."; 177 } 178 JFileChooser fc = new JFileChooser(new File(curDir)); 179 if (title != null) { 180 fc.setDialogTitle(title); 181 } 182 183 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 184 fc.setMultiSelectionEnabled(false); 185 fc.setAcceptAllFileFilterUsed(false); 186 187 FileFilter defaultFilter = null; 188 for (FileExporter exporter : ExtensionFileFilter.exporters) { 189 fc.addChoosableFileFilter(exporter.filter); 190 if (extension.endsWith(exporter.filter.defaultExtension)) { 191 defaultFilter = exporter.filter; 192 } 193 } 194 if (defaultFilter != null) { 195 fc.setFileFilter(defaultFilter); 196 } 197 198 int answer = fc.showSaveDialog(Main.parent); 199 if (answer != JFileChooser.APPROVE_OPTION) 200 return null; 201 202 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) { 203 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); 204 } 205 206 File file = fc.getSelectedFile(); 207 if(extension != null){ 208 String fn = file.getPath(); 209 if(fn.indexOf('.') == -1) 210 { 211 FileFilter ff = fc.getFileFilter(); 212 if (ff instanceof ExtensionFileFilter) { 213 fn += "." + ((ExtensionFileFilter)ff).defaultExtension; 214 } else { 215 fn += extension; 216 } 217 file = new File(fn); 218 } 219 } 220 if(file == null || (file.exists() && 1 != new ExtendedDialog(Main.parent, 221 tr("Overwrite"), tr("File exists. Overwrite?"), 222 new String[] {tr("Overwrite"), tr("Cancel")}, 223 new String[] {"save_as.png", "cancel.png"}).getValue())) 224 return null; 225 return file; 226 } 336 227 }
Note:
See TracChangeset
for help on using the changeset viewer.