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.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
13 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
14 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
15 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
17 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Cancel the updates and close the dialog
|
---|
21 | * @since 9496
|
---|
22 | */
|
---|
23 | public class CancelAction extends SavingAction {
|
---|
24 | private static final long serialVersionUID = 1L;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code CancelAction}.
|
---|
28 | * @param editorAccess An interface to access the relation editor contents.
|
---|
29 | */
|
---|
30 | public CancelAction(IRelationEditorActionAccess editorAccess) {
|
---|
31 | super(editorAccess);
|
---|
32 | putValue(SHORT_DESCRIPTION, tr("Cancel the updates and close the dialog"));
|
---|
33 | new ImageProvider("cancel").getResource().attachImageIcon(this);
|
---|
34 | putValue(NAME, tr("Cancel"));
|
---|
35 |
|
---|
36 | if (getEditor() instanceof RootPaneContainer) {
|
---|
37 | InputMapUtils.addEscapeAction(((RootPaneContainer) getEditor()).getRootPane(), this);
|
---|
38 | }
|
---|
39 | setEnabled(true);
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void actionPerformed(ActionEvent e) {
|
---|
44 | getMemberTable().stopHighlighting();
|
---|
45 | Relation snapshot = getEditor().getRelationSnapshot();
|
---|
46 | if ((!getMemberTableModel().hasSameMembersAs(snapshot) || getTagModel().isDirty())
|
---|
47 | && !(snapshot == null && getTagModel().getTags().isEmpty())) {
|
---|
48 | //give the user a chance to save the changes
|
---|
49 | int ret = confirmClosingByCancel();
|
---|
50 | if (ret == 0) { //Yes, save the changes
|
---|
51 | //copied from OKAction.run()
|
---|
52 | Config.getPref().put("relation.editor.generic.lastrole", tfRole.getText());
|
---|
53 | if (!applyChanges())
|
---|
54 | return;
|
---|
55 | } else if (ret == 2 || ret == JOptionPane.CLOSED_OPTION) //Cancel, continue editing
|
---|
56 | return;
|
---|
57 | //in case of "No, discard", there is no extra action to be performed here.
|
---|
58 | }
|
---|
59 | hideEditor();
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected int confirmClosingByCancel() {
|
---|
63 | ButtonSpec[] options = {
|
---|
64 | new ButtonSpec(
|
---|
65 | tr("Yes, save the changes and close"),
|
---|
66 | new ImageProvider("ok"),
|
---|
67 | tr("Click to save the changes and close this relation editor"),
|
---|
68 | null /* no specific help topic */
|
---|
69 | ),
|
---|
70 | new ButtonSpec(
|
---|
71 | tr("No, discard the changes and close"),
|
---|
72 | new ImageProvider("undo"),
|
---|
73 | tr("Click to discard the changes and close this relation editor"),
|
---|
74 | null /* no specific help topic */
|
---|
75 | ),
|
---|
76 | new ButtonSpec(
|
---|
77 | tr("Cancel, continue editing"),
|
---|
78 | new ImageProvider("cancel"),
|
---|
79 | tr("Click to return to the relation editor and to resume relation editing"),
|
---|
80 | null /* no specific help topic */
|
---|
81 | )
|
---|
82 | };
|
---|
83 |
|
---|
84 | return HelpAwareOptionPane.showOptionDialog(
|
---|
85 | MainApplication.getMainFrame(),
|
---|
86 | tr("<html>The relation has been changed.<br><br>Do you want to save your changes?</html>"),
|
---|
87 | tr("Unsaved changes"),
|
---|
88 | JOptionPane.WARNING_MESSAGE,
|
---|
89 | null,
|
---|
90 | options,
|
---|
91 | options[0], // OK is default,
|
---|
92 | "/Dialog/RelationEditor#DiscardChanges"
|
---|
93 | );
|
---|
94 | }
|
---|
95 | }
|
---|