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.JOptionPane;
|
---|
9 | import javax.swing.RootPaneContainer;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
14 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
15 | import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor;
|
---|
16 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
|
---|
17 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 | import org.openstreetmap.josm.gui.tagging.TagEditorModel;
|
---|
20 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
21 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Cancel the updates and close the dialog
|
---|
27 | * @since 9496
|
---|
28 | */
|
---|
29 | public 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 | }
|
---|