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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 17.0 KB
Line 
1// License: GPL. See LICENSE file for details.
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.Arrays;
16import java.util.Collection;
17import java.util.HashSet;
18import java.util.Iterator;
19import java.util.LinkedList;
20import java.util.Set;
21import java.util.concurrent.CopyOnWriteArrayList;
22
23import javax.swing.AbstractAction;
24import javax.swing.JList;
25import javax.swing.JOptionPane;
26import javax.swing.JPopupMenu;
27import javax.swing.ListModel;
28import javax.swing.ListSelectionModel;
29import javax.swing.event.ListDataEvent;
30import javax.swing.event.ListDataListener;
31import javax.swing.event.ListSelectionEvent;
32import javax.swing.event.ListSelectionListener;
33
34import org.openstreetmap.josm.Main;
35import org.openstreetmap.josm.data.SelectionChangedListener;
36import org.openstreetmap.josm.data.conflict.Conflict;
37import org.openstreetmap.josm.data.conflict.ConflictCollection;
38import org.openstreetmap.josm.data.conflict.IConflictListener;
39import org.openstreetmap.josm.data.osm.DataSet;
40import org.openstreetmap.josm.data.osm.Node;
41import org.openstreetmap.josm.data.osm.OsmPrimitive;
42import org.openstreetmap.josm.data.osm.Relation;
43import org.openstreetmap.josm.data.osm.RelationMember;
44import org.openstreetmap.josm.data.osm.Way;
45import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
46import org.openstreetmap.josm.data.osm.visitor.Visitor;
47import org.openstreetmap.josm.gui.HelpAwareOptionPane;
48import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
49import org.openstreetmap.josm.gui.MapView;
50import org.openstreetmap.josm.gui.NavigatableComponent;
51import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
52import org.openstreetmap.josm.gui.PopupMenuHandler;
53import org.openstreetmap.josm.gui.SideButton;
54import org.openstreetmap.josm.gui.layer.OsmDataLayer;
55import org.openstreetmap.josm.gui.util.GuiHelper;
56import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
57import org.openstreetmap.josm.tools.ImageProvider;
58import org.openstreetmap.josm.tools.Shortcut;
59
60/**
61 * This dialog displays the {@link ConflictCollection} of the active {@link OsmDataLayer} in a toggle
62 * dialog on the right of the main frame.
63 *
64 */
65public final class ConflictDialog extends ToggleDialog implements MapView.EditLayerChangeListener, IConflictListener, SelectionChangedListener{
66
67 /**
68 * Replies the color used to paint conflicts.
69 *
70 * @return the color used to paint conflicts
71 * @since 1221
72 * @see #paintConflicts
73 */
74 static public Color getColor() {
75 return Main.pref.getColor(marktr("conflict"), Color.gray);
76 }
77
78 /** the collection of conflicts displayed by this conflict dialog */
79 private ConflictCollection conflicts;
80
81 /** the model for the list of conflicts */
82 private ConflictListModel model;
83 /** the list widget for the list of conflicts */
84 private JList lstConflicts;
85
86 private final JPopupMenu popupMenu = new JPopupMenu();
87 private final PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
88
89 private ResolveAction actResolve;
90 private SelectAction actSelect;
91
92 /**
93 * builds the GUI
94 */
95 protected void build() {
96 model = new ConflictListModel();
97
98 lstConflicts = new JList(model);
99 lstConflicts.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
100 lstConflicts.setCellRenderer(new OsmPrimitivRenderer());
101 lstConflicts.addMouseListener(new MouseEventHandler());
102 addListSelectionListener(new ListSelectionListener(){
103 @Override
104 public void valueChanged(ListSelectionEvent e) {
105 Main.map.mapView.repaint();
106 }
107 });
108
109 SideButton btnResolve = new SideButton(actResolve = new ResolveAction());
110 addListSelectionListener(actResolve);
111
112 SideButton btnSelect = new SideButton(actSelect = new SelectAction());
113 addListSelectionListener(actSelect);
114
115 createLayout(lstConflicts, true, Arrays.asList(new SideButton[] {
116 btnResolve, btnSelect
117 }));
118
119 popupMenuHandler.addAction(Main.main.menu.autoScaleActions.get("conflict"));
120 }
121
122 /**
123 * constructor
124 */
125 public ConflictDialog() {
126 super(tr("Conflict"), "conflict", tr("Resolve conflicts."),
127 Shortcut.registerShortcut("subwindow:conflict", tr("Toggle: {0}", tr("Conflict")),
128 KeyEvent.VK_C, Shortcut.ALT_SHIFT), 100);
129
130 build();
131 refreshView();
132 }
133
134 @Override
135 public void showNotify() {
136 DataSet.addSelectionListener(this);
137 MapView.addEditLayerChangeListener(this, true);
138 refreshView();
139 }
140
141 @Override
142 public void hideNotify() {
143 MapView.removeEditLayerChangeListener(this);
144 DataSet.removeSelectionListener(this);
145 }
146
147 /**
148 * Add a list selection listener to the conflicts list.
149 * @param listener the ListSelectionListener
150 * @since 5958
151 */
152 public void addListSelectionListener(ListSelectionListener listener) {
153 lstConflicts.getSelectionModel().addListSelectionListener(listener);
154 }
155
156 /**
157 * Remove the given list selection listener from the conflicts list.
158 * @param listener the ListSelectionListener
159 * @since 5958
160 */
161 public void removeListSelectionListener(ListSelectionListener listener) {
162 lstConflicts.getSelectionModel().removeListSelectionListener(listener);
163 }
164
165 /**
166 * Replies the popup menu handler.
167 * @return The popup menu handler
168 * @since 5958
169 */
170 public PopupMenuHandler getPopupMenuHandler() {
171 return popupMenuHandler;
172 }
173
174 /**
175 * Launches a conflict resolution dialog for the first selected conflict
176 *
177 */
178 private final void resolve() {
179 if (conflicts == null || model.getSize() == 0) return;
180
181 int index = lstConflicts.getSelectedIndex();
182 if (index < 0) {
183 index = 0;
184 }
185
186 Conflict<? extends OsmPrimitive> c = conflicts.get(index);
187 ConflictResolutionDialog dialog = new ConflictResolutionDialog(Main.parent);
188 dialog.getConflictResolver().populate(c);
189 dialog.setVisible(true);
190
191 lstConflicts.setSelectedIndex(index);
192
193 Main.map.mapView.repaint();
194 }
195
196 /**
197 * refreshes the view of this dialog
198 */
199 public final void refreshView() {
200 OsmDataLayer editLayer = Main.main.getEditLayer();
201 conflicts = (editLayer == null ? new ConflictCollection() : editLayer.getConflicts());
202 GuiHelper.runInEDT(new Runnable() {
203 @Override
204 public void run() {
205 model.fireContentChanged();
206 updateTitle(conflicts.size());
207 }
208 });
209 }
210
211 private void updateTitle(int conflictsCount) {
212 if (conflictsCount > 0) {
213 setTitle(tr("Conflicts: {0} unresolved", conflicts.size()));
214 } else {
215 setTitle(tr("Conflict"));
216 }
217 }
218
219 /**
220 * Paints all conflicts that can be expressed on the main window.
221 *
222 * @param g The {@code Graphics} used to paint
223 * @param nc The {@code NavigatableComponent} used to get screen coordinates of nodes
224 * @since 86
225 */
226 public void paintConflicts(final Graphics g, final NavigatableComponent nc) {
227 Color preferencesColor = getColor();
228 if (preferencesColor.equals(Main.pref.getColor(marktr("background"), Color.black)))
229 return;
230 g.setColor(preferencesColor);
231 Visitor conflictPainter = new AbstractVisitor() {
232 // Manage a stack of visited relations to avoid infinite recursion with cyclic relations (fix #7938)
233 private final Set<Relation> visited = new HashSet<Relation>();
234 @Override
235 public void visit(Node n) {
236 Point p = nc.getPoint(n);
237 g.drawRect(p.x-1, p.y-1, 2, 2);
238 }
239 public void visit(Node n1, Node n2) {
240 Point p1 = nc.getPoint(n1);
241 Point p2 = nc.getPoint(n2);
242 g.drawLine(p1.x, p1.y, p2.x, p2.y);
243 }
244 @Override
245 public void visit(Way w) {
246 Node lastN = null;
247 for (Node n : w.getNodes()) {
248 if (lastN == null) {
249 lastN = n;
250 continue;
251 }
252 visit(lastN, n);
253 lastN = n;
254 }
255 }
256 @Override
257 public void visit(Relation e) {
258 if (!visited.contains(e)) {
259 visited.add(e);
260 try {
261 for (RelationMember em : e.getMembers()) {
262 em.getMember().accept(this);
263 }
264 } finally {
265 visited.remove(e);
266 }
267 }
268 }
269 };
270 for (Object o : lstConflicts.getSelectedValues()) {
271 if (conflicts == null || !conflicts.hasConflictForMy((OsmPrimitive)o)) {
272 continue;
273 }
274 conflicts.getConflictForMy((OsmPrimitive)o).getTheir().accept(conflictPainter);
275 }
276 }
277
278 @Override
279 public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
280 if (oldLayer != null) {
281 oldLayer.getConflicts().removeConflictListener(this);
282 }
283 if (newLayer != null) {
284 newLayer.getConflicts().addConflictListener(this);
285 }
286 refreshView();
287 }
288
289
290 /**
291 * replies the conflict collection currently held by this dialog; may be null
292 *
293 * @return the conflict collection currently held by this dialog; may be null
294 */
295 public ConflictCollection getConflicts() {
296 return conflicts;
297 }
298
299 /**
300 * returns the first selected item of the conflicts list
301 *
302 * @return Conflict
303 */
304 public Conflict<? extends OsmPrimitive> getSelectedConflict() {
305 if (conflicts == null || model.getSize() == 0) return null;
306
307 int index = lstConflicts.getSelectedIndex();
308 if (index < 0) return null;
309
310 return conflicts.get(index);
311 }
312
313 @Override
314 public void onConflictsAdded(ConflictCollection conflicts) {
315 refreshView();
316 }
317
318 @Override
319 public void onConflictsRemoved(ConflictCollection conflicts) {
320 System.err.println("1 conflict has been resolved.");
321 refreshView();
322 }
323
324 @Override
325 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
326 lstConflicts.clearSelection();
327 for (OsmPrimitive osm : newSelection) {
328 if (conflicts != null && conflicts.hasConflictForMy(osm)) {
329 int pos = model.indexOf(osm);
330 if (pos >= 0) {
331 lstConflicts.addSelectionInterval(pos, pos);
332 }
333 }
334 }
335 }
336
337 @Override
338 public String helpTopic() {
339 return ht("/Dialog/ConflictList");
340 }
341
342 class MouseEventHandler extends PopupMenuLauncher {
343 public MouseEventHandler() {
344 super(popupMenu);
345 }
346 @Override public void mouseClicked(MouseEvent e) {
347 if (isDoubleClick(e)) {
348 resolve();
349 }
350 }
351 }
352
353 /**
354 * The {@link ListModel} for conflicts
355 *
356 */
357 class ConflictListModel implements ListModel {
358
359 private CopyOnWriteArrayList<ListDataListener> listeners;
360
361 public ConflictListModel() {
362 listeners = new CopyOnWriteArrayList<ListDataListener>();
363 }
364
365 @Override
366 public void addListDataListener(ListDataListener l) {
367 if (l != null) {
368 listeners.addIfAbsent(l);
369 }
370 }
371
372 @Override
373 public void removeListDataListener(ListDataListener l) {
374 listeners.remove(l);
375 }
376
377 protected void fireContentChanged() {
378 ListDataEvent evt = new ListDataEvent(
379 this,
380 ListDataEvent.CONTENTS_CHANGED,
381 0,
382 getSize()
383 );
384 Iterator<ListDataListener> it = listeners.iterator();
385 while(it.hasNext()) {
386 it.next().contentsChanged(evt);
387 }
388 }
389
390 @Override
391 public Object getElementAt(int index) {
392 if (index < 0) return null;
393 if (index >= getSize()) return null;
394 return conflicts.get(index).getMy();
395 }
396
397 @Override
398 public int getSize() {
399 if (conflicts == null) return 0;
400 return conflicts.size();
401 }
402
403 public int indexOf(OsmPrimitive my) {
404 if (conflicts == null) return -1;
405 for (int i=0; i < conflicts.size();i++) {
406 if (conflicts.get(i).isMatchingMy(my))
407 return i;
408 }
409 return -1;
410 }
411
412 public OsmPrimitive get(int idx) {
413 if (conflicts == null) return null;
414 return conflicts.get(idx).getMy();
415 }
416 }
417
418 class ResolveAction extends AbstractAction implements ListSelectionListener {
419 public ResolveAction() {
420 putValue(NAME, tr("Resolve"));
421 putValue(SHORT_DESCRIPTION, tr("Open a merge dialog of all selected items in the list above."));
422 putValue(SMALL_ICON, ImageProvider.get("dialogs", "conflict"));
423 putValue("help", ht("/Dialog/ConflictList#ResolveAction"));
424 }
425
426 @Override
427 public void actionPerformed(ActionEvent e) {
428 resolve();
429 }
430
431 @Override
432 public void valueChanged(ListSelectionEvent e) {
433 ListSelectionModel model = (ListSelectionModel)e.getSource();
434 boolean enabled = model.getMinSelectionIndex() >= 0
435 && model.getMaxSelectionIndex() >= model.getMinSelectionIndex();
436 setEnabled(enabled);
437 }
438 }
439
440 class SelectAction extends AbstractAction implements ListSelectionListener {
441 public SelectAction() {
442 putValue(NAME, tr("Select"));
443 putValue(SHORT_DESCRIPTION, tr("Set the selected elements on the map to the selected items in the list above."));
444 putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
445 putValue("help", ht("/Dialog/ConflictList#SelectAction"));
446 }
447
448 @Override
449 public void actionPerformed(ActionEvent e) {
450 Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
451 for (Object o : lstConflicts.getSelectedValues()) {
452 sel.add((OsmPrimitive)o);
453 }
454 DataSet ds = Main.main.getCurrentDataSet();
455 if (ds != null) { // Can't see how it is possible but it happened in #7942
456 ds.setSelected(sel);
457 }
458 }
459
460 @Override
461 public void valueChanged(ListSelectionEvent e) {
462 ListSelectionModel model = (ListSelectionModel)e.getSource();
463 boolean enabled = model.getMinSelectionIndex() >= 0
464 && model.getMaxSelectionIndex() >= model.getMinSelectionIndex();
465 setEnabled(enabled);
466 }
467 }
468
469 /**
470 * Warns the user about the number of detected conflicts
471 *
472 * @param numNewConflicts the number of detected conflicts
473 * @since 5775
474 */
475 public void warnNumNewConflicts(int numNewConflicts) {
476 if (numNewConflicts == 0) return;
477
478 String msg1 = trn(
479 "There was {0} conflict detected.",
480 "There were {0} conflicts detected.",
481 numNewConflicts,
482 numNewConflicts
483 );
484
485 final StringBuffer sb = new StringBuffer();
486 sb.append("<html>").append(msg1).append("</html>");
487 if (numNewConflicts > 0) {
488 final ButtonSpec[] options = new ButtonSpec[] {
489 new ButtonSpec(
490 tr("OK"),
491 ImageProvider.get("ok"),
492 tr("Click to close this dialog and continue editing"),
493 null /* no specific help */
494 )
495 };
496 GuiHelper.runInEDT(new Runnable() {
497 @Override
498 public void run() {
499 HelpAwareOptionPane.showOptionDialog(
500 Main.parent,
501 sb.toString(),
502 tr("Conflicts detected"),
503 JOptionPane.WARNING_MESSAGE,
504 null, /* no icon */
505 options,
506 options[0],
507 ht("/Concepts/Conflict#WarningAboutDetectedConflicts")
508 );
509 unfurlDialog();
510 Main.map.repaint();
511 }
512 });
513 }
514 }
515}
Note: See TracBrowser for help on using the repository browser.