source: josm/trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java@ 4404

Last change on this file since 4404 was 4374, checked in by bastiK, 13 years ago

no duplicate entries in file history

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.io.File;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.LinkedList;
11import java.util.List;
12import javax.swing.JFileChooser;
13import javax.swing.JOptionPane;
14import javax.swing.filechooser.FileFilter;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.conflict.ConflictCollection;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.gui.ExtendedDialog;
20import org.openstreetmap.josm.gui.layer.GpxLayer;
21import org.openstreetmap.josm.gui.layer.Layer;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.io.FileExporter;
24import org.openstreetmap.josm.tools.Shortcut;
25
26public abstract class SaveActionBase extends DiskAccessAction {
27 private File file;
28
29 public SaveActionBase(String name, String iconName, String tooltip, Shortcut shortcut) {
30 super(name, iconName, tooltip, shortcut);
31 }
32
33 @Override
34 public void actionPerformed(ActionEvent e) {
35 if (!isEnabled()) {
36 return;
37 }
38 boolean saved = doSave();
39 if (saved) {
40 addToFileOpenHistory();
41 }
42 }
43
44 public boolean doSave() {
45 Layer layer = null;
46 if (Main.isDisplayingMapView() && (Main.map.mapView.getActiveLayer() instanceof OsmDataLayer
47 || Main.map.mapView.getActiveLayer() instanceof GpxLayer)) {
48 layer = Main.map.mapView.getActiveLayer();
49 }
50 if (layer == null)
51 return false;
52 return doSave(layer);
53 }
54
55 public boolean doSave(Layer layer) {
56 if(!checkSaveConditions(layer))
57 return false;
58 file = getFile(layer);
59 return doInternalSave(layer, file);
60 }
61
62 public boolean doSave(Layer layer, File file) {
63 if(!checkSaveConditions(layer))
64 return false;
65 return doInternalSave(layer, file);
66 }
67
68 private boolean doInternalSave(Layer layer, File file) {
69 if (file == null)
70 return false;
71
72 try {
73 boolean exported = false;
74 for (FileExporter exporter : ExtensionFileFilter.exporters) {
75 if (exporter.acceptFile(file, layer)) {
76 exporter.exportData(file, layer);
77 exported = true;
78 }
79 }
80 if (!exported) {
81 JOptionPane.showMessageDialog(Main.parent, tr("No Exporter found! Nothing saved."), tr("Warning"),
82 JOptionPane.WARNING_MESSAGE);
83 return false;
84 }
85 layer.setName(file.getName());
86 layer.setAssociatedFile(file);
87 if (layer instanceof OsmDataLayer) {
88 ((OsmDataLayer) layer).onPostSaveToFile();
89 }
90 Main.parent.repaint();
91 } catch (IOException e) {
92 e.printStackTrace();
93 return false;
94 }
95 return true;
96 }
97
98 protected abstract File getFile(Layer layer);
99
100 /**
101 * Checks whether it is ok to launch a save (whether we have data,
102 * there is no conflict etc.)
103 * @return <code>true</code>, if it is safe to save.
104 */
105 public boolean checkSaveConditions(Layer layer) {
106 if (layer instanceof GpxLayer)
107 return ((GpxLayer)layer).data != null;
108 else if (layer instanceof OsmDataLayer) {
109 if (isDataSetEmpty((OsmDataLayer)layer)) {
110 ExtendedDialog dialog = new ExtendedDialog(
111 Main.parent,
112 tr("Empty document"),
113 new String[] {tr("Save anyway"), tr("Cancel")}
114 );
115 dialog.setContent(tr("The document contains no data."));
116 dialog.setButtonIcons(new String[] {"save.png", "cancel.png"});
117 dialog.showDialog();
118 if (dialog.getValue() != 1) return false;
119 }
120
121 ConflictCollection conflicts = ((OsmDataLayer)layer).getConflicts();
122 if (conflicts != null && !conflicts.isEmpty()) {
123 ExtendedDialog dialog = new ExtendedDialog(
124 Main.parent,
125 /* I18N: Display title of the window showing conflicts */
126 tr("Conflicts"),
127 new String[] {tr("Reject Conflicts and Save"), tr("Cancel")}
128 );
129 dialog.setContent(tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"));
130 dialog.setButtonIcons(new String[] {"save.png", "cancel.png"});
131 dialog.showDialog();
132 if (dialog.getValue() != 1) return false;
133 }
134 return true;
135 }
136 return false;
137 }
138
139 public static File openFileDialog(Layer layer) {
140 if (layer instanceof OsmDataLayer)
141 return createAndOpenSaveFileChooser(tr("Save OSM file"), "osm");
142 else if (layer instanceof GpxLayer)
143 return createAndOpenSaveFileChooser(tr("Save GPX file"), "gpx");
144 return createAndOpenSaveFileChooser(tr("Save Layer"), "lay");
145 }
146
147 /**
148 * Check the data set if it would be empty on save. It is empty, if it contains
149 * no objects (after all objects that are created and deleted without being
150 * transferred to the server have been removed).
151 *
152 * @return <code>true</code>, if a save result in an empty data set.
153 */
154 private boolean isDataSetEmpty(OsmDataLayer layer) {
155 for (OsmPrimitive osm : layer.data.allNonDeletedPrimitives())
156 if (!osm.isDeleted() || !osm.isNewOrUndeleted())
157 return false;
158 return true;
159 }
160
161 /**
162 * Refreshes the enabled state
163 *
164 */
165 @Override
166 protected void updateEnabledState() {
167 if (Main.applet) {
168 setEnabled(false);
169 return;
170 }
171 boolean check = Main.map != null
172 && Main.map.mapView !=null
173 && Main.map.mapView.getActiveLayer() != null;
174 if(!check) {
175 setEnabled(false);
176 return;
177 }
178 Layer layer = Main.map.mapView.getActiveLayer();
179 setEnabled(layer instanceof OsmDataLayer || layer instanceof GpxLayer);
180 }
181
182 public static File createAndOpenSaveFileChooser(String title, String extension) {
183 String curDir = Main.pref.get("lastDirectory");
184 if (curDir.equals("")) {
185 curDir = ".";
186 }
187 JFileChooser fc = new JFileChooser(new File(curDir));
188 if (title != null) {
189 fc.setDialogTitle(title);
190 }
191
192 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
193 fc.setMultiSelectionEnabled(false);
194 fc.setAcceptAllFileFilterUsed(false);
195 ExtensionFileFilter.applyChoosableExportFileFilters(fc, extension);
196 int answer = fc.showSaveDialog(Main.parent);
197 if (answer != JFileChooser.APPROVE_OPTION)
198 return null;
199
200 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {
201 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
202 }
203
204 File file = fc.getSelectedFile();
205 String fn = file.getPath();
206 if(fn.indexOf('.') == -1)
207 {
208 FileFilter ff = fc.getFileFilter();
209 if (ff instanceof ExtensionFileFilter) {
210 fn += "." + ((ExtensionFileFilter)ff).getDefaultExtension();
211 } else if(extension != null) {
212 fn += "." + extension;
213 }
214 file = new File(fn);
215 }
216 if (!confirmOverride(file))
217 return null;
218 return file;
219 }
220
221 public static boolean confirmOverride(File file) {
222 if (file == null || (file.exists())) {
223 ExtendedDialog dialog = new ExtendedDialog(
224 Main.parent,
225 tr("Overwrite"),
226 new String[] {tr("Overwrite"), tr("Cancel")}
227 );
228 dialog.setContent(tr("File exists. Overwrite?"));
229 dialog.setButtonIcons(new String[] {"save_as.png", "cancel.png"});
230 dialog.showDialog();
231 return (dialog.getValue() == 1);
232 }
233 return true;
234 }
235
236 protected void addToFileOpenHistory() {
237 String filepath;
238 try {
239 filepath = file.getCanonicalPath();
240 } catch (IOException ign) {
241 return;
242 }
243
244 int maxsize = Math.max(0, Main.pref.getInteger("file-open.history.max-size", 15));
245 Collection<String> oldHistory = Main.pref.getCollection("file-open.history");
246 List<String> history = new LinkedList<String>(oldHistory);
247 history.remove(filepath);
248 history.add(0, filepath);
249 Main.pref.putCollectionBounded("file-open.history", maxsize, history);
250 }
251}
Note: See TracBrowser for help on using the repository browser.