| 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 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Cancel the updates and close the dialog
|
|---|
| 22 | * @since 9496
|
|---|
| 23 | */
|
|---|
| 24 | public class CancelAction extends SavingAction {
|
|---|
| 25 | private static final long serialVersionUID = 1L;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Constructs a new {@code CancelAction}.
|
|---|
| 29 | * @param editorAccess An interface to access the relation editor contents.
|
|---|
| 30 | */
|
|---|
| 31 | public CancelAction(IRelationEditorActionAccess editorAccess) {
|
|---|
| 32 | super(editorAccess);
|
|---|
| 33 | putValue(SHORT_DESCRIPTION, tr("Cancel the updates and close the dialog"));
|
|---|
| 34 | new ImageProvider("cancel").getResource().attachImageIcon(this);
|
|---|
| 35 | putValue(NAME, tr("Cancel"));
|
|---|
| 36 |
|
|---|
| 37 | if (getEditor() instanceof RootPaneContainer) {
|
|---|
| 38 | InputMapUtils.addEscapeAction(((RootPaneContainer) getEditor()).getRootPane(), this);
|
|---|
| 39 | }
|
|---|
| 40 | setEnabled(true);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | @Override
|
|---|
| 44 | public void actionPerformed(ActionEvent e) {
|
|---|
| 45 | getMemberTable().stopHighlighting();
|
|---|
| 46 | Relation snapshot = getEditor().getRelationSnapshot();
|
|---|
| 47 | if ((!getMemberTableModel().hasSameMembersAs(snapshot) || getTagModel().isDirty())
|
|---|
| 48 | && !(snapshot == null && getTagModel().getTags().isEmpty())) {
|
|---|
| 49 | //give the user a chance to save the changes
|
|---|
| 50 | int ret = confirmClosingByCancel(this.editorAccess.wouldRelationBeUseful());
|
|---|
| 51 | if (ret == 0) { //Yes, save the changes
|
|---|
| 52 | //copied from OKAction.run()
|
|---|
| 53 | Config.getPref().put("relation.editor.generic.lastrole", Utils.strip(tfRole.getText()));
|
|---|
| 54 | if (!applyChanges())
|
|---|
| 55 | return;
|
|---|
| 56 | } else if (ret == 2 || ret == JOptionPane.CLOSED_OPTION) //Cancel, continue editing
|
|---|
| 57 | return;
|
|---|
| 58 | //in case of "No, discard", there is no extra action to be performed here.
|
|---|
| 59 | }
|
|---|
| 60 | hideEditor();
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | protected int confirmClosingByCancel(boolean isUseful) {
|
|---|
| 64 | ButtonSpec[] options = {
|
|---|
| 65 | new ButtonSpec(
|
|---|
| 66 | tr("Yes, save the changes and close"),
|
|---|
| 67 | new ImageProvider("ok"),
|
|---|
| 68 | tr("Click to save the changes and close this relation editor"),
|
|---|
| 69 | null /* no specific help topic */
|
|---|
| 70 | ),
|
|---|
| 71 | new ButtonSpec(
|
|---|
| 72 | tr("No, discard the changes and close"),
|
|---|
| 73 | new ImageProvider("undo"),
|
|---|
| 74 | tr("Click to discard the changes and close this relation editor"),
|
|---|
| 75 | null /* no specific help topic */
|
|---|
| 76 | ),
|
|---|
| 77 | new ButtonSpec(
|
|---|
| 78 | tr("Cancel, continue editing"),
|
|---|
| 79 | new ImageProvider("cancel"),
|
|---|
| 80 | tr("Click to return to the relation editor and to resume relation editing"),
|
|---|
| 81 | null /* no specific help topic */
|
|---|
| 82 | )
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 | // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type"
|
|---|
| 86 | // AND must contain at least one other OSM object.
|
|---|
| 87 | options[0].setEnabled(isUseful);
|
|---|
| 88 |
|
|---|
| 89 | return HelpAwareOptionPane.showOptionDialog(
|
|---|
| 90 | MainApplication.getMainFrame(),
|
|---|
| 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 | }
|
|---|