source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/ConflictDialog.java@ 12279

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

sonar - squid:S3878 - Arrays should not be created for varargs parameters

  • Property svn:eol-style set to native
File size: 20.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7import static org.openstreetmap.josm.tools.I18n.trn;
8
9import java.awt.Color;
10import java.awt.Graphics;
11import java.awt.Point;
12import java.awt.event.ActionEvent;
13import java.awt.event.KeyEvent;
14import java.awt.event.MouseEvent;
15import java.util.ArrayList;
16import java.util.Arrays;
17import java.util.Collection;
18import java.util.HashSet;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.Set;
22import java.util.concurrent.CopyOnWriteArrayList;
23
24import javax.swing.AbstractAction;
25import javax.swing.JList;
26import javax.swing.JMenuItem;
27import javax.swing.JOptionPane;
28import javax.swing.JPopupMenu;
29import javax.swing.ListModel;
30import javax.swing.ListSelectionModel;
31import javax.swing.event.ListDataEvent;
32import javax.swing.event.ListDataListener;
33import javax.swing.event.ListSelectionEvent;
34import javax.swing.event.ListSelectionListener;
35import javax.swing.event.PopupMenuEvent;
36import javax.swing.event.PopupMenuListener;
37
38import org.openstreetmap.josm.Main;
39import org.openstreetmap.josm.actions.AbstractSelectAction;
40import org.openstreetmap.josm.actions.ExpertToggleAction;
41import org.openstreetmap.josm.command.Command;
42import org.openstreetmap.josm.command.SequenceCommand;
43import org.openstreetmap.josm.data.SelectionChangedListener;
44import org.openstreetmap.josm.data.conflict.Conflict;
45import org.openstreetmap.josm.data.conflict.ConflictCollection;
46import org.openstreetmap.josm.data.conflict.IConflictListener;
47import org.openstreetmap.josm.data.osm.DataSet;
48import org.openstreetmap.josm.data.osm.Node;
49import org.openstreetmap.josm.data.osm.OsmPrimitive;
50import org.openstreetmap.josm.data.osm.Relation;
51import org.openstreetmap.josm.data.osm.RelationMember;
52import org.openstreetmap.josm.data.osm.Way;
53import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
54import org.openstreetmap.josm.data.osm.visitor.Visitor;
55import org.openstreetmap.josm.data.preferences.ColorProperty;
56import org.openstreetmap.josm.gui.HelpAwareOptionPane;
57import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
58import org.openstreetmap.josm.gui.NavigatableComponent;
59import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
60import org.openstreetmap.josm.gui.PopupMenuHandler;
61import org.openstreetmap.josm.gui.SideButton;
62import org.openstreetmap.josm.gui.conflict.pair.ConflictResolver;
63import org.openstreetmap.josm.gui.conflict.pair.MergeDecisionType;
64import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
65import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
66import org.openstreetmap.josm.gui.layer.OsmDataLayer;
67import org.openstreetmap.josm.gui.util.GuiHelper;
68import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
69import org.openstreetmap.josm.tools.ImageProvider;
70import org.openstreetmap.josm.tools.Shortcut;
71
72/**
73 * This dialog displays the {@link ConflictCollection} of the active {@link OsmDataLayer} in a toggle
74 * dialog on the right of the main frame.
75 * @since 86
76 */
77public final class ConflictDialog extends ToggleDialog implements ActiveLayerChangeListener, IConflictListener, SelectionChangedListener {
78
79 private static final ColorProperty CONFLICT_COLOR = new ColorProperty(marktr("conflict"), Color.GRAY);
80 private static final ColorProperty BACKGROUND_COLOR = new ColorProperty(marktr("background"), Color.BLACK);
81
82 /** the collection of conflicts displayed by this conflict dialog */
83 private transient ConflictCollection conflicts;
84
85 /** the model for the list of conflicts */
86 private transient ConflictListModel model;
87 /** the list widget for the list of conflicts */
88 private JList<OsmPrimitive> lstConflicts;
89
90 private final JPopupMenu popupMenu = new JPopupMenu();
91 private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
92
93 private final ResolveAction actResolve = new ResolveAction();
94 private final SelectAction actSelect = new SelectAction();
95
96 /**
97 * Constructs a new {@code ConflictDialog}.
98 */
99 public ConflictDialog() {
100 super(tr("Conflict"), "conflict", tr("Resolve conflicts."),
101 Shortcut.registerShortcut("subwindow:conflict", tr("Toggle: {0}", tr("Conflict")),
102 KeyEvent.VK_C, Shortcut.ALT_SHIFT), 100);
103
104 build();
105 refreshView();
106 }
107
108 /**
109 * Replies the color used to paint conflicts.
110 *
111 * @return the color used to paint conflicts
112 * @see #paintConflicts
113 * @since 1221
114 */
115 public static Color getColor() {
116 return CONFLICT_COLOR.get();
117 }
118
119 /**
120 * builds the GUI
121 */
122 private void build() {
123 model = new ConflictListModel();
124
125 lstConflicts = new JList<>(model);
126 lstConflicts.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
127 lstConflicts.setCellRenderer(new OsmPrimitivRenderer());
128 lstConflicts.addMouseListener(new MouseEventHandler());
129 addListSelectionListener(e -> Main.map.mapView.repaint());
130
131 SideButton btnResolve = new SideButton(actResolve);
132 addListSelectionListener(actResolve);
133
134 SideButton btnSelect = new SideButton(actSelect);
135 addListSelectionListener(actSelect);
136
137 createLayout(lstConflicts, true, Arrays.asList(btnResolve, btnSelect));
138
139 popupMenuHandler.addAction(Main.main.menu.autoScaleActions.get("conflict"));
140
141 ResolveToMyVersionAction resolveToMyVersionAction = new ResolveToMyVersionAction();
142 ResolveToTheirVersionAction resolveToTheirVersionAction = new ResolveToTheirVersionAction();
143 addListSelectionListener(resolveToMyVersionAction);
144 addListSelectionListener(resolveToTheirVersionAction);
145 JMenuItem btnResolveMy = popupMenuHandler.addAction(resolveToMyVersionAction);
146 JMenuItem btnResolveTheir = popupMenuHandler.addAction(resolveToTheirVersionAction);
147
148 popupMenuHandler.addListener(new ResolveButtonsPopupMenuListener(btnResolveTheir, btnResolveMy));
149 }
150
151 @Override
152 public void showNotify() {
153 DataSet.addSelectionListener(this);
154 Main.getLayerManager().addAndFireActiveLayerChangeListener(this);
155 refreshView();
156 }
157
158 @Override
159 public void hideNotify() {
160 Main.getLayerManager().removeActiveLayerChangeListener(this);
161 DataSet.removeSelectionListener(this);
162 }
163
164 /**
165 * Add a list selection listener to the conflicts list.
166 * @param listener the ListSelectionListener
167 * @since 5958
168 */
169 public void addListSelectionListener(ListSelectionListener listener) {
170 lstConflicts.getSelectionModel().addListSelectionListener(listener);
171 }
172
173 /**
174 * Remove the given list selection listener from the conflicts list.
175 * @param listener the ListSelectionListener
176 * @since 5958
177 */
178 public void removeListSelectionListener(ListSelectionListener listener) {
179 lstConflicts.getSelectionModel().removeListSelectionListener(listener);
180 }
181
182 /**
183 * Replies the popup menu handler.
184 * @return The popup menu handler
185 * @since 5958
186 */
187 public PopupMenuHandler getPopupMenuHandler() {
188 return popupMenuHandler;
189 }
190
191 /**
192 * Launches a conflict resolution dialog for the first selected conflict
193 */
194 private void resolve() {
195 if (conflicts == null || model.getSize() == 0)
196 return;
197
198 int index = lstConflicts.getSelectedIndex();
199 if (index < 0) {
200 index = 0;
201 }
202
203 Conflict<? extends OsmPrimitive> c = conflicts.get(index);
204 ConflictResolutionDialog dialog = new ConflictResolutionDialog(Main.parent);
205 dialog.getConflictResolver().populate(c);
206 dialog.showDialog();
207
208 lstConflicts.setSelectedIndex(index);
209
210 Main.map.mapView.repaint();
211 }
212
213 /**
214 * refreshes the view of this dialog
215 */
216 public void refreshView() {
217 OsmDataLayer editLayer = Main.getLayerManager().getEditLayer();
218 conflicts = editLayer == null ? new ConflictCollection() : editLayer.getConflicts();
219 GuiHelper.runInEDT(() -> {
220 model.fireContentChanged();
221 updateTitle();
222 });
223 }
224
225 private void updateTitle() {
226 int conflictsCount = conflicts.size();
227 if (conflictsCount > 0) {
228 setTitle(trn("Conflict: {0} unresolved", "Conflicts: {0} unresolved", conflictsCount, conflictsCount) +
229 " ("+tr("Rel.:{0} / Ways:{1} / Nodes:{2}",
230 conflicts.getRelationConflicts().size(),
231 conflicts.getWayConflicts().size(),
232 conflicts.getNodeConflicts().size())+')');
233 } else {
234 setTitle(tr("Conflict"));
235 }
236 }
237
238 /**
239 * Paints all conflicts that can be expressed on the main window.
240 *
241 * @param g The {@code Graphics} used to paint
242 * @param nc The {@code NavigatableComponent} used to get screen coordinates of nodes
243 * @since 86
244 */
245 public void paintConflicts(final Graphics g, final NavigatableComponent nc) {
246 Color preferencesColor = getColor();
247 if (preferencesColor.equals(BACKGROUND_COLOR.get()))
248 return;
249 g.setColor(preferencesColor);
250 Visitor conflictPainter = new ConflictPainter(nc, g);
251 for (OsmPrimitive o : lstConflicts.getSelectedValuesList()) {
252 if (conflicts == null || !conflicts.hasConflictForMy(o)) {
253 continue;
254 }
255 conflicts.getConflictForMy(o).getTheir().accept(conflictPainter);
256 }
257 }
258
259 @Override
260 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
261 OsmDataLayer oldLayer = e.getPreviousEditLayer();
262 if (oldLayer != null) {
263 oldLayer.getConflicts().removeConflictListener(this);
264 }
265 OsmDataLayer newLayer = e.getSource().getEditLayer();
266 if (newLayer != null) {
267 newLayer.getConflicts().addConflictListener(this);
268 }
269 refreshView();
270 }
271
272 /**
273 * replies the conflict collection currently held by this dialog; may be null
274 *
275 * @return the conflict collection currently held by this dialog; may be null
276 */
277 public ConflictCollection getConflicts() {
278 return conflicts;
279 }
280
281 /**
282 * returns the first selected item of the conflicts list
283 *
284 * @return Conflict
285 */
286 public Conflict<? extends OsmPrimitive> getSelectedConflict() {
287 if (conflicts == null || model.getSize() == 0)
288 return null;
289
290 int index = lstConflicts.getSelectedIndex();
291
292 return index >= 0 ? conflicts.get(index) : null;
293 }
294
295 private boolean isConflictSelected() {
296 final ListSelectionModel selModel = lstConflicts.getSelectionModel();
297 return selModel.getMinSelectionIndex() >= 0 && selModel.getMaxSelectionIndex() >= selModel.getMinSelectionIndex();
298 }
299
300 @Override
301 public void onConflictsAdded(ConflictCollection conflicts) {
302 refreshView();
303 }
304
305 @Override
306 public void onConflictsRemoved(ConflictCollection conflicts) {
307 Main.info("1 conflict has been resolved.");
308 refreshView();
309 }
310
311 @Override
312 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
313 lstConflicts.setValueIsAdjusting(true);
314 lstConflicts.clearSelection();
315 for (OsmPrimitive osm : newSelection) {
316 if (conflicts != null && conflicts.hasConflictForMy(osm)) {
317 int pos = model.indexOf(osm);
318 if (pos >= 0) {
319 lstConflicts.addSelectionInterval(pos, pos);
320 }
321 }
322 }
323 lstConflicts.setValueIsAdjusting(false);
324 }
325
326 @Override
327 public String helpTopic() {
328 return ht("/Dialog/ConflictList");
329 }
330
331 static final class ResolveButtonsPopupMenuListener implements PopupMenuListener {
332 private final JMenuItem btnResolveTheir;
333 private final JMenuItem btnResolveMy;
334
335 ResolveButtonsPopupMenuListener(JMenuItem btnResolveTheir, JMenuItem btnResolveMy) {
336 this.btnResolveTheir = btnResolveTheir;
337 this.btnResolveMy = btnResolveMy;
338 }
339
340 @Override
341 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
342 btnResolveMy.setVisible(ExpertToggleAction.isExpert());
343 btnResolveTheir.setVisible(ExpertToggleAction.isExpert());
344 }
345
346 @Override
347 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
348 // Do nothing
349 }
350
351 @Override
352 public void popupMenuCanceled(PopupMenuEvent e) {
353 // Do nothing
354 }
355 }
356
357 class MouseEventHandler extends PopupMenuLauncher {
358 /**
359 * Constructs a new {@code MouseEventHandler}.
360 */
361 MouseEventHandler() {
362 super(popupMenu);
363 }
364
365 @Override public void mouseClicked(MouseEvent e) {
366 if (isDoubleClick(e)) {
367 resolve();
368 }
369 }
370 }
371
372 /**
373 * The {@link ListModel} for conflicts
374 *
375 */
376 class ConflictListModel implements ListModel<OsmPrimitive> {
377
378 private final CopyOnWriteArrayList<ListDataListener> listeners;
379
380 /**
381 * Constructs a new {@code ConflictListModel}.
382 */
383 ConflictListModel() {
384 listeners = new CopyOnWriteArrayList<>();
385 }
386
387 @Override
388 public void addListDataListener(ListDataListener l) {
389 if (l != null) {
390 listeners.addIfAbsent(l);
391 }
392 }
393
394 @Override
395 public void removeListDataListener(ListDataListener l) {
396 listeners.remove(l);
397 }
398
399 protected void fireContentChanged() {
400 ListDataEvent evt = new ListDataEvent(
401 this,
402 ListDataEvent.CONTENTS_CHANGED,
403 0,
404 getSize()
405 );
406 for (ListDataListener listener : listeners) {
407 listener.contentsChanged(evt);
408 }
409 }
410
411 @Override
412 public OsmPrimitive getElementAt(int index) {
413 if (index < 0 || index >= getSize())
414 return null;
415 return conflicts.get(index).getMy();
416 }
417
418 @Override
419 public int getSize() {
420 return conflicts != null ? conflicts.size() : 0;
421 }
422
423 public int indexOf(OsmPrimitive my) {
424 if (conflicts != null) {
425 for (int i = 0; i < conflicts.size(); i++) {
426 if (conflicts.get(i).isMatchingMy(my))
427 return i;
428 }
429 }
430 return -1;
431 }
432
433 public OsmPrimitive get(int idx) {
434 return conflicts != null ? conflicts.get(idx).getMy() : null;
435 }
436 }
437
438 class ResolveAction extends AbstractAction implements ListSelectionListener {
439 ResolveAction() {
440 putValue(NAME, tr("Resolve"));
441 putValue(SHORT_DESCRIPTION, tr("Open a merge dialog of all selected items in the list above."));
442 new ImageProvider("dialogs", "conflict").getResource().attachImageIcon(this, true);
443 putValue("help", ht("/Dialog/ConflictList#ResolveAction"));
444 }
445
446 @Override
447 public void actionPerformed(ActionEvent e) {
448 resolve();
449 }
450
451 @Override
452 public void valueChanged(ListSelectionEvent e) {
453 setEnabled(isConflictSelected());
454 }
455 }
456
457 final class SelectAction extends AbstractSelectAction implements ListSelectionListener {
458 private SelectAction() {
459 putValue("help", ht("/Dialog/ConflictList#SelectAction"));
460 }
461
462 @Override
463 public void actionPerformed(ActionEvent e) {
464 Collection<OsmPrimitive> sel = new LinkedList<>();
465 for (OsmPrimitive o : lstConflicts.getSelectedValuesList()) {
466 sel.add(o);
467 }
468 DataSet ds = Main.getLayerManager().getEditDataSet();
469 if (ds != null) { // Can't see how it is possible but it happened in #7942
470 ds.setSelected(sel);
471 }
472 }
473
474 @Override
475 public void valueChanged(ListSelectionEvent e) {
476 setEnabled(isConflictSelected());
477 }
478 }
479
480 abstract class ResolveToAction extends ResolveAction {
481 private final String name;
482 private final MergeDecisionType type;
483
484 ResolveToAction(String name, String description, MergeDecisionType type) {
485 this.name = name;
486 this.type = type;
487 putValue(NAME, name);
488 putValue(SHORT_DESCRIPTION, description);
489 }
490
491 @Override
492 public void actionPerformed(ActionEvent e) {
493 final ConflictResolver resolver = new ConflictResolver();
494 final List<Command> commands = new ArrayList<>();
495 for (OsmPrimitive osmPrimitive : lstConflicts.getSelectedValuesList()) {
496 Conflict<? extends OsmPrimitive> c = conflicts.getConflictForMy(osmPrimitive);
497 if (c != null) {
498 resolver.populate(c);
499 resolver.decideRemaining(type);
500 commands.add(resolver.buildResolveCommand());
501 }
502 }
503 Main.main.undoRedo.add(new SequenceCommand(name, commands));
504 refreshView();
505 }
506 }
507
508 class ResolveToMyVersionAction extends ResolveToAction {
509 ResolveToMyVersionAction() {
510 super(tr("Resolve to my versions"), tr("Resolves all unresolved conflicts to ''my'' version"),
511 MergeDecisionType.KEEP_MINE);
512 }
513 }
514
515 class ResolveToTheirVersionAction extends ResolveToAction {
516 ResolveToTheirVersionAction() {
517 super(tr("Resolve to their versions"), tr("Resolves all unresolved conflicts to ''their'' version"),
518 MergeDecisionType.KEEP_THEIR);
519 }
520 }
521
522 /**
523 * Paints conflicts.
524 */
525 public static class ConflictPainter extends AbstractVisitor {
526 // Manage a stack of visited relations to avoid infinite recursion with cyclic relations (fix #7938)
527 private final Set<Relation> visited = new HashSet<>();
528 private final NavigatableComponent nc;
529 private final Graphics g;
530
531 ConflictPainter(NavigatableComponent nc, Graphics g) {
532 this.nc = nc;
533 this.g = g;
534 }
535
536 @Override
537 public void visit(Node n) {
538 Point p = nc.getPoint(n);
539 g.drawRect(p.x-1, p.y-1, 2, 2);
540 }
541
542 private void visit(Node n1, Node n2) {
543 Point p1 = nc.getPoint(n1);
544 Point p2 = nc.getPoint(n2);
545 g.drawLine(p1.x, p1.y, p2.x, p2.y);
546 }
547
548 @Override
549 public void visit(Way w) {
550 Node lastN = null;
551 for (Node n : w.getNodes()) {
552 if (lastN == null) {
553 lastN = n;
554 continue;
555 }
556 visit(lastN, n);
557 lastN = n;
558 }
559 }
560
561 @Override
562 public void visit(Relation e) {
563 if (!visited.contains(e)) {
564 visited.add(e);
565 try {
566 for (RelationMember em : e.getMembers()) {
567 em.getMember().accept(this);
568 }
569 } finally {
570 visited.remove(e);
571 }
572 }
573 }
574 }
575
576 /**
577 * Warns the user about the number of detected conflicts
578 *
579 * @param numNewConflicts the number of detected conflicts
580 * @since 5775
581 */
582 public void warnNumNewConflicts(int numNewConflicts) {
583 if (numNewConflicts == 0)
584 return;
585
586 String msg1 = trn(
587 "There was {0} conflict detected.",
588 "There were {0} conflicts detected.",
589 numNewConflicts,
590 numNewConflicts
591 );
592
593 final StringBuilder sb = new StringBuilder();
594 sb.append("<html>").append(msg1).append("</html>");
595 if (numNewConflicts > 0) {
596 final ButtonSpec[] options = new ButtonSpec[] {
597 new ButtonSpec(
598 tr("OK"),
599 ImageProvider.get("ok"),
600 tr("Click to close this dialog and continue editing"),
601 null /* no specific help */
602 )
603 };
604 GuiHelper.runInEDT(() -> {
605 HelpAwareOptionPane.showOptionDialog(
606 Main.parent,
607 sb.toString(),
608 tr("Conflicts detected"),
609 JOptionPane.WARNING_MESSAGE,
610 null, /* no icon */
611 options,
612 options[0],
613 ht("/Concepts/Conflict#WarningAboutDetectedConflicts")
614 );
615 unfurlDialog();
616 Main.map.repaint();
617 });
618 }
619 }
620}
Note: See TracBrowser for help on using the repository browser.