1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 |
|
---|
10 | import javax.swing.JFileChooser;
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 | import javax.swing.filechooser.FileFilter;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 | import org.openstreetmap.josm.io.OsmWriter;
|
---|
18 |
|
---|
19 | public abstract class SaveActionBase extends DiskAccessAction {
|
---|
20 |
|
---|
21 | private OsmDataLayer layer;
|
---|
22 |
|
---|
23 | public SaveActionBase(String name, String iconName, String tooltip, int shortCut, int modifiers, OsmDataLayer layer) {
|
---|
24 | super(name, iconName, tooltip, shortCut, modifiers);
|
---|
25 | this.layer = layer;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public void actionPerformed(ActionEvent e) {
|
---|
29 | OsmDataLayer layer = this.layer;
|
---|
30 | if (layer == null && Main.map != null && Main.map.mapView.getActiveLayer() instanceof OsmDataLayer)
|
---|
31 | layer = (OsmDataLayer)Main.map.mapView.getActiveLayer();
|
---|
32 | if (layer == null)
|
---|
33 | layer = Main.main.editLayer();
|
---|
34 |
|
---|
35 | if (!checkSaveConditions(layer))
|
---|
36 | return;
|
---|
37 |
|
---|
38 |
|
---|
39 | File file = getFile(layer);
|
---|
40 | if (file == null)
|
---|
41 | return;
|
---|
42 |
|
---|
43 | save(file, layer);
|
---|
44 |
|
---|
45 | layer.name = file.getName();
|
---|
46 | layer.associatedFile = file;
|
---|
47 | Main.parent.repaint();
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected abstract File getFile(OsmDataLayer layer);
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Checks whether it is ok to launch a save (whether we have data,
|
---|
54 | * there is no conflict etc...)
|
---|
55 | * @return <code>true</code>, if it is save to save.
|
---|
56 | */
|
---|
57 | public boolean checkSaveConditions(OsmDataLayer layer) {
|
---|
58 | if (Main.map == null) {
|
---|
59 | JOptionPane.showMessageDialog(Main.parent, tr("No document open so nothing to save."));
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 | if (isDataSetEmpty(layer) && JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(Main.parent,tr("The document contains no data. Save anyway?"), tr("Empty document"), JOptionPane.YES_NO_OPTION))
|
---|
63 | return false;
|
---|
64 | if (!Main.map.conflictDialog.conflicts.isEmpty()) {
|
---|
65 | int answer = JOptionPane.showConfirmDialog(Main.parent,
|
---|
66 | tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"),tr("Conflicts"), JOptionPane.YES_NO_OPTION);
|
---|
67 | if (answer != JOptionPane.YES_OPTION)
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public static File openFileDialog() {
|
---|
74 | JFileChooser fc = createAndOpenFileChooser(false, false);
|
---|
75 | if (fc == null)
|
---|
76 | return null;
|
---|
77 |
|
---|
78 | File file = fc.getSelectedFile();
|
---|
79 |
|
---|
80 | String fn = file.getPath();
|
---|
81 | if (fn.indexOf('.') == -1) {
|
---|
82 | FileFilter ff = fc.getFileFilter();
|
---|
83 | if (ff instanceof ExtensionFileFilter)
|
---|
84 | fn = "." + ((ExtensionFileFilter)ff).defaultExtension;
|
---|
85 | else
|
---|
86 | fn += ".osm";
|
---|
87 | file = new File(fn);
|
---|
88 | }
|
---|
89 | return file;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public static void save(File file, OsmDataLayer layer) {
|
---|
93 | try {
|
---|
94 | if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(file.getPath())) {
|
---|
95 | GpxExportAction.exportGpx(file, layer);
|
---|
96 | } else if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(file.getPath())) {
|
---|
97 | OsmWriter.output(new FileOutputStream(file), new OsmWriter.All(layer.data, false));
|
---|
98 | } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(file.getPath())) {
|
---|
99 | JOptionPane.showMessageDialog(Main.parent, tr("CSV output not supported yet."));
|
---|
100 | return;
|
---|
101 | } else {
|
---|
102 | JOptionPane.showMessageDialog(Main.parent, tr("Unknown file extension."));
|
---|
103 | return;
|
---|
104 | }
|
---|
105 | ((OsmDataLayer)layer).cleanData(null, false);
|
---|
106 | } catch (IOException e) {
|
---|
107 | e.printStackTrace();
|
---|
108 | JOptionPane.showMessageDialog(Main.parent, tr("An error occurred while saving.")+"\n"+e.getMessage());
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Check the data set if it would be empty on save. It is empty, if it contains
|
---|
114 | * no objects (after all objects that are created and deleted without beeing
|
---|
115 | * transfered to the server have been removed).
|
---|
116 | *
|
---|
117 | * @return <code>true</code>, if a save result in an empty data set.
|
---|
118 | */
|
---|
119 | private boolean isDataSetEmpty(OsmDataLayer layer) {
|
---|
120 | for (OsmPrimitive osm : layer.data.allNonDeletedPrimitives())
|
---|
121 | if (!osm.deleted || osm.id > 0)
|
---|
122 | return false;
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 | }
|
---|