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