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