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

Last change on this file since 3965 was 3863, checked in by bastiK, 13 years ago

extended mappaint style dialog

  • Property svn:eol-style set to native
File size: 7.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;
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 tr("Conflicts"),
113 new String[] {tr("Reject Conflicts and Save"), tr("Cancel")}
114 );
115 dialog.setContent(tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"));
116 dialog.setButtonIcons(new String[] {"save.png", "cancel.png"});
117 dialog.showDialog();
118 if (dialog.getValue() != 1) return false;
119 }
120 }
121 return true;
122 }
123
124 public static File openFileDialog(Layer layer) {
125 if (layer instanceof OsmDataLayer)
126 return createAndOpenSaveFileChooser(tr("Save OSM file"), "osm");
127 else if (layer instanceof GpxLayer)
128 return createAndOpenSaveFileChooser(tr("Save GPX file"), "gpx");
129 return createAndOpenSaveFileChooser(tr("Save Layer"), "lay");
130 }
131
132 /**
133 * Check the data set if it would be empty on save. It is empty, if it contains
134 * no objects (after all objects that are created and deleted without being
135 * transferred to the server have been removed).
136 *
137 * @return <code>true</code>, if a save result in an empty data set.
138 */
139 private boolean isDataSetEmpty(OsmDataLayer layer) {
140 for (OsmPrimitive osm : layer.data.allNonDeletedPrimitives())
141 if (!osm.isDeleted() || !osm.isNewOrUndeleted())
142 return false;
143 return true;
144 }
145
146 /**
147 * Refreshes the enabled state
148 *
149 */
150 @Override
151 protected void updateEnabledState() {
152 if (Main.applet) {
153 setEnabled(false);
154 return;
155 }
156 boolean check = Main.map != null
157 && Main.map.mapView !=null
158 && Main.map.mapView.getActiveLayer() != null;
159 if(!check) {
160 setEnabled(false);
161 return;
162 }
163 Layer layer = Main.map.mapView.getActiveLayer();
164 setEnabled(layer instanceof OsmDataLayer || layer instanceof GpxLayer);
165 }
166
167 public static File createAndOpenSaveFileChooser(String title, String extension) {
168 String curDir = Main.pref.get("lastDirectory");
169 if (curDir.equals("")) {
170 curDir = ".";
171 }
172 JFileChooser fc = new JFileChooser(new File(curDir));
173 if (title != null) {
174 fc.setDialogTitle(title);
175 }
176
177 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
178 fc.setMultiSelectionEnabled(false);
179 fc.setAcceptAllFileFilterUsed(false);
180 ExtensionFileFilter.applyChoosableExportFileFilters(fc, extension);
181 int answer = fc.showSaveDialog(Main.parent);
182 if (answer != JFileChooser.APPROVE_OPTION)
183 return null;
184
185 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {
186 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
187 }
188
189 File file = fc.getSelectedFile();
190 String fn = file.getPath();
191 if(fn.indexOf('.') == -1)
192 {
193 FileFilter ff = fc.getFileFilter();
194 if (ff instanceof ExtensionFileFilter) {
195 fn += "." + ((ExtensionFileFilter)ff).getDefaultExtension();
196 } else if(extension != null) {
197 fn += "." + extension;
198 }
199 file = new File(fn);
200 }
201 if (!confirmOverride(file))
202 return null;
203 return file;
204 }
205
206 public static boolean confirmOverride(File file) {
207 if (file == null || (file.exists())) {
208 ExtendedDialog dialog = new ExtendedDialog(
209 Main.parent,
210 tr("Overwrite"),
211 new String[] {tr("Overwrite"), tr("Cancel")}
212 );
213 dialog.setContent(tr("File exists. Overwrite?"));
214 dialog.setButtonIcons(new String[] {"save_as.png", "cancel.png"});
215 dialog.showDialog();
216 return (dialog.getValue() == 1);
217 }
218 return true;
219 }
220}
Note: See TracBrowser for help on using the repository browser.