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

Last change on this file since 2017 was 2017, checked in by Gubaer, 15 years ago

removed OptionPaneUtil
cleanup of deprecated Layer API
cleanup of deprecated APIs in OsmPrimitive and Way
cleanup of imports

  • Property svn:eol-style set to native
File size: 8.0 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 (layer == null && Main.map != null && (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 Main.parent.repaint();
75 } catch (IOException e) {
76 e.printStackTrace();
77 return false;
78 }
79 return true;
80 }
81
82 protected abstract File getFile(Layer layer);
83
84 /**
85 * Checks whether it is ok to launch a save (whether we have data,
86 * there is no conflict etc.)
87 * @return <code>true</code>, if it is safe to save.
88 */
89 public boolean checkSaveConditions(Layer layer) {
90 if (layer == null) {
91 JOptionPane.showMessageDialog(
92 Main.parent,
93 tr("Internal Error: cannot check conditions for no layer. Please report this as a bug."),
94 tr("Error"),
95 JOptionPane.ERROR_MESSAGE
96 );
97 return false;
98 }
99 if (Main.map == null) {
100 JOptionPane.showMessageDialog(
101 Main.parent,
102 tr("No document open so nothing to save."),
103 tr("Warning"),
104 JOptionPane.WARNING_MESSAGE
105 );
106 return false;
107 }
108
109 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())
110 return false;
111 if (layer instanceof GpxLayer && ((GpxLayer)layer).data == null)
112 return false;
113 if (layer instanceof OsmDataLayer) {
114 ConflictCollection conflicts = ((OsmDataLayer)layer).getConflicts();
115 if (conflicts != null && !conflicts.isEmpty()) {
116 int answer = new ExtendedDialog(Main.parent,
117 tr("Conflicts"),
118 tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"),
119 new String[] {tr("Reject Conflicts and Save"), tr("Cancel")},
120 new String[] {"save.png", "cancel.png"}).getValue();
121
122 if (answer != 1) return false;
123 }
124 }
125 return true;
126 }
127
128 public static File openFileDialog(Layer layer) {
129 if (layer instanceof OsmDataLayer)
130 return createAndOpenSaveFileChooser(tr("Save OSM file"), ".osm");
131 else if (layer instanceof GpxLayer)
132 return createAndOpenSaveFileChooser(tr("Save GPX file"), ".gpx");
133 return createAndOpenSaveFileChooser(tr("Save Layer"), ".lay");
134 }
135
136
137 /**
138 * Check the data set if it would be empty on save. It is empty, if it contains
139 * no objects (after all objects that are created and deleted without being
140 * transferred to the server have been removed).
141 *
142 * @return <code>true</code>, if a save result in an empty data set.
143 */
144 private boolean isDataSetEmpty(OsmDataLayer layer) {
145 for (OsmPrimitive osm : layer.data.allNonDeletedPrimitives())
146 if (!osm.deleted || osm.id > 0)
147 return false;
148 return true;
149 }
150
151 /**
152 * Refreshes the enabled state
153 *
154 */
155 @Override
156 protected void updateEnabledState() {
157 if (Main.applet) {
158 setEnabled(false);
159 return;
160 }
161 boolean check = Main.map != null
162 && Main.map.mapView !=null
163 && Main.map.mapView.getActiveLayer() != null;
164 if(!check) {
165 setEnabled(false);
166 return;
167 }
168 Layer layer = Main.map.mapView.getActiveLayer();
169 setEnabled(layer instanceof OsmDataLayer || layer instanceof GpxLayer);
170 }
171
172 public static File createAndOpenSaveFileChooser(String title, String extension) {
173 String curDir = Main.pref.get("lastDirectory");
174 if (curDir.equals("")) {
175 curDir = ".";
176 }
177 JFileChooser fc = new JFileChooser(new File(curDir));
178 if (title != null) {
179 fc.setDialogTitle(title);
180 }
181
182 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
183 fc.setMultiSelectionEnabled(false);
184 fc.setAcceptAllFileFilterUsed(false);
185
186 FileFilter defaultFilter = null;
187 for (FileExporter exporter : ExtensionFileFilter.exporters) {
188 fc.addChoosableFileFilter(exporter.filter);
189 if (extension.endsWith(exporter.filter.defaultExtension)) {
190 defaultFilter = exporter.filter;
191 }
192 }
193 if (defaultFilter != null) {
194 fc.setFileFilter(defaultFilter);
195 }
196
197 int answer = fc.showSaveDialog(Main.parent);
198 if (answer != JFileChooser.APPROVE_OPTION)
199 return null;
200
201 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {
202 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
203 }
204
205 File file = fc.getSelectedFile();
206 if(extension != null){
207 String fn = file.getPath();
208 if(fn.indexOf('.') == -1)
209 {
210 FileFilter ff = fc.getFileFilter();
211 if (ff instanceof ExtensionFileFilter) {
212 fn += "." + ((ExtensionFileFilter)ff).defaultExtension;
213 } else {
214 fn += extension;
215 }
216 file = new File(fn);
217 }
218 }
219 if(file == null || (file.exists() && 1 != new ExtendedDialog(Main.parent,
220 tr("Overwrite"), tr("File exists. Overwrite?"),
221 new String[] {tr("Overwrite"), tr("Cancel")},
222 new String[] {"save_as.png", "cancel.png"}).getValue()))
223 return null;
224 return file;
225 }
226}
Note: See TracBrowser for help on using the repository browser.