source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/CancelAction.java@ 9512

Last change on this file since 9512 was 9512, checked in by simon04, 8 years ago

see #12131 - Pressing escape in closing relation confirmation should not discard the changes and close the editor

File size: 5.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.event.ActionEvent;
8
9import javax.swing.JComponent;
10import javax.swing.JOptionPane;
11import javax.swing.JRootPane;
12import javax.swing.KeyStroke;
13import javax.swing.RootPaneContainer;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.gui.HelpAwareOptionPane;
18import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
19import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
20import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
21import org.openstreetmap.josm.gui.dialogs.relation.RelationAware;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.gui.tagging.TagEditorModel;
24import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
25import org.openstreetmap.josm.tools.ImageProvider;
26
27/**
28 * Cancel the updates and close the dialog
29 * @since 9496
30 */
31public class CancelAction extends SavingAction {
32
33 /**
34 * Constructs a new {@code CancelAction}.
35 * @param memberTable member table
36 * @param memberTableModel member table model
37 * @param tagModel tag editor model
38 * @param layer OSM data layer
39 * @param editor relation editor
40 * @param tfRole role text field
41 */
42 public CancelAction(MemberTable memberTable, MemberTableModel memberTableModel, TagEditorModel tagModel, OsmDataLayer layer,
43 RelationAware editor, AutoCompletingTextField tfRole) {
44 super(memberTable, memberTableModel, tagModel, layer, editor, tfRole);
45 putValue(SHORT_DESCRIPTION, tr("Cancel the updates and close the dialog"));
46 putValue(SMALL_ICON, ImageProvider.get("cancel"));
47 putValue(NAME, tr("Cancel"));
48
49 if (editor instanceof RootPaneContainer) {
50 JRootPane root = ((RootPaneContainer) editor).getRootPane();
51 root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
52 root.getActionMap().put("ESCAPE", this);
53 }
54 setEnabled(true);
55 }
56
57 @Override
58 public void actionPerformed(ActionEvent e) {
59 memberTable.stopHighlighting();
60 Relation snapshot = editor.getRelationSnapshot();
61 if ((!memberTableModel.hasSameMembersAs(snapshot) || tagModel.isDirty())
62 && !(snapshot == null && tagModel.getTags().isEmpty())) {
63 //give the user a chance to save the changes
64 int ret = confirmClosingByCancel();
65 if (ret == 0) { //Yes, save the changes
66 //copied from OKAction.run()
67 Main.pref.put("relation.editor.generic.lastrole", tfRole.getText());
68 if (editor.getRelation() == null) {
69 applyNewRelation(tagModel);
70 } else if (!memberTableModel.hasSameMembersAs(snapshot) || tagModel.isDirty()) {
71 if (editor.isDirtyRelation()) {
72 if (confirmClosingBecauseOfDirtyState()) {
73 if (layer.getConflicts().hasConflictForMy(editor.getRelation())) {
74 warnDoubleConflict();
75 return;
76 }
77 applyExistingConflictingRelation(tagModel);
78 } else
79 return;
80 } else {
81 applyExistingNonConflictingRelation(tagModel);
82 }
83 }
84 } else if (ret == 2 || ret == JOptionPane.CLOSED_OPTION) //Cancel, continue editing
85 return;
86 //in case of "No, discard", there is no extra action to be performed here.
87 }
88 if (editor instanceof Component) {
89 ((Component) editor).setVisible(false);
90 }
91 }
92
93 protected int confirmClosingByCancel() {
94 ButtonSpec[] options = new ButtonSpec[] {
95 new ButtonSpec(
96 tr("Yes, save the changes and close"),
97 ImageProvider.get("ok"),
98 tr("Click to save the changes and close this relation editor"),
99 null /* no specific help topic */
100 ),
101 new ButtonSpec(
102 tr("No, discard the changes and close"),
103 ImageProvider.get("cancel"),
104 tr("Click to discard the changes and close this relation editor"),
105 null /* no specific help topic */
106 ),
107 new ButtonSpec(
108 tr("Cancel, continue editing"),
109 ImageProvider.get("cancel"),
110 tr("Click to return to the relation editor and to resume relation editing"),
111 null /* no specific help topic */
112 )
113 };
114
115 return HelpAwareOptionPane.showOptionDialog(
116 Main.parent,
117 tr("<html>The relation has been changed.<br><br>Do you want to save your changes?</html>"),
118 tr("Unsaved changes"),
119 JOptionPane.WARNING_MESSAGE,
120 null,
121 options,
122 options[0], // OK is default,
123 "/Dialog/RelationEditor#DiscardChanges"
124 );
125 }
126}
Note: See TracBrowser for help on using the repository browser.