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