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

Last change on this file since 1724 was 1617, checked in by stoecker, 15 years ago

fix #2645 - patch by Teemu Koskinen - cleanup relation role handling on way reversing

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