source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/tags/PasteTagsConflictResolverDialog.java@ 15586

Last change on this file since 15586 was 15586, checked in by Don-vip, 4 years ago

code cleanup

  • Property svn:eol-style set to native
File size: 19.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.tags;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.BorderLayout;
8import java.awt.Component;
9import java.awt.Dimension;
10import java.awt.FlowLayout;
11import java.awt.Font;
12import java.awt.GridBagConstraints;
13import java.awt.GridBagLayout;
14import java.awt.Insets;
15import java.awt.event.ActionEvent;
16import java.beans.PropertyChangeEvent;
17import java.beans.PropertyChangeListener;
18import java.util.ArrayList;
19import java.util.EnumMap;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23import java.util.Map.Entry;
24
25import javax.swing.AbstractAction;
26import javax.swing.Action;
27import javax.swing.ImageIcon;
28import javax.swing.JButton;
29import javax.swing.JDialog;
30import javax.swing.JLabel;
31import javax.swing.JPanel;
32import javax.swing.JTabbedPane;
33import javax.swing.JTable;
34import javax.swing.UIManager;
35import javax.swing.table.DefaultTableModel;
36import javax.swing.table.TableCellRenderer;
37
38import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
39import org.openstreetmap.josm.data.osm.TagCollection;
40import org.openstreetmap.josm.gui.tagging.TagTableColumnModelBuilder;
41import org.openstreetmap.josm.gui.util.GuiHelper;
42import org.openstreetmap.josm.gui.util.WindowGeometry;
43import org.openstreetmap.josm.tools.ImageProvider;
44import org.openstreetmap.josm.tools.InputMapUtils;
45
46/**
47 * This conflict resolution dialog is used when tags are pasted from the clipboard that conflict with the existing ones.
48 */
49public class PasteTagsConflictResolverDialog extends JDialog implements PropertyChangeListener {
50 static final Map<OsmPrimitiveType, String> PANE_TITLES;
51 static {
52 PANE_TITLES = new EnumMap<>(OsmPrimitiveType.class);
53 PANE_TITLES.put(OsmPrimitiveType.NODE, tr("Tags from nodes"));
54 PANE_TITLES.put(OsmPrimitiveType.WAY, tr("Tags from ways"));
55 PANE_TITLES.put(OsmPrimitiveType.RELATION, tr("Tags from relations"));
56 }
57
58 enum Mode {
59 RESOLVING_ONE_TAGCOLLECTION_ONLY,
60 RESOLVING_TYPED_TAGCOLLECTIONS
61 }
62
63 private final TagConflictResolverModel model = new TagConflictResolverModel();
64 private final transient Map<OsmPrimitiveType, TagConflictResolver> resolvers = new EnumMap<>(OsmPrimitiveType.class);
65 private final JTabbedPane tpResolvers = new JTabbedPane();
66 private Mode mode;
67 private boolean canceled;
68
69 private final ImageIcon iconResolved = ImageProvider.get("dialogs/conflict", "tagconflictresolved");
70 private final ImageIcon iconUnresolved = ImageProvider.get("dialogs/conflict", "tagconflictunresolved");
71 private final StatisticsTableModel statisticsModel = new StatisticsTableModel();
72 private final JPanel pnlTagResolver = new JPanel(new BorderLayout());
73
74 /**
75 * Constructs a new {@code PasteTagsConflictResolverDialog}.
76 * @param owner parent component
77 */
78 public PasteTagsConflictResolverDialog(Component owner) {
79 super(GuiHelper.getFrameForComponent(owner), ModalityType.DOCUMENT_MODAL);
80 build();
81 }
82
83 protected final void build() {
84 setTitle(tr("Conflicts in pasted tags"));
85 for (OsmPrimitiveType type: OsmPrimitiveType.dataValues()) {
86 TagConflictResolverModel tagModel = new TagConflictResolverModel();
87 resolvers.put(type, new TagConflictResolver(tagModel));
88 tagModel.addPropertyChangeListener(this);
89 }
90 getContentPane().setLayout(new GridBagLayout());
91 mode = null;
92 GridBagConstraints gc = new GridBagConstraints();
93 gc.gridx = 0;
94 gc.gridy = 0;
95 gc.fill = GridBagConstraints.HORIZONTAL;
96 gc.weightx = 1.0;
97 gc.weighty = 0.0;
98 getContentPane().add(buildSourceAndTargetInfoPanel(), gc);
99 gc.gridx = 0;
100 gc.gridy = 1;
101 gc.fill = GridBagConstraints.BOTH;
102 gc.weightx = 1.0;
103 gc.weighty = 1.0;
104 getContentPane().add(pnlTagResolver, gc);
105 gc.gridx = 0;
106 gc.gridy = 2;
107 gc.fill = GridBagConstraints.HORIZONTAL;
108 gc.weightx = 1.0;
109 gc.weighty = 0.0;
110 getContentPane().add(buildButtonPanel(), gc);
111 InputMapUtils.addEscapeAction(getRootPane(), new CancelAction());
112 }
113
114 protected JPanel buildButtonPanel() {
115 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
116
117 // -- apply button
118 ApplyAction applyAction = new ApplyAction();
119 model.addPropertyChangeListener(applyAction);
120 for (TagConflictResolver r : resolvers.values()) {
121 r.getModel().addPropertyChangeListener(applyAction);
122 }
123 pnl.add(new JButton(applyAction));
124
125 // -- cancel button
126 CancelAction cancelAction = new CancelAction();
127 pnl.add(new JButton(cancelAction));
128
129 return pnl;
130 }
131
132 protected JPanel buildSourceAndTargetInfoPanel() {
133 JPanel pnl = new JPanel(new BorderLayout());
134 pnl.add(new StatisticsInfoTable(statisticsModel), BorderLayout.CENTER);
135 return pnl;
136 }
137
138 /**
139 * Initializes the conflict resolver for a specific type of primitives
140 *
141 * @param type the type of primitives
142 * @param tc the tags belonging to this type of primitives
143 * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
144 */
145 protected void initResolver(OsmPrimitiveType type, TagCollection tc, Map<OsmPrimitiveType, Integer> targetStatistics) {
146 TagConflictResolver resolver = resolvers.get(type);
147 resolver.getModel().populate(tc, tc.getKeysWithMultipleValues());
148 resolver.getModel().prepareDefaultTagDecisions();
149 if (!tc.isEmpty() && targetStatistics.get(type) != null && targetStatistics.get(type) > 0) {
150 tpResolvers.add(PANE_TITLES.get(type), resolver);
151 }
152 }
153
154 /**
155 * Populates the conflict resolver with one tag collection
156 *
157 * @param tagsForAllPrimitives the tag collection
158 * @param sourceStatistics histogram of tag source, number of primitives of each type in the source
159 * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
160 */
161 public void populate(TagCollection tagsForAllPrimitives, Map<OsmPrimitiveType, Integer> sourceStatistics,
162 Map<OsmPrimitiveType, Integer> targetStatistics) {
163 mode = Mode.RESOLVING_ONE_TAGCOLLECTION_ONLY;
164 tagsForAllPrimitives = tagsForAllPrimitives == null ? new TagCollection() : tagsForAllPrimitives;
165 sourceStatistics = sourceStatistics == null ? new HashMap<>() : sourceStatistics;
166 targetStatistics = targetStatistics == null ? new HashMap<>() : targetStatistics;
167
168 // init the resolver
169 //
170 model.populate(tagsForAllPrimitives, tagsForAllPrimitives.getKeysWithMultipleValues());
171 model.prepareDefaultTagDecisions();
172
173 // prepare the dialog with one tag resolver
174 pnlTagResolver.removeAll();
175 pnlTagResolver.add(new TagConflictResolver(model), BorderLayout.CENTER);
176
177 statisticsModel.reset();
178 StatisticsInfo info = new StatisticsInfo();
179 info.numTags = tagsForAllPrimitives.getKeys().size();
180 info.sourceInfo.putAll(sourceStatistics);
181 info.targetInfo.putAll(targetStatistics);
182 statisticsModel.append(info);
183 validate();
184 }
185
186 protected int getNumResolverTabs() {
187 return tpResolvers.getTabCount();
188 }
189
190 protected TagConflictResolver getResolver(int idx) {
191 return (TagConflictResolver) tpResolvers.getComponentAt(idx);
192 }
193
194 /**
195 * Populate the tag conflict resolver with tags for each type of primitives
196 *
197 * @param tagsForNodes the tags belonging to nodes in the paste source
198 * @param tagsForWays the tags belonging to way in the paste source
199 * @param tagsForRelations the tags belonging to relations in the paste source
200 * @param sourceStatistics histogram of tag source, number of primitives of each type in the source
201 * @param targetStatistics histogram of paste targets, number of primitives of each type in the paste target
202 */
203 public void populate(TagCollection tagsForNodes, TagCollection tagsForWays, TagCollection tagsForRelations,
204 Map<OsmPrimitiveType, Integer> sourceStatistics, Map<OsmPrimitiveType, Integer> targetStatistics) {
205 tagsForNodes = (tagsForNodes == null) ? new TagCollection() : tagsForNodes;
206 tagsForWays = (tagsForWays == null) ? new TagCollection() : tagsForWays;
207 tagsForRelations = (tagsForRelations == null) ? new TagCollection() : tagsForRelations;
208 if (tagsForNodes.isEmpty() && tagsForWays.isEmpty() && tagsForRelations.isEmpty()) {
209 populate(null, null, null);
210 return;
211 }
212 tpResolvers.removeAll();
213 initResolver(OsmPrimitiveType.NODE, tagsForNodes, targetStatistics);
214 initResolver(OsmPrimitiveType.WAY, tagsForWays, targetStatistics);
215 initResolver(OsmPrimitiveType.RELATION, tagsForRelations, targetStatistics);
216
217 pnlTagResolver.removeAll();
218 pnlTagResolver.add(tpResolvers, BorderLayout.CENTER);
219 mode = Mode.RESOLVING_TYPED_TAGCOLLECTIONS;
220 validate();
221 statisticsModel.reset();
222 if (!tagsForNodes.isEmpty()) {
223 StatisticsInfo info = new StatisticsInfo();
224 info.numTags = tagsForNodes.getKeys().size();
225 int numTargets = targetStatistics.get(OsmPrimitiveType.NODE) == null ? 0 : targetStatistics.get(OsmPrimitiveType.NODE);
226 if (numTargets > 0) {
227 info.sourceInfo.put(OsmPrimitiveType.NODE, sourceStatistics.get(OsmPrimitiveType.NODE));
228 info.targetInfo.put(OsmPrimitiveType.NODE, numTargets);
229 statisticsModel.append(info);
230 }
231 }
232 if (!tagsForWays.isEmpty()) {
233 StatisticsInfo info = new StatisticsInfo();
234 info.numTags = tagsForWays.getKeys().size();
235 int numTargets = targetStatistics.get(OsmPrimitiveType.WAY) == null ? 0 : targetStatistics.get(OsmPrimitiveType.WAY);
236 if (numTargets > 0) {
237 info.sourceInfo.put(OsmPrimitiveType.WAY, sourceStatistics.get(OsmPrimitiveType.WAY));
238 info.targetInfo.put(OsmPrimitiveType.WAY, numTargets);
239 statisticsModel.append(info);
240 }
241 }
242 if (!tagsForRelations.isEmpty()) {
243 StatisticsInfo info = new StatisticsInfo();
244 info.numTags = tagsForRelations.getKeys().size();
245 int numTargets = targetStatistics.get(OsmPrimitiveType.RELATION) == null ? 0 : targetStatistics.get(OsmPrimitiveType.RELATION);
246 if (numTargets > 0) {
247 info.sourceInfo.put(OsmPrimitiveType.RELATION, sourceStatistics.get(OsmPrimitiveType.RELATION));
248 info.targetInfo.put(OsmPrimitiveType.RELATION, numTargets);
249 statisticsModel.append(info);
250 }
251 }
252
253 for (int i = 0; i < getNumResolverTabs(); i++) {
254 if (!getResolver(i).getModel().isResolvedCompletely()) {
255 tpResolvers.setSelectedIndex(i);
256 break;
257 }
258 }
259 }
260
261 protected void setCanceled(boolean canceled) {
262 this.canceled = canceled;
263 }
264
265 public boolean isCanceled() {
266 return this.canceled;
267 }
268
269 final class CancelAction extends AbstractAction {
270
271 private CancelAction() {
272 putValue(Action.SHORT_DESCRIPTION, tr("Cancel conflict resolution"));
273 putValue(Action.NAME, tr("Cancel"));
274 new ImageProvider("cancel").getResource().attachImageIcon(this);
275 setEnabled(true);
276 }
277
278 @Override
279 public void actionPerformed(ActionEvent arg0) {
280 setVisible(false);
281 setCanceled(true);
282 }
283 }
284
285 final class ApplyAction extends AbstractAction implements PropertyChangeListener {
286
287 private ApplyAction() {
288 putValue(Action.SHORT_DESCRIPTION, tr("Apply resolved conflicts"));
289 putValue(Action.NAME, tr("Apply"));
290 new ImageProvider("ok").getResource().attachImageIcon(this);
291 updateEnabledState();
292 }
293
294 @Override
295 public void actionPerformed(ActionEvent arg0) {
296 setVisible(false);
297 }
298
299 void updateEnabledState() {
300 if (mode == null) {
301 setEnabled(false);
302 } else if (mode == Mode.RESOLVING_ONE_TAGCOLLECTION_ONLY) {
303 setEnabled(model.isResolvedCompletely());
304 } else {
305 setEnabled(resolvers.values().stream().allMatch(val -> val.getModel().isResolvedCompletely()));
306 }
307 }
308
309 @Override
310 public void propertyChange(PropertyChangeEvent evt) {
311 if (evt.getPropertyName().equals(TagConflictResolverModel.NUM_CONFLICTS_PROP)) {
312 updateEnabledState();
313 }
314 }
315 }
316
317 @Override
318 public void setVisible(boolean visible) {
319 if (visible) {
320 new WindowGeometry(
321 getClass().getName() + ".geometry",
322 WindowGeometry.centerOnScreen(new Dimension(600, 400))
323 ).applySafe(this);
324 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
325 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
326 }
327 super.setVisible(visible);
328 }
329
330 /**
331 * Returns conflict resolution.
332 * @return conflict resolution
333 */
334 public TagCollection getResolution() {
335 return model.getResolution();
336 }
337
338 public TagCollection getResolution(OsmPrimitiveType type) {
339 if (type == null) return null;
340 return resolvers.get(type).getModel().getResolution();
341 }
342
343 @Override
344 public void propertyChange(PropertyChangeEvent evt) {
345 if (evt.getPropertyName().equals(TagConflictResolverModel.NUM_CONFLICTS_PROP)) {
346 TagConflictResolverModel tagModel = (TagConflictResolverModel) evt.getSource();
347 for (int i = 0; i < tpResolvers.getTabCount(); i++) {
348 TagConflictResolver resolver = (TagConflictResolver) tpResolvers.getComponentAt(i);
349 if (tagModel == resolver.getModel()) {
350 tpResolvers.setIconAt(i,
351 (Integer) evt.getNewValue() == 0 ? iconResolved : iconUnresolved
352 );
353 }
354 }
355 }
356 }
357
358 static final class StatisticsInfo {
359 int numTags;
360 final Map<OsmPrimitiveType, Integer> sourceInfo;
361 final Map<OsmPrimitiveType, Integer> targetInfo;
362
363 StatisticsInfo() {
364 sourceInfo = new EnumMap<>(OsmPrimitiveType.class);
365 targetInfo = new EnumMap<>(OsmPrimitiveType.class);
366 }
367 }
368
369 static final class StatisticsTableModel extends DefaultTableModel {
370 private static final String[] HEADERS = {tr("Paste ..."), tr("From ..."), tr("To ...") };
371 private final transient List<StatisticsInfo> data = new ArrayList<>();
372
373 @Override
374 public Object getValueAt(int row, int column) {
375 if (row == 0)
376 return HEADERS[column];
377 else if (row -1 < data.size())
378 return data.get(row -1);
379 else
380 return null;
381 }
382
383 @Override
384 public boolean isCellEditable(int row, int column) {
385 return false;
386 }
387
388 @Override
389 public int getRowCount() {
390 return data == null ? 1 : data.size() + 1;
391 }
392
393 void reset() {
394 data.clear();
395 }
396
397 void append(StatisticsInfo info) {
398 data.add(info);
399 fireTableDataChanged();
400 }
401 }
402
403 static final class StatisticsInfoRenderer extends JLabel implements TableCellRenderer {
404 private void reset() {
405 setIcon(null);
406 setText("");
407 setFont(UIManager.getFont("Table.font"));
408 }
409
410 private void renderNumTags(StatisticsInfo info) {
411 if (info == null) return;
412 setText(trn("{0} tag", "{0} tags", info.numTags, info.numTags));
413 }
414
415 private void renderStatistics(Map<OsmPrimitiveType, Integer> stat) {
416 if (stat == null) return;
417 if (stat.isEmpty()) return;
418 if (stat.size() == 1) {
419 setIcon(ImageProvider.get(stat.keySet().iterator().next()));
420 } else {
421 setIcon(ImageProvider.get("data", "object"));
422 }
423 StringBuilder text = new StringBuilder();
424 for (Entry<OsmPrimitiveType, Integer> entry: stat.entrySet()) {
425 OsmPrimitiveType type = entry.getKey();
426 int numPrimitives = entry.getValue() == null ? 0 : entry.getValue();
427 if (numPrimitives == 0) {
428 continue;
429 }
430 String msg;
431 switch(type) {
432 case NODE: msg = trn("{0} node", "{0} nodes", numPrimitives, numPrimitives); break;
433 case WAY: msg = trn("{0} way", "{0} ways", numPrimitives, numPrimitives); break;
434 case RELATION: msg = trn("{0} relation", "{0} relations", numPrimitives, numPrimitives); break;
435 default: throw new AssertionError();
436 }
437 if (text.length() > 0) {
438 text.append(", ");
439 }
440 text.append(msg);
441 }
442 setText(text.toString());
443 }
444
445 private void renderFrom(StatisticsInfo info) {
446 renderStatistics(info.sourceInfo);
447 }
448
449 private void renderTo(StatisticsInfo info) {
450 renderStatistics(info.targetInfo);
451 }
452
453 @Override
454 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
455 boolean hasFocus, int row, int column) {
456 reset();
457 if (value == null)
458 return this;
459
460 if (row == 0) {
461 setFont(getFont().deriveFont(Font.BOLD));
462 setText((String) value);
463 } else {
464 StatisticsInfo info = (StatisticsInfo) value;
465
466 switch(column) {
467 case 0: renderNumTags(info); break;
468 case 1: renderFrom(info); break;
469 case 2: renderTo(info); break;
470 default: // Do nothing
471 }
472 }
473 return this;
474 }
475 }
476
477 static final class StatisticsInfoTable extends JPanel {
478
479 StatisticsInfoTable(StatisticsTableModel model) {
480 JTable infoTable = new JTable(model,
481 new TagTableColumnModelBuilder(new StatisticsInfoRenderer(), tr("Paste ..."), tr("From ..."), tr("To ...")).build());
482 infoTable.setShowHorizontalLines(true);
483 infoTable.setShowVerticalLines(false);
484 infoTable.setEnabled(false);
485 setLayout(new BorderLayout());
486 add(infoTable, BorderLayout.CENTER);
487 }
488
489 @Override
490 public Insets getInsets() {
491 Insets insets = super.getInsets();
492 insets.bottom = 20;
493 return insets;
494 }
495 }
496}
Note: See TracBrowser for help on using the repository browser.