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