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

Last change on this file since 1023 was 1000, checked in by stoecker, 16 years ago

applied correction patch by Robin Rattay

File size: 5.9 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.List;
12import java.util.Map;
13
14import javax.swing.JLabel;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.command.ChangeCommand;
21import org.openstreetmap.josm.command.ChangePropertyCommand;
22import org.openstreetmap.josm.command.Command;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.Relation;
25import org.openstreetmap.josm.data.osm.RelationMember;
26import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
27import org.openstreetmap.josm.gui.JMultilineLabel;
28import org.openstreetmap.josm.tools.GBC;
29
30public abstract class TagCorrector<P extends OsmPrimitive> {
31
32 public abstract Collection<Command> execute(P primitive)
33 throws UserCancelException;
34
35 private String[] applicationOptions = new String[] {
36 tr("Apply selected changes"),
37 tr("Don't apply changes"),
38 tr("Cancel")
39 };
40
41 protected Collection<Command> applyCorrections(
42 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap,
43 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap,
44 String description) throws UserCancelException {
45
46 boolean hasCorrections = false;
47 for (List<TagCorrection> tagCorrectionList : tagCorrectionsMap.values()) {
48 if (!tagCorrectionList.isEmpty()) {
49 hasCorrections = true;
50 break;
51 }
52 }
53
54 if (!hasCorrections)
55 for (List<RoleCorrection> roleCorrectionList : roleCorrectionMap
56 .values()) {
57 if (!roleCorrectionList.isEmpty()) {
58 hasCorrections = true;
59 break;
60 }
61 }
62
63 if (hasCorrections) {
64 Collection<Command> commands = new ArrayList<Command>();
65 Map<OsmPrimitive, TagCorrectionTable> tagTableMap =
66 new HashMap<OsmPrimitive, TagCorrectionTable>();
67 Map<OsmPrimitive, RoleCorrectionTable> roleTableMap =
68 new HashMap<OsmPrimitive, RoleCorrectionTable>();
69
70 NameVisitor nameVisitor = new NameVisitor();
71
72 final JPanel p = new JPanel(new GridBagLayout());
73
74 final JMultilineLabel label1 = new JMultilineLabel(description);
75 label1.setMaxWidth(400);
76 p.add(label1, GBC.eop());
77
78 final JMultilineLabel label2 = new JMultilineLabel(
79 tr("Please select which property changes you want to apply."));
80 label2.setMaxWidth(400);
81 p.add(label2, GBC.eop());
82
83 for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) {
84 final List<TagCorrection> tagCorrections = tagCorrectionsMap
85 .get(primitive);
86
87 if (tagCorrections.isEmpty())
88 continue;
89
90 primitive.visit(nameVisitor);
91
92 final JLabel propertiesLabel = new JLabel(tr("Properties of "));
93 p.add(propertiesLabel, GBC.std());
94
95 final JLabel primitiveLabel = new JLabel(
96 nameVisitor.name + ":", nameVisitor.icon, JLabel.LEFT);
97 p.add(primitiveLabel, GBC.eol());
98
99 final TagCorrectionTable table = new TagCorrectionTable(
100 tagCorrections);
101 final JScrollPane scrollPane = new JScrollPane(table);
102 p.add(scrollPane, GBC.eop());
103
104 tagTableMap.put(primitive, table);
105 }
106
107 for (OsmPrimitive primitive : roleCorrectionMap.keySet()) {
108 final List<RoleCorrection> roleCorrections = roleCorrectionMap
109 .get(primitive);
110 if (roleCorrections.isEmpty())
111 continue;
112
113 primitive.visit(nameVisitor);
114
115 final JLabel rolesLabel = new JLabel(
116 tr("Roles in relations refering to"));
117 p.add(rolesLabel, GBC.std());
118
119 final JLabel primitiveLabel = new JLabel(
120 nameVisitor.name + ":", nameVisitor.icon, JLabel.LEFT);
121 p.add(primitiveLabel, GBC.eol());
122
123 final RoleCorrectionTable table = new RoleCorrectionTable(
124 roleCorrections);
125 final JScrollPane scrollPane = new JScrollPane(table);
126 p.add(scrollPane, GBC.eop());
127
128 roleTableMap.put(primitive, table);
129 }
130
131 int answer = JOptionPane.showOptionDialog(Main.parent, p,
132 tr("Automatic tag correction"), JOptionPane.YES_NO_CANCEL_OPTION,
133 JOptionPane.PLAIN_MESSAGE, null,
134 applicationOptions, applicationOptions[0]);
135
136 if (answer == JOptionPane.YES_OPTION) {
137 for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) {
138 List<TagCorrection> tagCorrections = tagCorrectionsMap
139 .get(primitive);
140 for (int i = 0; i < tagCorrections.size(); i++) {
141 if (tagTableMap.get(primitive)
142 .getCorrectionTableModel().getApply(i)) {
143 TagCorrection tagCorrection = tagCorrections.get(i);
144 if (tagCorrection.isKeyChanged())
145 commands.add(new ChangePropertyCommand(
146 primitive, tagCorrection.oldKey, null));
147 commands.add(new ChangePropertyCommand(primitive,
148 tagCorrection.newKey,
149 tagCorrection.newValue));
150 }
151 }
152 }
153 for (OsmPrimitive primitive : roleCorrectionMap.keySet()) {
154 List<RoleCorrection> roleCorrections = roleCorrectionMap
155 .get(primitive);
156 for (int i = 0; i < roleCorrections.size(); i++) {
157 if (roleTableMap.get(primitive)
158 .getCorrectionTableModel().getApply(i)) {
159 RoleCorrection roleCorrection = roleCorrections
160 .get(i);
161 Relation newRelation = new Relation(
162 roleCorrection.relation);
163 for (RelationMember member : newRelation.members)
164 if (member.equals(roleCorrection.member))
165 member.role = roleCorrection.newRole;
166 commands.add(new ChangeCommand(
167 roleCorrection.relation, newRelation));
168 }
169 }
170 }
171 } else if (answer != JOptionPane.NO_OPTION) {
172 throw new UserCancelException();
173 }
174 return commands;
175 }
176
177 return Collections.emptyList();
178 }
179}
Note: See TracBrowser for help on using the repository browser.