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.JMultilineLabel;
|
---|
31 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
32 | import org.openstreetmap.josm.gui.PrimitiveNameFormatter;
|
---|
33 | import org.openstreetmap.josm.tools.GBC;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 |
|
---|
36 | public abstract class TagCorrector<P extends OsmPrimitive> {
|
---|
37 | private static final PrimitiveNameFormatter NAME_FORMATTER = new PrimitiveNameFormatter();
|
---|
38 |
|
---|
39 | public abstract Collection<Command> execute(P primitive, P oldprimitive)
|
---|
40 | throws UserCancelException;
|
---|
41 |
|
---|
42 | private String[] applicationOptions = new String[] {
|
---|
43 | tr("Apply selected changes"),
|
---|
44 | tr("Don't apply changes"),
|
---|
45 | tr("Cancel")
|
---|
46 | };
|
---|
47 |
|
---|
48 | protected Collection<Command> applyCorrections(
|
---|
49 | Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap,
|
---|
50 | Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap,
|
---|
51 | String description) throws UserCancelException {
|
---|
52 |
|
---|
53 | boolean hasCorrections = false;
|
---|
54 | for (List<TagCorrection> tagCorrectionList : tagCorrectionsMap.values()) {
|
---|
55 | if (!tagCorrectionList.isEmpty()) {
|
---|
56 | hasCorrections = true;
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (!hasCorrections) {
|
---|
62 | for (List<RoleCorrection> roleCorrectionList : roleCorrectionMap
|
---|
63 | .values()) {
|
---|
64 | if (!roleCorrectionList.isEmpty()) {
|
---|
65 | hasCorrections = true;
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (hasCorrections) {
|
---|
72 | Collection<Command> commands = new ArrayList<Command>();
|
---|
73 | Map<OsmPrimitive, TagCorrectionTable> tagTableMap =
|
---|
74 | new HashMap<OsmPrimitive, TagCorrectionTable>();
|
---|
75 | Map<OsmPrimitive, RoleCorrectionTable> roleTableMap =
|
---|
76 | new HashMap<OsmPrimitive, RoleCorrectionTable>();
|
---|
77 |
|
---|
78 | //NameVisitor nameVisitor = new NameVisitor();
|
---|
79 |
|
---|
80 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
81 |
|
---|
82 | final JMultilineLabel label1 = new JMultilineLabel(description);
|
---|
83 | label1.setMaxWidth(600);
|
---|
84 | p.add(label1, GBC.eop().anchor(GBC.CENTER));
|
---|
85 |
|
---|
86 | final JMultilineLabel label2 = new JMultilineLabel(
|
---|
87 | tr("Please select which property changes you want to apply."));
|
---|
88 | label2.setMaxWidth(600);
|
---|
89 | p.add(label2, GBC.eop().anchor(GBC.CENTER));
|
---|
90 |
|
---|
91 | for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) {
|
---|
92 | final List<TagCorrection> tagCorrections = tagCorrectionsMap
|
---|
93 | .get(primitive);
|
---|
94 |
|
---|
95 | if (tagCorrections.isEmpty()) {
|
---|
96 | continue;
|
---|
97 | }
|
---|
98 |
|
---|
99 | final JLabel propertiesLabel = new JLabel(tr("Properties of "));
|
---|
100 | p.add(propertiesLabel, GBC.std());
|
---|
101 |
|
---|
102 | final JLabel primitiveLabel = new JLabel(
|
---|
103 | NAME_FORMATTER.getName(primitive) + ":",
|
---|
104 | ImageProvider.get(OsmPrimitiveType.from(primitive)),
|
---|
105 | JLabel.LEFT
|
---|
106 | );
|
---|
107 | p.add(primitiveLabel, GBC.eol());
|
---|
108 |
|
---|
109 | final TagCorrectionTable table = new TagCorrectionTable(
|
---|
110 | tagCorrections);
|
---|
111 | final JScrollPane scrollPane = new JScrollPane(table);
|
---|
112 | p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
113 |
|
---|
114 | tagTableMap.put(primitive, table);
|
---|
115 | }
|
---|
116 |
|
---|
117 | for (OsmPrimitive primitive : roleCorrectionMap.keySet()) {
|
---|
118 | final List<RoleCorrection> roleCorrections = roleCorrectionMap
|
---|
119 | .get(primitive);
|
---|
120 | if (roleCorrections.isEmpty()) {
|
---|
121 | continue;
|
---|
122 | }
|
---|
123 |
|
---|
124 | final JLabel rolesLabel = new JLabel(
|
---|
125 | tr("Roles in relations referring to"));
|
---|
126 | p.add(rolesLabel, GBC.std());
|
---|
127 |
|
---|
128 | final JLabel primitiveLabel = new JLabel(
|
---|
129 | NAME_FORMATTER.getName(primitive),
|
---|
130 | ImageProvider.get(OsmPrimitiveType.from(primitive)),
|
---|
131 | JLabel.LEFT
|
---|
132 | );
|
---|
133 | p.add(primitiveLabel, GBC.eol());
|
---|
134 |
|
---|
135 | final RoleCorrectionTable table = new RoleCorrectionTable(
|
---|
136 | roleCorrections);
|
---|
137 | final JScrollPane scrollPane = new JScrollPane(table);
|
---|
138 | p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
139 |
|
---|
140 | roleTableMap.put(primitive, table);
|
---|
141 | }
|
---|
142 |
|
---|
143 | int answer = OptionPaneUtil.showOptionDialog(
|
---|
144 | Main.parent,
|
---|
145 | p,
|
---|
146 | tr("Automatic tag correction"),
|
---|
147 | JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
148 | JOptionPane.PLAIN_MESSAGE,
|
---|
149 | applicationOptions,
|
---|
150 | applicationOptions[0]
|
---|
151 | );
|
---|
152 |
|
---|
153 | if (answer == JOptionPane.YES_OPTION) {
|
---|
154 | for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) {
|
---|
155 | List<TagCorrection> tagCorrections =
|
---|
156 | tagCorrectionsMap.get(primitive);
|
---|
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 (OsmPrimitive primitive : roleCorrectionMap.keySet()) {
|
---|
190 | List<RoleCorrection> roleCorrections = roleCorrectionMap
|
---|
191 | .get(primitive);
|
---|
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 | }
|
---|