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

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

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

File size: 8.2 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;
32
33/**
34 * Abstract superclass of relation saving actions (OK, Apply, Cancel).
35 * @since 9496
36 */
37abstract class SavingAction extends AbstractRelationEditorAction {
38
39 protected final TagEditorModel tagModel;
40 protected final AutoCompletingTextField tfRole;
41
42 protected SavingAction(MemberTable memberTable, MemberTableModel memberTableModel, TagEditorModel tagModel, OsmDataLayer layer,
43 IRelationEditor editor, AutoCompletingTextField tfRole) {
44 super(memberTable, memberTableModel, null, layer, editor);
45 this.tagModel = tagModel;
46 this.tfRole = tfRole;
47 }
48
49 /**
50 * apply updates to a new relation
51 * @param tagEditorModel tag editor model
52 */
53 protected void applyNewRelation(TagEditorModel tagEditorModel) {
54 final Relation newRelation = new Relation();
55 tagEditorModel.applyToPrimitive(newRelation);
56 memberTableModel.applyToRelation(newRelation);
57 List<RelationMember> newMembers = new ArrayList<>();
58 for (RelationMember rm: newRelation.getMembers()) {
59 if (!rm.getMember().isDeleted()) {
60 newMembers.add(rm);
61 }
62 }
63 if (newRelation.getMembersCount() != newMembers.size()) {
64 newRelation.setMembers(newMembers);
65 String msg = tr("One or more members of this new relation have been deleted while the relation editor\n" +
66 "was open. They have been removed from the relation members list.");
67 JOptionPane.showMessageDialog(Main.parent, msg, tr("Warning"), JOptionPane.WARNING_MESSAGE);
68 }
69 // If the user wanted to create a new relation, but hasn't added any members or
70 // tags, don't add an empty relation
71 if (newRelation.getMembersCount() == 0 && !newRelation.hasKeys())
72 return;
73 Main.main.undoRedo.add(new AddCommand(layer, newRelation));
74
75 // make sure everybody is notified about the changes
76 //
77 layer.data.fireSelectionChanged();
78 editor.setRelation(newRelation);
79 if (editor instanceof RelationEditor) {
80 RelationDialogManager.getRelationDialogManager().updateContext(
81 layer, editor.getRelation(), (RelationEditor) editor);
82 }
83 // Relation list gets update in EDT so selecting my be postponed to following EDT run
84 SwingUtilities.invokeLater(() -> Main.map.relationListDialog.selectRelation(newRelation));
85 }
86
87 /**
88 * Apply the updates for an existing relation which has been changed outside of the relation editor.
89 * @param tagEditorModel tag editor model
90 */
91 protected void applyExistingConflictingRelation(TagEditorModel tagEditorModel) {
92 Relation editedRelation = new Relation(editor.getRelation());
93 tagEditorModel.applyToPrimitive(editedRelation);
94 memberTableModel.applyToRelation(editedRelation);
95 Conflict<Relation> conflict = new Conflict<>(editor.getRelation(), editedRelation);
96 Main.main.undoRedo.add(new ConflictAddCommand(layer, conflict));
97 }
98
99 /**
100 * Apply the updates for an existing relation which has not been changed outside of the relation editor.
101 * @param tagEditorModel tag editor model
102 */
103 protected void applyExistingNonConflictingRelation(TagEditorModel tagEditorModel) {
104 Relation editedRelation = new Relation(editor.getRelation());
105 tagEditorModel.applyToPrimitive(editedRelation);
106 memberTableModel.applyToRelation(editedRelation);
107 Main.main.undoRedo.add(new ChangeCommand(editor.getRelation(), editedRelation));
108 layer.data.fireSelectionChanged();
109 }
110
111 protected boolean confirmClosingBecauseOfDirtyState() {
112 ButtonSpec[] options = new ButtonSpec[] {
113 new ButtonSpec(
114 tr("Yes, create a conflict and close"),
115 ImageProvider.get("ok"),
116 tr("Click to create a conflict and close this relation editor"),
117 null /* no specific help topic */
118 ),
119 new ButtonSpec(
120 tr("No, continue editing"),
121 ImageProvider.get("cancel"),
122 tr("Click to return to the relation editor and to resume relation editing"),
123 null /* no specific help topic */
124 )
125 };
126
127 int ret = HelpAwareOptionPane.showOptionDialog(
128 Main.parent,
129 tr("<html>This relation has been changed outside of the editor.<br>"
130 + "You cannot apply your changes and continue editing.<br>"
131 + "<br>"
132 + "Do you want to create a conflict and close the editor?</html>"),
133 tr("Conflict in data"),
134 JOptionPane.WARNING_MESSAGE,
135 null,
136 options,
137 options[0], // OK is default
138 "/Dialog/RelationEditor#RelationChangedOutsideOfEditor"
139 );
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>",
149 layer.getName(),
150 editor.getRelation().getDisplayName(DefaultNameFormatter.getInstance())
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() {
163 if (editor.getRelation() == null) {
164 applyNewRelation(tagModel);
165 } else if (isEditorDirty()) {
166 if (editor.isDirtyRelation()) {
167 if (confirmClosingBecauseOfDirtyState()) {
168 if (layer.getConflicts().hasConflictForMy(editor.getRelation())) {
169 warnDoubleConflict();
170 return false;
171 }
172 applyExistingConflictingRelation(tagModel);
173 hideEditor();
174 } else
175 return false;
176 } else {
177 applyExistingNonConflictingRelation(tagModel);
178 }
179 }
180 editor.setRelation(editor.getRelation());
181 return true;
182 }
183
184 protected void hideEditor() {
185 if (editor instanceof Component) {
186 ((Component) editor).setVisible(false);
187 }
188 }
189
190 protected boolean isEditorDirty() {
191 Relation snapshot = editor.getRelationSnapshot();
192 return (snapshot != null && !memberTableModel.hasSameMembersAs(snapshot)) || tagModel.isDirty();
193 }
194}
Note: See TracBrowser for help on using the repository browser.