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

Last change on this file since 2676 was 2676, checked in by jttt, 14 years ago

Fix some of the warnings found by FindBugs

File size: 8.4 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.OsmPrimitiveType;
29import org.openstreetmap.josm.data.osm.Relation;
30import org.openstreetmap.josm.data.osm.Way;
31import org.openstreetmap.josm.gui.DefaultNameFormatter;
32import org.openstreetmap.josm.gui.JMultilineLabel;
33import org.openstreetmap.josm.tools.GBC;
34import org.openstreetmap.josm.tools.ImageProvider;
35
36public abstract class TagCorrector<P extends OsmPrimitive> {
37
38 public abstract Collection<Command> execute(P primitive, P oldprimitive)
39 throws UserCancelException;
40
41 private String[] applicationOptions = new String[] {
42 tr("Apply selected changes"),
43 tr("Don't apply changes"),
44 tr("Cancel")
45 };
46
47 protected Collection<Command> applyCorrections(
48 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap,
49 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap,
50 String description) throws UserCancelException {
51
52 boolean hasCorrections = false;
53 for (List<TagCorrection> tagCorrectionList : tagCorrectionsMap.values()) {
54 if (!tagCorrectionList.isEmpty()) {
55 hasCorrections = true;
56 break;
57 }
58 }
59
60 if (!hasCorrections) {
61 for (List<RoleCorrection> roleCorrectionList : roleCorrectionMap
62 .values()) {
63 if (!roleCorrectionList.isEmpty()) {
64 hasCorrections = true;
65 break;
66 }
67 }
68 }
69
70 if (hasCorrections) {
71 Collection<Command> commands = new ArrayList<Command>();
72 Map<OsmPrimitive, TagCorrectionTable> tagTableMap =
73 new HashMap<OsmPrimitive, TagCorrectionTable>();
74 Map<OsmPrimitive, RoleCorrectionTable> roleTableMap =
75 new HashMap<OsmPrimitive, RoleCorrectionTable>();
76
77 //NameVisitor nameVisitor = new NameVisitor();
78
79 final JPanel p = new JPanel(new GridBagLayout());
80
81 final JMultilineLabel label1 = new JMultilineLabel(description);
82 label1.setMaxWidth(600);
83 p.add(label1, GBC.eop().anchor(GBC.CENTER));
84
85 final JMultilineLabel label2 = new JMultilineLabel(
86 tr("Please select which property changes you want to apply."));
87 label2.setMaxWidth(600);
88 p.add(label2, GBC.eop().anchor(GBC.CENTER));
89
90 for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) {
91 final List<TagCorrection> tagCorrections = tagCorrectionsMap
92 .get(primitive);
93
94 if (tagCorrections.isEmpty()) {
95 continue;
96 }
97
98 final JLabel propertiesLabel = new JLabel(tr("Properties of "));
99 p.add(propertiesLabel, GBC.std());
100
101 final JLabel primitiveLabel = new JLabel(
102 primitive.getDisplayName(DefaultNameFormatter.getInstance()) + ":",
103 ImageProvider.get(OsmPrimitiveType.from(primitive)),
104 JLabel.LEFT
105 );
106 p.add(primitiveLabel, GBC.eol());
107
108 final TagCorrectionTable table = new TagCorrectionTable(
109 tagCorrections);
110 final JScrollPane scrollPane = new JScrollPane(table);
111 p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
112
113 tagTableMap.put(primitive, table);
114 }
115
116 for (OsmPrimitive primitive : roleCorrectionMap.keySet()) {
117 final List<RoleCorrection> roleCorrections = roleCorrectionMap
118 .get(primitive);
119 if (roleCorrections.isEmpty()) {
120 continue;
121 }
122
123 final JLabel rolesLabel = new JLabel(
124 tr("Roles in relations referring to"));
125 p.add(rolesLabel, GBC.std());
126
127 final JLabel primitiveLabel = new JLabel(
128 primitive.getDisplayName(DefaultNameFormatter.getInstance()),
129 ImageProvider.get(OsmPrimitiveType.from(primitive)),
130 JLabel.LEFT
131 );
132 p.add(primitiveLabel, GBC.eol());
133
134 final RoleCorrectionTable table = new RoleCorrectionTable(
135 roleCorrections);
136 final JScrollPane scrollPane = new JScrollPane(table);
137 p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
138
139 roleTableMap.put(primitive, table);
140 }
141
142 int answer = JOptionPane.showOptionDialog(
143 Main.parent,
144 p,
145 tr("Automatic tag correction"),
146 JOptionPane.YES_NO_CANCEL_OPTION,
147 JOptionPane.PLAIN_MESSAGE,
148 null,
149 applicationOptions,
150 applicationOptions[0]
151 );
152
153 if (answer == JOptionPane.YES_OPTION) {
154 for (Entry<OsmPrimitive, List<TagCorrection>> entry : tagCorrectionsMap.entrySet()) {
155 List<TagCorrection> tagCorrections = entry.getValue();
156 OsmPrimitive primitive = entry.getKey();
157
158 // create the clone
159 OsmPrimitive clone = null;
160 if (primitive instanceof Way) {
161 clone = new Way((Way)primitive);
162 } else if (primitive instanceof Node) {
163 clone = new Node((Node)primitive);
164 } else if (primitive instanceof Relation) {
165 clone = new Relation((Relation)primitive);
166 }
167
168 // use this structure to remember keys that have been set already so that
169 // they're not dropped by a later step
170 Set<String> keysChanged = new HashSet<String>();
171
172 // apply all changes to this clone
173 for (int i = 0; i < tagCorrections.size(); i++) {
174 if (tagTableMap.get(primitive).getCorrectionTableModel().getApply(i)) {
175 TagCorrection tagCorrection = tagCorrections.get(i);
176 if (tagCorrection.isKeyChanged() && !keysChanged.contains(tagCorrection.oldKey)) {
177 clone.remove(tagCorrection.oldKey);
178 }
179 clone.put(tagCorrection.newKey, tagCorrection.newValue);
180 keysChanged.add(tagCorrection.newKey);
181 }
182 }
183
184 // save the clone
185 if (!keysChanged.isEmpty()) {
186 commands.add(new ChangeCommand(primitive, clone));
187 }
188 }
189 for (Entry<OsmPrimitive, List<RoleCorrection>> entry : roleCorrectionMap.entrySet()) {
190 OsmPrimitive primitive = entry.getKey();
191 List<RoleCorrection> roleCorrections = entry.getValue();
192
193 for (int i = 0; i < roleCorrections.size(); i++) {
194 RoleCorrection roleCorrection = roleCorrections.get(i);
195 if (roleTableMap.get(primitive).getCorrectionTableModel().getApply(i)) {
196 commands.add(new ChangeRelationMemberRoleCommand(roleCorrection.relation, roleCorrection.position, roleCorrection.newRole));
197 }
198 }
199 }
200 } else if (answer != JOptionPane.NO_OPTION)
201 throw new UserCancelException();
202 return commands;
203 }
204
205 return Collections.emptyList();
206 }
207}
Note: See TracBrowser for help on using the repository browser.