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

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

File size: 4.4 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.event.ActionEvent;
7
8import javax.swing.JOptionPane;
9import javax.swing.RootPaneContainer;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.Relation;
13import org.openstreetmap.josm.gui.HelpAwareOptionPane;
14import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
15import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor;
16import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
17import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.gui.tagging.TagEditorModel;
20import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
21import org.openstreetmap.josm.spi.preferences.Config;
22import org.openstreetmap.josm.tools.ImageProvider;
23import org.openstreetmap.josm.tools.InputMapUtils;
24
25/**
26 * Cancel the updates and close the dialog
27 * @since 9496
28 */
29public class CancelAction extends SavingAction {
30
31 /**
32 * Constructs a new {@code CancelAction}.
33 * @param memberTable member table
34 * @param memberTableModel member table model
35 * @param tagModel tag editor model
36 * @param layer OSM data layer
37 * @param editor relation editor
38 * @param tfRole role text field
39 */
40 public CancelAction(MemberTable memberTable, MemberTableModel memberTableModel, TagEditorModel tagModel, OsmDataLayer layer,
41 IRelationEditor editor, AutoCompletingTextField tfRole) {
42 super(memberTable, memberTableModel, tagModel, layer, editor, tfRole);
43 putValue(SHORT_DESCRIPTION, tr("Cancel the updates and close the dialog"));
44 new ImageProvider("cancel").getResource().attachImageIcon(this);
45 putValue(NAME, tr("Cancel"));
46
47 if (editor instanceof RootPaneContainer) {
48 InputMapUtils.addEscapeAction(((RootPaneContainer) editor).getRootPane(), this);
49 }
50 setEnabled(true);
51 }
52
53 @Override
54 public void actionPerformed(ActionEvent e) {
55 memberTable.stopHighlighting();
56 Relation snapshot = editor.getRelationSnapshot();
57 if ((!memberTableModel.hasSameMembersAs(snapshot) || tagModel.isDirty())
58 && !(snapshot == null && tagModel.getTags().isEmpty())) {
59 //give the user a chance to save the changes
60 int ret = confirmClosingByCancel();
61 if (ret == 0) { //Yes, save the changes
62 //copied from OKAction.run()
63 Config.getPref().put("relation.editor.generic.lastrole", tfRole.getText());
64 if (!applyChanges())
65 return;
66 } else if (ret == 2 || ret == JOptionPane.CLOSED_OPTION) //Cancel, continue editing
67 return;
68 //in case of "No, discard", there is no extra action to be performed here.
69 }
70 hideEditor();
71 }
72
73 protected int confirmClosingByCancel() {
74 ButtonSpec[] options = new ButtonSpec[] {
75 new ButtonSpec(
76 tr("Yes, save the changes and close"),
77 ImageProvider.get("ok"),
78 tr("Click to save the changes and close this relation editor"),
79 null /* no specific help topic */
80 ),
81 new ButtonSpec(
82 tr("No, discard the changes and close"),
83 ImageProvider.get("cancel"),
84 tr("Click to discard the changes and close this relation editor"),
85 null /* no specific help topic */
86 ),
87 new ButtonSpec(
88 tr("Cancel, continue editing"),
89 ImageProvider.get("cancel"),
90 tr("Click to return to the relation editor and to resume relation editing"),
91 null /* no specific help topic */
92 )
93 };
94
95 return HelpAwareOptionPane.showOptionDialog(
96 Main.parent,
97 tr("<html>The relation has been changed.<br><br>Do you want to save your changes?</html>"),
98 tr("Unsaved changes"),
99 JOptionPane.WARNING_MESSAGE,
100 null,
101 options,
102 options[0], // OK is default,
103 "/Dialog/RelationEditor#DiscardChanges"
104 );
105 }
106}
Note: See TracBrowser for help on using the repository browser.