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

Last change on this file since 2381 was 2017, checked in by Gubaer, 15 years ago

removed OptionPaneUtil
cleanup of deprecated Layer API
cleanup of deprecated APIs in OsmPrimitive and Way
cleanup of imports

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