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

Last change on this file since 6070 was 6070, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

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