source: josm/trunk/src/org/openstreetmap/josm/corrector/TagCorrector.java@ 6986

Last change on this file since 6986 was 6944, checked in by Don-vip, 10 years ago

fix #3764 - should fix UI problem in tag correction dialog (cannot reproduce)

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.corrector;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.List;
13import java.util.Map;
14import java.util.Set;
15import java.util.Map.Entry;
16
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20import javax.swing.JScrollPane;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.command.ChangeCommand;
24import org.openstreetmap.josm.command.ChangeRelationMemberRoleCommand;
25import org.openstreetmap.josm.command.Command;
26import org.openstreetmap.josm.data.osm.Node;
27import org.openstreetmap.josm.data.osm.OsmPrimitive;
28import org.openstreetmap.josm.data.osm.Relation;
29import org.openstreetmap.josm.data.osm.Way;
30import org.openstreetmap.josm.gui.DefaultNameFormatter;
31import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
32import org.openstreetmap.josm.tools.GBC;
33import 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 */
43public 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<Command>();
60 Map<OsmPrimitive, TagCorrectionTable> tagTableMap =
61 new HashMap<OsmPrimitive, TagCorrectionTable>();
62 Map<OsmPrimitive, RoleCorrectionTable> roleTableMap =
63 new HashMap<OsmPrimitive, RoleCorrectionTable>();
64
65 final JPanel p = new JPanel(new GridBagLayout());
66
67 final JMultilineLabel label1 = new JMultilineLabel(description);
68 label1.setMaxWidth(600);
69 p.add(label1, GBC.eop().anchor(GBC.CENTER).fill(GBC.HORIZONTAL));
70
71 final JMultilineLabel label2 = new JMultilineLabel(
72 tr("Please select which changes you want to apply."));
73 label2.setMaxWidth(600);
74 p.add(label2, GBC.eop().anchor(GBC.CENTER).fill(GBC.HORIZONTAL));
75
76 for (Entry<OsmPrimitive, List<TagCorrection>> entry : tagCorrectionsMap.entrySet()) {
77 final OsmPrimitive primitive = entry.getKey();
78 final List<TagCorrection> tagCorrections = entry.getValue();
79
80 if (tagCorrections.isEmpty()) {
81 continue;
82 }
83
84 final JLabel propertiesLabel = new JLabel(tr("Tags of "));
85 p.add(propertiesLabel, GBC.std());
86
87 final JLabel primitiveLabel = new JLabel(
88 primitive.getDisplayName(DefaultNameFormatter.getInstance()) + ":",
89 ImageProvider.get(primitive.getDisplayType()),
90 JLabel.LEFT
91 );
92 p.add(primitiveLabel, GBC.eol());
93
94 final TagCorrectionTable table = new TagCorrectionTable(
95 tagCorrections);
96 final JScrollPane scrollPane = new JScrollPane(table);
97 p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
98
99 tagTableMap.put(primitive, table);
100 }
101
102 for (Entry<OsmPrimitive, List<RoleCorrection>> entry : roleCorrectionMap.entrySet()) {
103 final OsmPrimitive primitive = entry.getKey();
104 final List<RoleCorrection> roleCorrections = entry.getValue();
105
106 if (roleCorrections.isEmpty()) {
107 continue;
108 }
109
110 final JLabel rolesLabel = new JLabel(
111 tr("Roles in relations referring to"));
112 p.add(rolesLabel, GBC.std());
113
114 final JLabel primitiveLabel = new JLabel(
115 primitive.getDisplayName(DefaultNameFormatter.getInstance()),
116 ImageProvider.get(primitive.getDisplayType()),
117 JLabel.LEFT
118 );
119 p.add(primitiveLabel, GBC.eol());
120
121 final RoleCorrectionTable table = new RoleCorrectionTable(
122 roleCorrections);
123 final JScrollPane scrollPane = new JScrollPane(table);
124 p.add(scrollPane, GBC.eop().fill(GBC.HORIZONTAL));
125
126 roleTableMap.put(primitive, table);
127 }
128
129 int answer = JOptionPane.showOptionDialog(
130 Main.parent,
131 p,
132 tr("Automatic tag correction"),
133 JOptionPane.YES_NO_CANCEL_OPTION,
134 JOptionPane.PLAIN_MESSAGE,
135 null,
136 applicationOptions,
137 applicationOptions[0]
138 );
139
140 if (answer == JOptionPane.YES_OPTION) {
141 for (Entry<OsmPrimitive, List<TagCorrection>> entry : tagCorrectionsMap.entrySet()) {
142 List<TagCorrection> tagCorrections = entry.getValue();
143 OsmPrimitive primitive = entry.getKey();
144
145 // create the clone
146 OsmPrimitive clone = null;
147 if (primitive instanceof Way) {
148 clone = new Way((Way)primitive);
149 } else if (primitive instanceof Node) {
150 clone = new Node((Node)primitive);
151 } else if (primitive instanceof Relation) {
152 clone = new Relation((Relation)primitive);
153 } else
154 throw new AssertionError();
155
156 // use this structure to remember keys that have been set already so that
157 // they're not dropped by a later step
158 Set<String> keysChanged = new HashSet<String>();
159
160 // apply all changes to this clone
161 for (int i = 0; i < tagCorrections.size(); i++) {
162 if (tagTableMap.get(primitive).getCorrectionTableModel().getApply(i)) {
163 TagCorrection tagCorrection = tagCorrections.get(i);
164 if (tagCorrection.isKeyChanged() && !keysChanged.contains(tagCorrection.oldKey)) {
165 clone.remove(tagCorrection.oldKey);
166 }
167 clone.put(tagCorrection.newKey, tagCorrection.newValue);
168 keysChanged.add(tagCorrection.newKey);
169 }
170 }
171
172 // save the clone
173 if (!keysChanged.isEmpty()) {
174 commands.add(new ChangeCommand(primitive, clone));
175 }
176 }
177 for (Entry<OsmPrimitive, List<RoleCorrection>> entry : roleCorrectionMap.entrySet()) {
178 OsmPrimitive primitive = entry.getKey();
179 List<RoleCorrection> roleCorrections = entry.getValue();
180
181 for (int i = 0; i < roleCorrections.size(); i++) {
182 RoleCorrection roleCorrection = roleCorrections.get(i);
183 if (roleTableMap.get(primitive).getCorrectionTableModel().getApply(i)) {
184 commands.add(new ChangeRelationMemberRoleCommand(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}
Note: See TracBrowser for help on using the repository browser.