source: josm/trunk/src/org/openstreetmap/josm/corrector/TagCorrector.java@ 10621

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

sonar - squid:S1854 - Dead stores should be removed

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.corrector;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.List;
13import java.util.Map;
14import java.util.Map.Entry;
15import java.util.Set;
16
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20import javax.swing.JScrollPane;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.command.ChangeCommand;
24import org.openstreetmap.josm.command.ChangeRelationMemberRoleCommand;
25import org.openstreetmap.josm.command.Command;
26import org.openstreetmap.josm.data.correction.RoleCorrection;
27import org.openstreetmap.josm.data.correction.TagCorrection;
28import org.openstreetmap.josm.data.osm.Node;
29import org.openstreetmap.josm.data.osm.OsmPrimitive;
30import org.openstreetmap.josm.data.osm.Relation;
31import org.openstreetmap.josm.data.osm.Way;
32import org.openstreetmap.josm.gui.DefaultNameFormatter;
33import org.openstreetmap.josm.gui.correction.RoleCorrectionTable;
34import org.openstreetmap.josm.gui.correction.TagCorrectionTable;
35import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
36import org.openstreetmap.josm.tools.GBC;
37import org.openstreetmap.josm.tools.ImageProvider;
38import org.openstreetmap.josm.tools.UserCancelException;
39
40/**
41 * Abstract base class for automatic tag corrections.
42 *
43 * Subclasses call applyCorrections() with maps of the requested
44 * corrections and a dialog is pesented to the user to
45 * confirm these changes.
46 * @param <P> The type of OSM primitive to correct
47 */
48public abstract class TagCorrector<P extends OsmPrimitive> {
49
50 public abstract Collection<Command> execute(P oldprimitive, P primitive) throws UserCancelException;
51
52 private static final String[] APPLICATION_OPTIONS = new String[] {
53 tr("Apply selected changes"),
54 tr("Do not apply changes"),
55 tr("Cancel")
56 };
57
58 protected Collection<Command> applyCorrections(
59 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap,
60 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap,
61 String description) throws UserCancelException {
62
63 if (!tagCorrectionsMap.isEmpty() || !roleCorrectionMap.isEmpty()) {
64 Collection<Command> commands = new ArrayList<>();
65 Map<OsmPrimitive, TagCorrectionTable> tagTableMap = new HashMap<>();
66 Map<OsmPrimitive, RoleCorrectionTable> roleTableMap = new HashMap<>();
67
68 final JPanel p = new JPanel(new GridBagLayout());
69
70 final JMultilineLabel label1 = new JMultilineLabel(description);
71 label1.setMaxWidth(600);
72 p.add(label1, GBC.eop().anchor(GBC.CENTER).fill(GBC.HORIZONTAL));
73
74 final JMultilineLabel label2 = new JMultilineLabel(
75 tr("Please select which changes you want to apply."));
76 label2.setMaxWidth(600);
77 p.add(label2, GBC.eop().anchor(GBC.CENTER).fill(GBC.HORIZONTAL));
78
79 for (Entry<OsmPrimitive, List<TagCorrection>> entry : tagCorrectionsMap.entrySet()) {
80 final OsmPrimitive primitive = entry.getKey();
81 final List<TagCorrection> tagCorrections = entry.getValue();
82
83 if (tagCorrections.isEmpty()) {
84 continue;
85 }
86
87 final JLabel propertiesLabel = new JLabel(tr("Tags of "));
88 p.add(propertiesLabel, GBC.std());
89
90 final JLabel primitiveLabel = new JLabel(
91 primitive.getDisplayName(DefaultNameFormatter.getInstance()) + ':',
92 ImageProvider.get(primitive.getDisplayType()),
93 JLabel.LEFT
94 );
95 p.add(primitiveLabel, GBC.eol());
96
97 final TagCorrectionTable table = new TagCorrectionTable(
98 tagCorrections);
99 final JScrollPane scrollPane = new JScrollPane(table);
100 p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
101
102 tagTableMap.put(primitive, table);
103 }
104
105 for (Entry<OsmPrimitive, List<RoleCorrection>> entry : roleCorrectionMap.entrySet()) {
106 final OsmPrimitive primitive = entry.getKey();
107 final List<RoleCorrection> roleCorrections = entry.getValue();
108
109 if (roleCorrections.isEmpty()) {
110 continue;
111 }
112
113 final JLabel rolesLabel = new JLabel(tr("Roles in relations referring to"));
114 p.add(rolesLabel, GBC.std());
115
116 final JLabel primitiveLabel = new JLabel(
117 primitive.getDisplayName(DefaultNameFormatter.getInstance()),
118 ImageProvider.get(primitive.getDisplayType()),
119 JLabel.LEFT
120 );
121 p.add(primitiveLabel, GBC.eol());
122 rolesLabel.setLabelFor(primitiveLabel);
123
124 final RoleCorrectionTable table = new RoleCorrectionTable(roleCorrections);
125 final JScrollPane scrollPane = new JScrollPane(table);
126 p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
127 primitiveLabel.setLabelFor(table);
128
129 roleTableMap.put(primitive, table);
130 }
131
132 int answer = JOptionPane.showOptionDialog(
133 Main.parent,
134 p,
135 tr("Automatic tag correction"),
136 JOptionPane.YES_NO_CANCEL_OPTION,
137 JOptionPane.PLAIN_MESSAGE,
138 null,
139 APPLICATION_OPTIONS,
140 APPLICATION_OPTIONS[0]
141 );
142
143 if (answer == JOptionPane.YES_OPTION) {
144 for (Entry<OsmPrimitive, List<TagCorrection>> entry : tagCorrectionsMap.entrySet()) {
145 OsmPrimitive primitive = entry.getKey();
146
147 // create the clone
148 OsmPrimitive clone;
149 if (primitive instanceof Way) {
150 clone = new Way((Way) primitive);
151 } else if (primitive instanceof Node) {
152 clone = new Node((Node) primitive);
153 } else if (primitive instanceof Relation) {
154 clone = new Relation((Relation) primitive);
155 } else
156 throw new AssertionError();
157
158 // use this structure to remember keys that have been set already so that
159 // they're not dropped by a later step
160 Set<String> keysChanged = new HashSet<>();
161
162 // apply all changes to this clone
163 List<TagCorrection> tagCorrections = entry.getValue();
164 for (int i = 0; i < tagCorrections.size(); i++) {
165 if (tagTableMap.get(primitive).getCorrectionTableModel().getApply(i)) {
166 TagCorrection tagCorrection = tagCorrections.get(i);
167 if (tagCorrection.isKeyChanged() && !keysChanged.contains(tagCorrection.oldKey)) {
168 clone.remove(tagCorrection.oldKey);
169 }
170 clone.put(tagCorrection.newKey, tagCorrection.newValue);
171 keysChanged.add(tagCorrection.newKey);
172 }
173 }
174
175 // save the clone
176 if (!keysChanged.isEmpty()) {
177 commands.add(new ChangeCommand(primitive, clone));
178 }
179 }
180 for (Entry<OsmPrimitive, List<RoleCorrection>> entry : roleCorrectionMap.entrySet()) {
181 OsmPrimitive primitive = entry.getKey();
182 List<RoleCorrection> roleCorrections = entry.getValue();
183
184 for (int i = 0; i < roleCorrections.size(); i++) {
185 RoleCorrection roleCorrection = roleCorrections.get(i);
186 if (roleTableMap.get(primitive).getCorrectionTableModel().getApply(i)) {
187 commands.add(new ChangeRelationMemberRoleCommand(
188 roleCorrection.relation, roleCorrection.position, roleCorrection.newRole));
189 }
190 }
191 }
192 } else if (answer != JOptionPane.NO_OPTION)
193 throw new UserCancelException();
194 return commands;
195 }
196
197 return Collections.emptyList();
198 }
199}
Note: See TracBrowser for help on using the repository browser.