1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.hot.sds;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileOutputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.OutputStream;
|
---|
11 | import java.io.OutputStreamWriter;
|
---|
12 | import java.io.PrintWriter;
|
---|
13 | import java.io.Writer;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
21 | import org.openstreetmap.josm.tools.Logging;
|
---|
22 | import org.openstreetmap.josm.tools.Utils;
|
---|
23 |
|
---|
24 | public class SdsSaveAction extends SdsDiskAccessAction {
|
---|
25 |
|
---|
26 | public SdsSaveAction() {
|
---|
27 | super(tr("Save..."), "sds_save", tr("Save the current separate data store information to a file."),
|
---|
28 | null);
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public void actionPerformed(ActionEvent e) {
|
---|
33 | if (!isEnabled())
|
---|
34 | return;
|
---|
35 | doSave();
|
---|
36 | }
|
---|
37 |
|
---|
38 | public boolean doSave() {
|
---|
39 | Layer layer = null;
|
---|
40 | if (getLayerManager().getActiveLayer() instanceof OsmDataLayer)
|
---|
41 | layer = getLayerManager().getActiveLayer();
|
---|
42 |
|
---|
43 | if (layer == null)
|
---|
44 | return false;
|
---|
45 | return doSave(layer);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public boolean doSave(Layer iLayer) {
|
---|
49 | if (iLayer == null)
|
---|
50 | return false;
|
---|
51 | if (!(iLayer instanceof OsmDataLayer))
|
---|
52 | return false;
|
---|
53 | OsmDataLayer layer = (OsmDataLayer) iLayer;
|
---|
54 |
|
---|
55 | File file = createAndOpenSaveFileChooser(tr("Save SDS file"));
|
---|
56 |
|
---|
57 | if (file == null)
|
---|
58 | return false;
|
---|
59 |
|
---|
60 | File tmpFile = null;
|
---|
61 |
|
---|
62 | try {
|
---|
63 |
|
---|
64 | if (file.exists()) {
|
---|
65 | tmpFile = new File(file.getPath() + "~");
|
---|
66 | Utils.copyFile(file, tmpFile);
|
---|
67 | }
|
---|
68 |
|
---|
69 | OutputStream out = new FileOutputStream(file);
|
---|
70 | Writer writer = new OutputStreamWriter(out, "UTF-8");
|
---|
71 |
|
---|
72 | layer.data.getReadLock().lock();
|
---|
73 | try (SdsWriter w = new SdsWriter(new PrintWriter(writer))) {
|
---|
74 | w.header();
|
---|
75 | for (IPrimitive p : layer.data.allNonDeletedPrimitives()) {
|
---|
76 | w.write(p, p.getKeys());
|
---|
77 | }
|
---|
78 | w.footer();
|
---|
79 | } finally {
|
---|
80 | layer.data.getReadLock().unlock();
|
---|
81 | }
|
---|
82 | // FIXME - how to close?
|
---|
83 | if (!Main.pref.getBoolean("save.keepbackup", false) && (tmpFile != null)) {
|
---|
84 | tmpFile.delete();
|
---|
85 | }
|
---|
86 | } catch (IOException e) {
|
---|
87 | Logging.error(e);
|
---|
88 | JOptionPane.showMessageDialog(
|
---|
89 | Main.parent,
|
---|
90 | tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>", e.getMessage()),
|
---|
91 | tr("Error"),
|
---|
92 | JOptionPane.ERROR_MESSAGE
|
---|
93 | );
|
---|
94 |
|
---|
95 | try {
|
---|
96 | // if the file save failed, then the tempfile will not
|
---|
97 | // be deleted. So, restore the backup if we made one.
|
---|
98 | if (tmpFile != null && tmpFile.exists()) {
|
---|
99 | Utils.copyFile(tmpFile, file);
|
---|
100 | }
|
---|
101 | } catch (IOException e2) {
|
---|
102 | Logging.error(e2);
|
---|
103 | JOptionPane.showMessageDialog(
|
---|
104 | Main.parent,
|
---|
105 | tr("<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>", e2.getMessage()),
|
---|
106 | tr("Error"),
|
---|
107 | JOptionPane.ERROR_MESSAGE
|
---|
108 | );
|
---|
109 | }
|
---|
110 | }
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 | }
|
---|