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