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