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