[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.Component;
|
---|
| 7 | import java.util.ArrayList;
|
---|
| 8 | import java.util.List;
|
---|
| 9 |
|
---|
| 10 | import javax.swing.JOptionPane;
|
---|
| 11 | import javax.swing.SwingUtilities;
|
---|
| 12 |
|
---|
| 13 | import org.openstreetmap.josm.Main;
|
---|
| 14 | import org.openstreetmap.josm.command.AddCommand;
|
---|
| 15 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
| 16 | import org.openstreetmap.josm.command.conflict.ConflictAddCommand;
|
---|
| 17 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
[12663] | 18 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
[9665] | 19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 21 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
| 22 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
[12630] | 23 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[9665] | 24 | import org.openstreetmap.josm.gui.dialogs.relation.RelationDialogManager;
|
---|
| 25 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
| 26 | import org.openstreetmap.josm.gui.tagging.TagEditorModel;
|
---|
| 27 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
| 28 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[11848] | 29 | import org.openstreetmap.josm.tools.Utils;
|
---|
[9665] | 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Abstract superclass of relation saving actions (OK, Apply, Cancel).
|
---|
| 33 | * @since 9496
|
---|
| 34 | */
|
---|
| 35 | abstract class SavingAction extends AbstractRelationEditorAction {
|
---|
[14027] | 36 | private static final long serialVersionUID = 1L;
|
---|
[9665] | 37 |
|
---|
| 38 | protected final AutoCompletingTextField tfRole;
|
---|
| 39 |
|
---|
[14027] | 40 | protected SavingAction(IRelationEditorActionAccess editorAccess, IRelationEditorUpdateOn... updateOn) {
|
---|
| 41 | super(editorAccess, updateOn);
|
---|
| 42 | this.tfRole = editorAccess.getTextFieldRole();
|
---|
[9665] | 43 | }
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * apply updates to a new relation
|
---|
| 47 | * @param tagEditorModel tag editor model
|
---|
| 48 | */
|
---|
| 49 | protected void applyNewRelation(TagEditorModel tagEditorModel) {
|
---|
| 50 | final Relation newRelation = new Relation();
|
---|
| 51 | tagEditorModel.applyToPrimitive(newRelation);
|
---|
[14027] | 52 | getMemberTableModel().applyToRelation(newRelation);
|
---|
[9665] | 53 | List<RelationMember> newMembers = new ArrayList<>();
|
---|
| 54 | for (RelationMember rm: newRelation.getMembers()) {
|
---|
| 55 | if (!rm.getMember().isDeleted()) {
|
---|
| 56 | newMembers.add(rm);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | if (newRelation.getMembersCount() != newMembers.size()) {
|
---|
| 60 | newRelation.setMembers(newMembers);
|
---|
| 61 | String msg = tr("One or more members of this new relation have been deleted while the relation editor\n" +
|
---|
| 62 | "was open. They have been removed from the relation members list.");
|
---|
| 63 | JOptionPane.showMessageDialog(Main.parent, msg, tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
| 64 | }
|
---|
| 65 | // If the user wanted to create a new relation, but hasn't added any members or
|
---|
| 66 | // tags, don't add an empty relation
|
---|
| 67 | if (newRelation.getMembersCount() == 0 && !newRelation.hasKeys())
|
---|
| 68 | return;
|
---|
[14027] | 69 | MainApplication.undoRedo.add(new AddCommand(getLayer().getDataSet(), newRelation));
|
---|
[9665] | 70 |
|
---|
| 71 | // make sure everybody is notified about the changes
|
---|
| 72 | //
|
---|
[14027] | 73 | getEditor().setRelation(newRelation);
|
---|
| 74 | if (getEditor() instanceof RelationEditor) {
|
---|
[9665] | 75 | RelationDialogManager.getRelationDialogManager().updateContext(
|
---|
[14027] | 76 | getLayer(), getEditor().getRelation(), (RelationEditor) getEditor());
|
---|
[9665] | 77 | }
|
---|
[10611] | 78 | // Relation list gets update in EDT so selecting my be postponed to following EDT run
|
---|
[12630] | 79 | SwingUtilities.invokeLater(() -> MainApplication.getMap().relationListDialog.selectRelation(newRelation));
|
---|
[9665] | 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Apply the updates for an existing relation which has been changed outside of the relation editor.
|
---|
| 84 | * @param tagEditorModel tag editor model
|
---|
| 85 | */
|
---|
| 86 | protected void applyExistingConflictingRelation(TagEditorModel tagEditorModel) {
|
---|
[14027] | 87 | Relation editedRelation = new Relation(editorAccess.getEditor().getRelation());
|
---|
[9665] | 88 | tagEditorModel.applyToPrimitive(editedRelation);
|
---|
[14027] | 89 | editorAccess.getMemberTableModel().applyToRelation(editedRelation);
|
---|
| 90 | Conflict<Relation> conflict = new Conflict<>(editorAccess.getEditor().getRelation(), editedRelation);
|
---|
| 91 | MainApplication.undoRedo.add(new ConflictAddCommand(getLayer().getDataSet(), conflict));
|
---|
[9665] | 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * Apply the updates for an existing relation which has not been changed outside of the relation editor.
|
---|
| 96 | * @param tagEditorModel tag editor model
|
---|
| 97 | */
|
---|
| 98 | protected void applyExistingNonConflictingRelation(TagEditorModel tagEditorModel) {
|
---|
[14027] | 99 | Relation originRelation = editorAccess.getEditor().getRelation();
|
---|
[12726] | 100 | Relation editedRelation = new Relation(originRelation);
|
---|
[9665] | 101 | tagEditorModel.applyToPrimitive(editedRelation);
|
---|
[14027] | 102 | getMemberTableModel().applyToRelation(editedRelation);
|
---|
[12726] | 103 | if (!editedRelation.hasEqualSemanticAttributes(originRelation, false)) {
|
---|
| 104 | MainApplication.undoRedo.add(new ChangeCommand(originRelation, editedRelation));
|
---|
[11684] | 105 | }
|
---|
[9665] | 106 | }
|
---|
| 107 |
|
---|
| 108 | protected boolean confirmClosingBecauseOfDirtyState() {
|
---|
| 109 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
| 110 | new ButtonSpec(
|
---|
| 111 | tr("Yes, create a conflict and close"),
|
---|
[13842] | 112 | new ImageProvider("ok"),
|
---|
[9665] | 113 | tr("Click to create a conflict and close this relation editor"),
|
---|
| 114 | null /* no specific help topic */
|
---|
| 115 | ),
|
---|
| 116 | new ButtonSpec(
|
---|
| 117 | tr("No, continue editing"),
|
---|
[13842] | 118 | new ImageProvider("cancel"),
|
---|
[9665] | 119 | tr("Click to return to the relation editor and to resume relation editing"),
|
---|
| 120 | null /* no specific help topic */
|
---|
| 121 | )
|
---|
| 122 | };
|
---|
| 123 |
|
---|
| 124 | int ret = HelpAwareOptionPane.showOptionDialog(
|
---|
| 125 | Main.parent,
|
---|
| 126 | tr("<html>This relation has been changed outside of the editor.<br>"
|
---|
| 127 | + "You cannot apply your changes and continue editing.<br>"
|
---|
| 128 | + "<br>"
|
---|
| 129 | + "Do you want to create a conflict and close the editor?</html>"),
|
---|
| 130 | tr("Conflict in data"),
|
---|
| 131 | JOptionPane.WARNING_MESSAGE,
|
---|
| 132 | null,
|
---|
| 133 | options,
|
---|
| 134 | options[0], // OK is default
|
---|
| 135 | "/Dialog/RelationEditor#RelationChangedOutsideOfEditor"
|
---|
| 136 | );
|
---|
[11120] | 137 | if (ret == 0) {
|
---|
[12630] | 138 | MainApplication.getMap().conflictDialog.unfurlDialog();
|
---|
[11120] | 139 | }
|
---|
[9665] | 140 | return ret == 0;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | protected void warnDoubleConflict() {
|
---|
| 144 | JOptionPane.showMessageDialog(
|
---|
| 145 | Main.parent,
|
---|
| 146 | tr("<html>Layer ''{0}'' already has a conflict for object<br>"
|
---|
| 147 | + "''{1}''.<br>"
|
---|
| 148 | + "Please resolve this conflict first, then try again.</html>",
|
---|
[14027] | 149 | Utils.escapeReservedCharactersHTML(getLayer().getName()),
|
---|
| 150 | Utils.escapeReservedCharactersHTML(getEditor().getRelation().getDisplayName(DefaultNameFormatter.getInstance()))
|
---|
[9665] | 151 | ),
|
---|
| 152 | tr("Double conflict"),
|
---|
| 153 | JOptionPane.WARNING_MESSAGE
|
---|
| 154 | );
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | @Override
|
---|
| 158 | protected void updateEnabledState() {
|
---|
| 159 | // Do nothing
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | protected boolean applyChanges() {
|
---|
[14027] | 163 | if (editorAccess.getEditor().getRelation() == null) {
|
---|
| 164 | applyNewRelation(getTagModel());
|
---|
[9665] | 165 | } else if (isEditorDirty()) {
|
---|
[14027] | 166 | if (editorAccess.getEditor().isDirtyRelation()) {
|
---|
[9665] | 167 | if (confirmClosingBecauseOfDirtyState()) {
|
---|
[14027] | 168 | if (getLayer().getConflicts().hasConflictForMy(editorAccess.getEditor().getRelation())) {
|
---|
[9665] | 169 | warnDoubleConflict();
|
---|
| 170 | return false;
|
---|
| 171 | }
|
---|
[14027] | 172 | applyExistingConflictingRelation(getTagModel());
|
---|
[9665] | 173 | hideEditor();
|
---|
| 174 | } else
|
---|
| 175 | return false;
|
---|
| 176 | } else {
|
---|
[14027] | 177 | applyExistingNonConflictingRelation(getTagModel());
|
---|
[9665] | 178 | }
|
---|
| 179 | }
|
---|
[14027] | 180 | editorAccess.getEditor().setRelation(editorAccess.getEditor().getRelation());
|
---|
[9665] | 181 | return true;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | protected void hideEditor() {
|
---|
[14027] | 185 | if (editorAccess.getEditor() instanceof Component) {
|
---|
| 186 | ((Component) editorAccess.getEditor()).setVisible(false);
|
---|
[9665] | 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | protected boolean isEditorDirty() {
|
---|
[14027] | 191 | Relation snapshot = editorAccess.getEditor().getRelationSnapshot();
|
---|
| 192 | return (snapshot != null && !getMemberTableModel().hasSameMembersAs(snapshot)) || getTagModel().isDirty();
|
---|
[9665] | 193 | }
|
---|
| 194 | }
|
---|