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

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

removed dependencies to Main.ds, removed Main.ds
removed AddVisitor, NameVisitor, DeleteVisitor - unnecessary double dispatching for these simple cases

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