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

Last change on this file since 4067 was 4045, checked in by stoecker, 13 years ago

fix #6097 - i18n issues

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