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