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