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