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

Last change on this file since 8855 was 8846, checked in by Don-vip, 9 years ago

sonar - fb-contrib - minor performance improvements:

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