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.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
21 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
22 | import org.openstreetmap.josm.command.Command;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
25 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
26 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
27 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
28 | import org.openstreetmap.josm.tools.GBC;
|
---|
29 |
|
---|
30 | public 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 | }
|
---|