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