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

Last change on this file since 8308 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 8.1 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.Set;
15import java.util.Map.Entry;
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
118 final RoleCorrectionTable table = new RoleCorrectionTable(roleCorrections);
119 final JScrollPane scrollPane = new JScrollPane(table);
120 p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
121
122 roleTableMap.put(primitive, table);
123 }
124
125 int answer = JOptionPane.showOptionDialog(
126 Main.parent,
127 p,
128 tr("Automatic tag correction"),
129 JOptionPane.YES_NO_CANCEL_OPTION,
130 JOptionPane.PLAIN_MESSAGE,
131 null,
132 applicationOptions,
133 applicationOptions[0]
134 );
135
136 if (answer == JOptionPane.YES_OPTION) {
137 for (Entry<OsmPrimitive, List<TagCorrection>> entry : tagCorrectionsMap.entrySet()) {
138 List<TagCorrection> tagCorrections = entry.getValue();
139 OsmPrimitive primitive = entry.getKey();
140
141 // create the clone
142 OsmPrimitive clone = null;
143 if (primitive instanceof Way) {
144 clone = new Way((Way)primitive);
145 } else if (primitive instanceof Node) {
146 clone = new Node((Node)primitive);
147 } else if (primitive instanceof Relation) {
148 clone = new Relation((Relation)primitive);
149 } else
150 throw new AssertionError();
151
152 // use this structure to remember keys that have been set already so that
153 // they're not dropped by a later step
154 Set<String> keysChanged = new HashSet<>();
155
156 // apply all changes to this clone
157 for (int i = 0; i < tagCorrections.size(); i++) {
158 if (tagTableMap.get(primitive).getCorrectionTableModel().getApply(i)) {
159 TagCorrection tagCorrection = tagCorrections.get(i);
160 if (tagCorrection.isKeyChanged() && !keysChanged.contains(tagCorrection.oldKey)) {
161 clone.remove(tagCorrection.oldKey);
162 }
163 clone.put(tagCorrection.newKey, tagCorrection.newValue);
164 keysChanged.add(tagCorrection.newKey);
165 }
166 }
167
168 // save the clone
169 if (!keysChanged.isEmpty()) {
170 commands.add(new ChangeCommand(primitive, clone));
171 }
172 }
173 for (Entry<OsmPrimitive, List<RoleCorrection>> entry : roleCorrectionMap.entrySet()) {
174 OsmPrimitive primitive = entry.getKey();
175 List<RoleCorrection> roleCorrections = entry.getValue();
176
177 for (int i = 0; i < roleCorrections.size(); i++) {
178 RoleCorrection roleCorrection = roleCorrections.get(i);
179 if (roleTableMap.get(primitive).getCorrectionTableModel().getApply(i)) {
180 commands.add(new ChangeRelationMemberRoleCommand(roleCorrection.relation, roleCorrection.position, roleCorrection.newRole));
181 }
182 }
183 }
184 } else if (answer != JOptionPane.NO_OPTION)
185 throw new UserCancelException();
186 return commands;
187 }
188
189 return Collections.emptyList();
190 }
191}
Note: See TracBrowser for help on using the repository browser.