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.Main;
012import org.openstreetmap.josm.data.osm.Relation;
013import org.openstreetmap.josm.gui.HelpAwareOptionPane;
014import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
015import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor;
016import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
017import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
018import org.openstreetmap.josm.gui.layer.OsmDataLayer;
019import org.openstreetmap.josm.gui.tagging.TagEditorModel;
020import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
021import org.openstreetmap.josm.spi.preferences.Config;
022import org.openstreetmap.josm.tools.ImageProvider;
023import org.openstreetmap.josm.tools.InputMapUtils;
024
025/**
026 * Cancel the updates and close the dialog
027 * @since 9496
028 */
029public class CancelAction extends SavingAction {
030
031    /**
032     * Constructs a new {@code CancelAction}.
033     * @param memberTable member table
034     * @param memberTableModel member table model
035     * @param tagModel tag editor model
036     * @param layer OSM data layer
037     * @param editor relation editor
038     * @param tfRole role text field
039     */
040    public CancelAction(MemberTable memberTable, MemberTableModel memberTableModel, TagEditorModel tagModel, OsmDataLayer layer,
041            IRelationEditor editor, AutoCompletingTextField tfRole) {
042        super(memberTable, memberTableModel, tagModel, layer, editor, tfRole);
043        putValue(SHORT_DESCRIPTION, tr("Cancel the updates and close the dialog"));
044        new ImageProvider("cancel").getResource().attachImageIcon(this);
045        putValue(NAME, tr("Cancel"));
046
047        if (editor instanceof RootPaneContainer) {
048            InputMapUtils.addEscapeAction(((RootPaneContainer) editor).getRootPane(), this);
049        }
050        setEnabled(true);
051    }
052
053    @Override
054    public void actionPerformed(ActionEvent e) {
055        memberTable.stopHighlighting();
056        Relation snapshot = editor.getRelationSnapshot();
057        if ((!memberTableModel.hasSameMembersAs(snapshot) || tagModel.isDirty())
058         && !(snapshot == null && tagModel.getTags().isEmpty())) {
059            //give the user a chance to save the changes
060            int ret = confirmClosingByCancel();
061            if (ret == 0) { //Yes, save the changes
062                //copied from OKAction.run()
063                Config.getPref().put("relation.editor.generic.lastrole", tfRole.getText());
064                if (!applyChanges())
065                    return;
066            } else if (ret == 2 || ret == JOptionPane.CLOSED_OPTION) //Cancel, continue editing
067                return;
068            //in case of "No, discard", there is no extra action to be performed here.
069        }
070        hideEditor();
071    }
072
073    protected int confirmClosingByCancel() {
074        ButtonSpec[] options = new ButtonSpec[] {
075                new ButtonSpec(
076                        tr("Yes, save the changes and close"),
077                        ImageProvider.get("ok"),
078                        tr("Click to save the changes and close this relation editor"),
079                        null /* no specific help topic */
080                ),
081                new ButtonSpec(
082                        tr("No, discard the changes and close"),
083                        ImageProvider.get("cancel"),
084                        tr("Click to discard the changes and close this relation editor"),
085                        null /* no specific help topic */
086                ),
087                new ButtonSpec(
088                        tr("Cancel, continue editing"),
089                        ImageProvider.get("cancel"),
090                        tr("Click to return to the relation editor and to resume relation editing"),
091                        null /* no specific help topic */
092                )
093        };
094
095        return HelpAwareOptionPane.showOptionDialog(
096                Main.parent,
097                tr("<html>The relation has been changed.<br><br>Do you want to save your changes?</html>"),
098                        tr("Unsaved changes"),
099                        JOptionPane.WARNING_MESSAGE,
100                        null,
101                        options,
102                        options[0], // OK is default,
103                        "/Dialog/RelationEditor#DiscardChanges"
104        );
105    }
106}