001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.relation.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007
008import javax.swing.JOptionPane;
009import javax.swing.RootPaneContainer;
010
011import org.openstreetmap.josm.data.osm.Relation;
012import org.openstreetmap.josm.gui.HelpAwareOptionPane;
013import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
014import org.openstreetmap.josm.gui.MainApplication;
015import org.openstreetmap.josm.spi.preferences.Config;
016import org.openstreetmap.josm.tools.ImageProvider;
017import org.openstreetmap.josm.tools.InputMapUtils;
018import org.openstreetmap.josm.tools.Utils;
019
020/**
021 * Cancel the updates and close the dialog
022 * @since 9496
023 */
024public class CancelAction extends SavingAction {
025    private static final long serialVersionUID = 1L;
026
027    /**
028     * Constructs a new {@code CancelAction}.
029     * @param editorAccess An interface to access the relation editor contents.
030     */
031    public CancelAction(IRelationEditorActionAccess editorAccess) {
032        super(editorAccess);
033        putValue(SHORT_DESCRIPTION, tr("Cancel the updates and close the dialog"));
034        new ImageProvider("cancel").getResource().attachImageIcon(this);
035        putValue(NAME, tr("Cancel"));
036
037        if (getEditor() instanceof RootPaneContainer) {
038            InputMapUtils.addEscapeAction(((RootPaneContainer) getEditor()).getRootPane(), this);
039        }
040        setEnabled(true);
041    }
042
043    @Override
044    public void actionPerformed(ActionEvent e) {
045        getMemberTable().stopHighlighting();
046        Relation snapshot = getEditor().getRelationSnapshot();
047        if ((!getMemberTableModel().hasSameMembersAs(snapshot) || getTagModel().isDirty())
048         && !(snapshot == null && getTagModel().getTags().isEmpty())) {
049            //give the user a chance to save the changes
050            int ret = confirmClosingByCancel();
051            if (ret == 0) { //Yes, save the changes
052                //copied from OKAction.run()
053                Config.getPref().put("relation.editor.generic.lastrole", Utils.strip(tfRole.getText()));
054                if (!applyChanges())
055                    return;
056            } else if (ret == 2 || ret == JOptionPane.CLOSED_OPTION) //Cancel, continue editing
057                return;
058            //in case of "No, discard", there is no extra action to be performed here.
059        }
060        hideEditor();
061    }
062
063    protected int confirmClosingByCancel() {
064        ButtonSpec[] options = {
065                new ButtonSpec(
066                        tr("Yes, save the changes and close"),
067                        new ImageProvider("ok"),
068                        tr("Click to save the changes and close this relation editor"),
069                        null /* no specific help topic */
070                ),
071                new ButtonSpec(
072                        tr("No, discard the changes and close"),
073                        new ImageProvider("undo"),
074                        tr("Click to discard the changes and close this relation editor"),
075                        null /* no specific help topic */
076                ),
077                new ButtonSpec(
078                        tr("Cancel, continue editing"),
079                        new ImageProvider("cancel"),
080                        tr("Click to return to the relation editor and to resume relation editing"),
081                        null /* no specific help topic */
082                )
083        };
084
085        return HelpAwareOptionPane.showOptionDialog(
086                MainApplication.getMainFrame(),
087                tr("<html>The relation has been changed.<br><br>Do you want to save your changes?</html>"),
088                        tr("Unsaved changes"),
089                        JOptionPane.WARNING_MESSAGE,
090                        null,
091                        options,
092                        options[0], // OK is default,
093                        "/Dialog/RelationEditor#DiscardChanges"
094        );
095    }
096}