Ignore:
Timestamp:
2016-10-07T21:55:54+02:00 (3 years ago)
Author:
simon04
Message:

fix #13320 - Use restart text and icon in unsaved changes dialog

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    r11017 r11093  
    1919import java.beans.PropertyChangeEvent;
    2020import java.beans.PropertyChangeListener;
     21import java.util.ArrayList;
    2122import java.util.List;
    2223import java.util.concurrent.CancellationException;
     
    4748import org.openstreetmap.josm.gui.io.SaveLayersModel.Mode;
    4849import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer;
     50import org.openstreetmap.josm.gui.layer.Layer;
    4951import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    5052import org.openstreetmap.josm.gui.progress.SwingRenderingProgressMonitor;
     
    5860
    5961public class SaveLayersDialog extends JDialog implements TableModelListener {
    60     public enum UserAction {
     62
     63    /**
     64     * The cause for requesting an action on unsaved modifications
     65     */
     66    public enum Reason {
     67        /** deleting a layer */
     68        DELETE,
     69        /** exiting JOSM */
     70        EXIT,
     71        /* restarting JOSM */
     72        RESTART
     73    }
     74
     75    private enum UserAction {
    6176        /** save/upload layers was successful, proceed with operation */
    6277        PROCEED,
     
    7691
    7792    private final JButton saveAndProceedActionButton = new JButton(saveAndProceedAction);
     93
     94    /**
     95     * Asks user to perform "save layer" operations (save on disk and/or upload data to server) before data layers deletion.
     96     *
     97     * @param selectedLayers The layers to check. Only instances of {@link AbstractModifiableLayer} are considered.
     98     * @param reason the cause for requesting an action on unsaved modifications
     99     * @return {@code true} if there was nothing to save, or if the user wants to proceed to save operations.
     100     *         {@code false} if the user cancels.
     101     * @since 11093
     102     */
     103    public static boolean saveUnsavedModifications(Iterable<? extends Layer> selectedLayers, Reason reason) {
     104        SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
     105        List<AbstractModifiableLayer> layersWithUnmodifiedChanges = new ArrayList<>();
     106        for (Layer l: selectedLayers) {
     107            if (!(l instanceof AbstractModifiableLayer)) {
     108                continue;
     109            }
     110            AbstractModifiableLayer odl = (AbstractModifiableLayer) l;
     111            if (odl.isModified() &&
     112                    ((!odl.isSavable() && !odl.isUploadable()) ||
     113                            odl.requiresSaveToFile() ||
     114                            (odl.requiresUploadToServer() && !odl.isUploadDiscouraged()))) {
     115                layersWithUnmodifiedChanges.add(odl);
     116            }
     117        }
     118        dialog.prepareForSavingAndUpdatingLayers(reason);
     119        if (!layersWithUnmodifiedChanges.isEmpty()) {
     120            dialog.getModel().populate(layersWithUnmodifiedChanges);
     121            dialog.setVisible(true);
     122            switch(dialog.getUserAction()) {
     123                case PROCEED: return true;
     124                case CANCEL:
     125                default: return false;
     126            }
     127        }
     128
     129        return true;
     130    }
    78131
    79132    /**
     
    131184    }
    132185
    133     public void prepareForSavingAndUpdatingLayersBeforeExit() {
    134         setTitle(tr("Unsaved changes - Save/Upload before exiting?"));
    135         this.saveAndProceedAction.initForSaveAndExit();
    136         this.discardAndProceedAction.initForDiscardAndExit();
    137     }
    138 
    139     public void prepareForSavingAndUpdatingLayersBeforeDelete() {
    140         setTitle(tr("Unsaved changes - Save/Upload before deleting?"));
    141         this.saveAndProceedAction.initForSaveAndDelete();
    142         this.discardAndProceedAction.initForDiscardAndDelete();
     186    public void prepareForSavingAndUpdatingLayers(final Reason reason) {
     187        switch (reason) {
     188            case EXIT:
     189                setTitle(tr("Unsaved changes - Save/Upload before exiting?"));
     190                break;
     191            case DELETE:
     192                setTitle(tr("Unsaved changes - Save/Upload before deleting?"));
     193                break;
     194            case RESTART:
     195                setTitle(tr("Unsaved changes - Save/Upload before restarting?"));
     196                break;
     197        }
     198        this.saveAndProceedAction.initForReason(reason);
     199        this.discardAndProceedAction.initForReason(reason);
    143200    }
    144201
     
    323380    class DiscardAndProceedAction extends AbstractAction implements PropertyChangeListener {
    324381        DiscardAndProceedAction() {
    325             initForDiscardAndExit();
    326         }
    327 
    328         public void initForDiscardAndExit() {
    329             putValue(NAME, tr("Exit now!"));
    330             putValue(SHORT_DESCRIPTION, tr("Exit JOSM without saving. Unsaved changes are lost."));
    331             putValue(SMALL_ICON, ImageProvider.get("exit"));
    332         }
    333 
    334         public void initForDiscardAndDelete() {
    335             putValue(NAME, tr("Delete now!"));
    336             putValue(SHORT_DESCRIPTION, tr("Delete layers without saving. Unsaved changes are lost."));
    337             putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
     382            initForReason(Reason.EXIT);
     383        }
     384
     385        public void initForReason(Reason reason) {
     386            switch (reason) {
     387                case EXIT:
     388                    putValue(NAME, tr("Exit now!"));
     389                    putValue(SHORT_DESCRIPTION, tr("Exit JOSM without saving. Unsaved changes are lost."));
     390                    putValue(SMALL_ICON, ImageProvider.get("exit"));
     391                    break;
     392                case RESTART:
     393                    putValue(NAME, tr("Restart now!"));
     394                    putValue(SHORT_DESCRIPTION, tr("Restart JOSM without saving. Unsaved changes are lost."));
     395                    putValue(SMALL_ICON, ImageProvider.get("restart"));
     396                    break;
     397                case DELETE:
     398                    putValue(NAME, tr("Delete now!"));
     399                    putValue(SHORT_DESCRIPTION, tr("Delete layers without saving. Unsaved changes are lost."));
     400                    putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
     401                    break;
     402            }
     403
    338404        }
    339405
     
    385451
    386452        SaveAndProceedAction() {
    387             initForSaveAndExit();
    388         }
    389 
    390         public void initForSaveAndExit() {
    391             putValue(NAME, tr("Perform actions before exiting"));
    392             putValue(SHORT_DESCRIPTION, tr("Exit JOSM with saving. Unsaved changes are uploaded and/or saved."));
    393             putValue(BASE_ICON, ImageProvider.get("exit"));
    394             redrawIcon();
    395         }
    396 
    397         public void initForSaveAndDelete() {
    398             putValue(NAME, tr("Perform actions before deleting"));
    399             putValue(SHORT_DESCRIPTION, tr("Save/Upload layers before deleting. Unsaved changes are not lost."));
    400             putValue(BASE_ICON, ImageProvider.get("dialogs", "delete"));
     453            initForReason(Reason.EXIT);
     454        }
     455
     456        public void initForReason(Reason reason) {
     457            switch (reason) {
     458                case EXIT:
     459                    putValue(NAME, tr("Perform actions before exiting"));
     460                    putValue(SHORT_DESCRIPTION, tr("Exit JOSM with saving. Unsaved changes are uploaded and/or saved."));
     461                    putValue(BASE_ICON, ImageProvider.get("exit"));
     462                    break;
     463                case RESTART:
     464                    putValue(NAME, tr("Perform actions before restarting"));
     465                    putValue(SHORT_DESCRIPTION, tr("Restart JOSM with saving. Unsaved changes are uploaded and/or saved."));
     466                    putValue(BASE_ICON, ImageProvider.get("restart"));
     467                    break;
     468                case DELETE:
     469                    putValue(NAME, tr("Perform actions before deleting"));
     470                    putValue(SHORT_DESCRIPTION, tr("Save/Upload layers before deleting. Unsaved changes are not lost."));
     471                    putValue(BASE_ICON, ImageProvider.get("dialogs", "delete"));
     472                    break;
     473            }
    401474            redrawIcon();
    402475        }
Note: See TracChangeset for help on using the changeset viewer.