Ignore:
Timestamp:
2009-09-02T21:17:52+02:00 (15 years ago)
Author:
Gubaer
Message:

new: improved dialog for uploading/saving modified layers on exit
new: improved dialog for uploading/saving modified layers if layers are deleted
new: new progress monitor which can delegate rendering to any Swing component
more setters/getters for properties in OSM data classes (fields are @deprecated); started to update references in the code base

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r2017 r2025  
    22package org.openstreetmap.josm;
    33import static org.openstreetmap.josm.tools.I18n.tr;
     4import static org.openstreetmap.josm.tools.I18n.trn;
    45
    56import java.awt.BorderLayout;
     
    1213import java.net.URI;
    1314import java.net.URISyntaxException;
     15import java.util.ArrayList;
    1416import java.util.Collection;
     17import java.util.List;
    1518import java.util.Map;
    1619import java.util.StringTokenizer;
     
    2225import javax.swing.JComponent;
    2326import javax.swing.JFrame;
     27import javax.swing.JLabel;
    2428import javax.swing.JOptionPane;
    2529import javax.swing.JPanel;
     
    4549import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    4650import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
     51import org.openstreetmap.josm.gui.io.SaveLayersDialog;
    4752import org.openstreetmap.josm.gui.layer.Layer;
    4853import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    393398    }
    394399
    395     public static boolean breakBecauseUnsavedChanges() {
     400    public static boolean saveUnsavedModifications() {
     401        if (map == null) return true;
     402        SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
     403        List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<OsmDataLayer>();
     404        for (OsmDataLayer l: Main.map.mapView.getLayersOfType(OsmDataLayer.class)) {
     405            if (l.requiresSaveToFile() || l.requiresUploadToServer()) {
     406                layersWithUnmodifiedChanges.add(l);
     407            }
     408        }
     409        dialog.prepareForSavingAndUpdatingLayersBeforeExit();
     410        if (!layersWithUnmodifiedChanges.isEmpty()) {
     411            dialog.getModel().populate(layersWithUnmodifiedChanges);
     412            dialog.setVisible(true);
     413            switch(dialog.getUserAction()) {
     414                case CANCEL: return false;
     415                case PROCEED: return true;
     416                default: return false;
     417            }
     418        }
     419        return true;
     420    }
     421
     422    /**
     423     * Saves all {@see OsmDataLayer}s with an associated file and with unsaved
     424     * data modifications.
     425     *
     426     * @return true, if the save operation was successful; false, otherwise
     427     */
     428    public static boolean saveUnsavedModifications_old() {
    396429        Shortcut.savePrefs();
    397         if (map != null) {
    398             boolean modified = false;
    399             boolean uploadedModified = false;
    400             for (final Layer l : map.mapView.getAllLayers()) {
    401                 if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
    402                     modified = true;
    403                     uploadedModified = ((OsmDataLayer)l).uploadedModified;
    404                     break;
    405                 }
    406             }
    407             if (modified) {
    408                 final String msg = uploadedModified ? "\n"
    409                         +tr("Hint: Some changes came from uploading new data to the server.") : "";
    410                         int result = new ExtendedDialog(parent, tr("Unsaved Changes"),
    411                                 new javax.swing.JLabel(tr("There are unsaved changes. Discard the changes and continue?")+msg),
    412                                 new String[] {tr("Save and Exit"), tr("Discard and Exit"), tr("Cancel")},
    413                                 new String[] {"save.png", "exit.png", "cancel.png"}).getValue();
    414 
    415                         // Save before exiting
    416                         if(result == 1) {
    417                             Boolean savefailed = false;
    418                             for (final Layer l : map.mapView.getAllLayers()) {
    419                                 if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
    420                                     SaveAction save = new SaveAction();
    421                                     if(!save.doSave(l)) {
    422                                         savefailed = true;
    423                                     }
    424                                 }
    425                             }
    426                             return savefailed;
    427                         }
    428                         else if(result != 2) // Cancel exiting unless the 2nd button was clicked
    429                             return true;
    430             }
    431         }
    432         return false;
     430        if (map == null)
     431            return true; // nothing to save, return success
     432
     433        int numUnsavedLayers = 0;
     434        for (final OsmDataLayer l : map.mapView.getLayersOfType(OsmDataLayer.class)) {
     435            if (l.requiresSaveToFile()) {
     436                numUnsavedLayers++;
     437            }
     438        }
     439        if (numUnsavedLayers == 0)
     440            return true; // nothing to save, return success
     441
     442        String msg = trn(
     443                "There are unsaved changes in {0} layer. Discard the changes and continue?",
     444                "There are unsaved changes in {0} layers. Discard the changes and continue?",
     445                numUnsavedLayers,
     446                numUnsavedLayers
     447        );
     448        int result = new ExtendedDialog(parent, tr("Unsaved Changes"),
     449                new JLabel(msg),
     450                new String[] {tr("Save and Exit"), tr("Discard and Exit"), tr("Cancel")},
     451                new String[] {"save.png", "exit.png", "cancel.png"}).getValue();
     452
     453        switch(result) {
     454            case 2: /* discard and exit */ return true;
     455            case 3: /* cancel */ return false;
     456        }
     457        boolean savefailed = false;
     458        for (OsmDataLayer l : map.mapView.getLayersOfType(OsmDataLayer.class)) {
     459            if(!new SaveAction().doSave(l)) {
     460                savefailed = true;
     461            }
     462        }
     463        return !savefailed;
    433464    }
    434465
Note: See TracChangeset for help on using the changeset viewer.