source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/SavingAction.java@ 11848

Last change on this file since 11848 was 11848, checked in by Don-vip, 7 years ago

fix #14613 - Special HTML characters not escaped in GUI error messages

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