1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
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.FileInputStream;
|
---|
9 | import java.io.FileNotFoundException;
|
---|
10 | import java.io.FileOutputStream;
|
---|
11 | 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 |
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 |
|
---|
20 | import org.apache.tools.bzip2.CBZip2OutputStream;
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
25 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
26 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
27 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
28 | 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;
|
---|
35 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
36 |
|
---|
37 | public abstract class SaveActionBase extends DiskAccessAction {
|
---|
38 |
|
---|
39 | public SaveActionBase(String name, String iconName, String tooltip, Shortcut shortcut) {
|
---|
40 | super(name, iconName, tooltip, shortcut);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void actionPerformed(ActionEvent e) {
|
---|
44 | if (!isEnabled())
|
---|
45 | return;
|
---|
46 | doSave();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public boolean doSave() {
|
---|
50 | Layer layer = null;
|
---|
51 | if (layer == null && Main.map != null && (Main.map.mapView.getActiveLayer() instanceof OsmDataLayer
|
---|
52 | || Main.map.mapView.getActiveLayer() instanceof GpxLayer)) {
|
---|
53 | layer = Main.map.mapView.getActiveLayer();
|
---|
54 | }
|
---|
55 | if (layer == null)
|
---|
56 | return false;
|
---|
57 | return doSave(layer);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public boolean doSave(Layer layer) {
|
---|
61 | if (layer == null)
|
---|
62 | return false;
|
---|
63 | if ( !(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
|
---|
64 | return false;
|
---|
65 | if (!checkSaveConditions(layer))
|
---|
66 | return false;
|
---|
67 |
|
---|
68 | File file = getFile(layer);
|
---|
69 | if (file == null)
|
---|
70 | return false;
|
---|
71 |
|
---|
72 | save(file, layer);
|
---|
73 |
|
---|
74 | layer.setName(file.getName());
|
---|
75 | layer.setAssociatedFile(file);
|
---|
76 | Main.parent.repaint();
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected abstract File getFile(Layer layer);
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Checks whether it is ok to launch a save (whether we have data,
|
---|
84 | * there is no conflict etc.)
|
---|
85 | * @return <code>true</code>, if it is safe to save.
|
---|
86 | */
|
---|
87 | public boolean checkSaveConditions(Layer layer) {
|
---|
88 | if (layer == null) {
|
---|
89 | OptionPaneUtil.showMessageDialog(
|
---|
90 | Main.parent,
|
---|
91 | tr("Internal Error: cannot check conditions for no layer. Please report this as a bug."),
|
---|
92 | tr("Error"),
|
---|
93 | JOptionPane.ERROR_MESSAGE
|
---|
94 | );
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 | if (Main.map == null) {
|
---|
98 | OptionPaneUtil.showMessageDialog(
|
---|
99 | Main.parent,
|
---|
100 | tr("No document open so nothing to save."),
|
---|
101 | tr("Warning"),
|
---|
102 | JOptionPane.WARNING_MESSAGE
|
---|
103 | );
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (layer instanceof OsmDataLayer && isDataSetEmpty((OsmDataLayer)layer) && 1 != new ExtendedDialog(Main.parent, tr("Empty document"), tr("The document contains no data."), new String[] {tr("Save anyway"), tr("Cancel")}, new String[] {"save.png", "cancel.png"}).getValue())
|
---|
108 | return false;
|
---|
109 | if (layer instanceof GpxLayer && ((GpxLayer)layer).data == null)
|
---|
110 | return false;
|
---|
111 | if (layer instanceof OsmDataLayer) {
|
---|
112 | ConflictCollection conflicts = ((OsmDataLayer)layer).getConflicts();
|
---|
113 | if (conflicts != null && !conflicts.isEmpty()) {
|
---|
114 | int answer = new ExtendedDialog(Main.parent,
|
---|
115 | tr("Conflicts"),
|
---|
116 | tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"),
|
---|
117 | new String[] {tr("Reject Conflicts and Save"), tr("Cancel")},
|
---|
118 | new String[] {"save.png", "cancel.png"}).getValue();
|
---|
119 |
|
---|
120 | if (answer != 1) return false;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public static File openFileDialog(Layer layer) {
|
---|
127 | if (layer instanceof OsmDataLayer)
|
---|
128 | return createAndOpenSaveFileChooser(tr("Save OSM file"), ".osm");
|
---|
129 | else if (layer instanceof GpxLayer)
|
---|
130 | return createAndOpenSaveFileChooser(tr("Save GPX file"), ".gpx");
|
---|
131 | return createAndOpenSaveFileChooser(tr("Save Layer"), ".lay");
|
---|
132 | }
|
---|
133 |
|
---|
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_MESSAGE
|
---|
146 | );
|
---|
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 the
|
---|
181 | // process of writing the file, we might just end up with
|
---|
182 | // 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 necessary
|
---|
189 | 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_MESSAGE
|
---|
215 | );
|
---|
216 | return;
|
---|
217 | }
|
---|
218 | layer.cleanData(null, false);
|
---|
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_MESSAGE
|
---|
226 | );
|
---|
227 |
|
---|
228 | try {
|
---|
229 | // if the file save failed, then the tempfile will not
|
---|
230 | // 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_MESSAGE
|
---|
241 | );
|
---|
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 the
|
---|
253 | // process of writing the file, we might just end up with
|
---|
254 | // 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_MESSAGE
|
---|
273 | );
|
---|
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_MESSAGE
|
---|
283 | );
|
---|
284 | }
|
---|
285 | try {
|
---|
286 | // if the file save failed, then the tempfile will not
|
---|
287 | // 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_MESSAGE
|
---|
298 | );;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Check the data set if it would be empty on save. It is empty, if it contains
|
---|
304 | * no objects (after all objects that are created and deleted without being
|
---|
305 | * transferred to the server have been removed).
|
---|
306 | *
|
---|
307 | * @return <code>true</code>, if a save result in an empty data set.
|
---|
308 | */
|
---|
309 | private boolean isDataSetEmpty(OsmDataLayer layer) {
|
---|
310 | for (OsmPrimitive osm : layer.data.allNonDeletedPrimitives())
|
---|
311 | if (!osm.deleted || osm.id > 0)
|
---|
312 | return false;
|
---|
313 | return true;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Refreshes the enabled state
|
---|
318 | *
|
---|
319 | */
|
---|
320 | @Override
|
---|
321 | protected void updateEnabledState() {
|
---|
322 | if (Main.applet) {
|
---|
323 | setEnabled(false);
|
---|
324 | return;
|
---|
325 | }
|
---|
326 | boolean check = Main.map != null
|
---|
327 | && Main.map.mapView !=null
|
---|
328 | && Main.map.mapView.getActiveLayer() != null;
|
---|
329 | if(!check) {
|
---|
330 | setEnabled(false);
|
---|
331 | return;
|
---|
332 | }
|
---|
333 | Layer layer = Main.map.mapView.getActiveLayer();
|
---|
334 | setEnabled(layer instanceof OsmDataLayer || layer instanceof GpxLayer);
|
---|
335 | }
|
---|
336 | }
|
---|