1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileWriter;
|
---|
7 | import java.io.IOException;
|
---|
8 |
|
---|
9 | import javax.swing.AbstractAction;
|
---|
10 | import javax.swing.JFileChooser;
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.gui.ImageProvider;
|
---|
15 | import org.openstreetmap.josm.io.GpxWriter;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Export the current selected window's DataSet as gpx values. Remember, that some
|
---|
19 | * information could be lost. If so, an information message will ask the user to proceed.
|
---|
20 | * (Means, if all information can be saved, no warning message appears).
|
---|
21 | *
|
---|
22 | * @author imi
|
---|
23 | */
|
---|
24 | public class SaveGpxAction extends AbstractAction {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Construct the action with "Save GPX" as label.
|
---|
28 | */
|
---|
29 | public SaveGpxAction() {
|
---|
30 | super("Save GPX", ImageProvider.get("savegpx"));
|
---|
31 | putValue(MNEMONIC_KEY, KeyEvent.VK_S);
|
---|
32 | putValue(SHORT_DESCRIPTION, "Save the current active layer as GPX file.");
|
---|
33 | }
|
---|
34 |
|
---|
35 | @SuppressWarnings("unchecked")
|
---|
36 | public void actionPerformed(ActionEvent event) {
|
---|
37 | if (Main.main.getMapFrame() == null) {
|
---|
38 | JOptionPane.showMessageDialog(Main.main, "No document open so nothing to save.");
|
---|
39 | return;
|
---|
40 | }
|
---|
41 | JFileChooser fc = new JFileChooser("data");
|
---|
42 | fc.showSaveDialog(Main.main);
|
---|
43 | File gpxFile = fc.getSelectedFile();
|
---|
44 | if (gpxFile == null)
|
---|
45 | return;
|
---|
46 |
|
---|
47 | try {
|
---|
48 | FileWriter fileWriter = new FileWriter(gpxFile);
|
---|
49 | GpxWriter out = new GpxWriter(fileWriter);
|
---|
50 | out.output();
|
---|
51 | fileWriter.close();
|
---|
52 | } catch (IOException e) {
|
---|
53 | e.printStackTrace();
|
---|
54 | JOptionPane.showMessageDialog(Main.main, "An error occoured while saving.\n"+e.getMessage());
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | }
|
---|