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

Last change on this file since 2880 was 2880, checked in by jttt, 14 years ago

Fixed #4411 Can't upload due to conflicts, but conflict list is empty

  • Property svn:eol-style set to native
File size: 12.2 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.BorderLayout;
8import java.awt.Color;
9import java.awt.Graphics;
10import java.awt.Point;
11import java.awt.event.ActionEvent;
12import java.awt.event.KeyEvent;
13import java.awt.event.MouseAdapter;
14import java.awt.event.MouseEvent;
15import java.util.Collection;
16import java.util.Iterator;
17import java.util.LinkedList;
18import java.util.concurrent.CopyOnWriteArrayList;
19
20import javax.swing.AbstractAction;
21import javax.swing.JList;
22import javax.swing.JPanel;
23import javax.swing.JScrollPane;
24import javax.swing.ListModel;
25import javax.swing.ListSelectionModel;
26import javax.swing.event.ListDataEvent;
27import javax.swing.event.ListDataListener;
28import javax.swing.event.ListSelectionEvent;
29import javax.swing.event.ListSelectionListener;
30
31import org.openstreetmap.josm.Main;
32import org.openstreetmap.josm.data.SelectionChangedListener;
33import org.openstreetmap.josm.data.conflict.Conflict;
34import org.openstreetmap.josm.data.conflict.ConflictCollection;
35import org.openstreetmap.josm.data.conflict.IConflictListener;
36import org.openstreetmap.josm.data.osm.DataSet;
37import org.openstreetmap.josm.data.osm.Node;
38import org.openstreetmap.josm.data.osm.OsmPrimitive;
39import org.openstreetmap.josm.data.osm.Relation;
40import org.openstreetmap.josm.data.osm.RelationMember;
41import org.openstreetmap.josm.data.osm.Way;
42import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
43import org.openstreetmap.josm.data.osm.visitor.Visitor;
44import org.openstreetmap.josm.gui.MapView;
45import org.openstreetmap.josm.gui.NavigatableComponent;
46import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
47import org.openstreetmap.josm.gui.SideButton;
48import org.openstreetmap.josm.gui.layer.OsmDataLayer;
49import org.openstreetmap.josm.tools.ImageProvider;
50import org.openstreetmap.josm.tools.Shortcut;
51
52/**
53 * This dialog displays the {@see ConflictCollection} of the active {@see OsmDataLayer} in a toggle
54 * dialog on the right of the main frame.
55 *
56 */
57public final class ConflictDialog extends ToggleDialog implements MapView.EditLayerChangeListener, IConflictListener, SelectionChangedListener{
58
59 static public Color getColor() {
60 return Main.pref.getColor(marktr("conflict"), Color.gray);
61 }
62
63 /** the collection of conflicts displayed by this conflict dialog*/
64 private ConflictCollection conflicts;
65
66 /** the model for the list of conflicts */
67 private ConflictListModel model;
68 /** the list widget for the list of conflicts */
69 private JList lstConflicts;
70
71 private ResolveAction actResolve;
72 private SelectAction actSelect;
73
74 /**
75 * builds the GUI
76 */
77 protected void build() {
78 model = new ConflictListModel();
79
80 lstConflicts = new JList(model);
81 lstConflicts.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
82 lstConflicts.setCellRenderer(new OsmPrimitivRenderer());
83 lstConflicts.addMouseListener(new MouseAdapter(){
84 @Override public void mouseClicked(MouseEvent e) {
85 if (e.getClickCount() >= 2) {
86 resolve();
87 }
88 }
89 });
90 lstConflicts.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
91 public void valueChanged(ListSelectionEvent e) {
92 Main.map.mapView.repaint();
93 }
94 });
95
96 add(new JScrollPane(lstConflicts), BorderLayout.CENTER);
97
98 SideButton btnResolve = new SideButton(actResolve = new ResolveAction());
99 lstConflicts.getSelectionModel().addListSelectionListener(actResolve);
100
101 SideButton btnSelect = new SideButton(actSelect = new SelectAction());
102 lstConflicts.getSelectionModel().addListSelectionListener(actSelect);
103
104 JPanel buttonPanel = getButtonPanel(2);
105 buttonPanel.add(btnResolve);
106 buttonPanel.add(btnSelect);
107 add(buttonPanel, BorderLayout.SOUTH);
108 }
109
110 /**
111 * constructor
112 */
113 public ConflictDialog() {
114 super(tr("Conflict"), "conflict", tr("Resolve conflicts."),
115 Shortcut.registerShortcut("subwindow:conflict", tr("Toggle: {0}", tr("Conflict")), KeyEvent.VK_C, Shortcut.GROUP_LAYER), 100);
116
117 build();
118 refreshView();
119 }
120
121 @Override
122 public void showNotify() {
123 DataSet.selListeners.add(this);
124 MapView.addEditLayerChangeListener(this);
125 refreshView();
126 }
127
128 @Override
129 public void hideNotify() {
130 MapView.removeEditLayerChangeListener(this);
131 DataSet.selListeners.remove(this);
132 }
133
134 /**
135 * Launches a conflict resolution dialog for the first selected conflict
136 *
137 */
138 private final void resolve() {
139 if (conflicts == null) return;
140 if (conflicts.size() == 1) {
141 lstConflicts.setSelectedIndex(0);
142 }
143
144 if (lstConflicts.getSelectedIndex() == -1)
145 return;
146
147 int [] selectedRows = lstConflicts.getSelectedIndices();
148 if (selectedRows == null || selectedRows.length == 0)
149 return;
150 int row = selectedRows[0];
151 Conflict<?> c = conflicts.get(row);
152 OsmPrimitive my = c.getMy();
153 OsmPrimitive their = c.getTheir();
154 ConflictResolutionDialog dialog = new ConflictResolutionDialog(Main.parent);
155 dialog.getConflictResolver().populate(my, their);
156 dialog.setVisible(true);
157 Main.map.mapView.repaint();
158 }
159
160 /**
161 * refreshes the view of this dialog
162 */
163 public final void refreshView() {
164 OsmDataLayer editLayer = Main.main.getEditLayer();
165 conflicts = editLayer == null?new ConflictCollection():editLayer.getConflicts();
166 model.fireContentChanged();
167 }
168
169 /**
170 * Paint all conflicts that can be expressed on the main window.
171 */
172 public void paintConflicts(final Graphics g, final NavigatableComponent nc) {
173 Color preferencesColor = getColor();
174 if (preferencesColor.equals(Main.pref.getColor(marktr("background"), Color.black)))
175 return;
176 g.setColor(preferencesColor);
177 Visitor conflictPainter = new AbstractVisitor(){
178 public void visit(Node n) {
179 Point p = nc.getPoint(n);
180 g.drawRect(p.x-1, p.y-1, 2, 2);
181 }
182 public void visit(Node n1, Node n2) {
183 Point p1 = nc.getPoint(n1);
184 Point p2 = nc.getPoint(n2);
185 g.drawLine(p1.x, p1.y, p2.x, p2.y);
186 }
187 public void visit(Way w) {
188 Node lastN = null;
189 for (Node n : w.getNodes()) {
190 if (lastN == null) {
191 lastN = n;
192 continue;
193 }
194 visit(lastN, n);
195 lastN = n;
196 }
197 }
198 public void visit(Relation e) {
199 for (RelationMember em : e.getMembers()) {
200 em.getMember().visit(this);
201 }
202 }
203 };
204 for (Object o : lstConflicts.getSelectedValues()) {
205 if (conflicts == null || !conflicts.hasConflictForMy((OsmPrimitive)o)) {
206 continue;
207 }
208 conflicts.getConflictForMy((OsmPrimitive)o).getTheir().visit(conflictPainter);
209 }
210 }
211
212 public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
213 if (oldLayer != null) {
214 oldLayer.getConflicts().removeConflictListener(this);
215 }
216 if (newLayer != null) {
217 newLayer.getConflicts().addConflictListener(this);
218 }
219 refreshView();
220 }
221
222
223 /**
224 * replies the conflict collection currently held by this dialog; may be null
225 *
226 * @return the conflict collection currently held by this dialog; may be null
227 */
228 public ConflictCollection getConflicts() {
229 return conflicts;
230 }
231
232 public void onConflictsAdded(ConflictCollection conflicts) {
233 refreshView();
234 }
235
236 public void onConflictsRemoved(ConflictCollection conflicts) {
237 refreshView();
238 }
239
240 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
241 lstConflicts.clearSelection();
242 for (OsmPrimitive osm : newSelection) {
243 if (conflicts != null && conflicts.hasConflictForMy(osm)) {
244 int pos = model.indexOf(osm);
245 if (pos >= 0) {
246 lstConflicts.addSelectionInterval(pos, pos);
247 }
248 }
249 }
250 }
251
252 @Override
253 public String helpTopic() {
254 return "Dialogs/ConflictListDialog";
255 }
256
257 /**
258 * The {@see ListModel} for conflicts
259 *
260 */
261 class ConflictListModel implements ListModel {
262
263 private CopyOnWriteArrayList<ListDataListener> listeners;
264
265 public ConflictListModel() {
266 listeners = new CopyOnWriteArrayList<ListDataListener>();
267 }
268
269 public void addListDataListener(ListDataListener l) {
270 if (l != null) {
271 listeners.addIfAbsent(l);
272 }
273 }
274
275 public void removeListDataListener(ListDataListener l) {
276 listeners.remove(l);
277 }
278
279 protected void fireContentChanged() {
280 ListDataEvent evt = new ListDataEvent(
281 this,
282 ListDataEvent.CONTENTS_CHANGED,
283 0,
284 getSize()
285 );
286 Iterator<ListDataListener> it = listeners.iterator();
287 while(it.hasNext()) {
288 it.next().contentsChanged(evt);
289 }
290 }
291
292 public Object getElementAt(int index) {
293 if (index < 0) return null;
294 if (index >= getSize()) return null;
295 return conflicts.get(index).getMy();
296 }
297
298 public int getSize() {
299 if (conflicts == null) return 0;
300 return conflicts.size();
301 }
302
303 public int indexOf(OsmPrimitive my) {
304 if (conflicts == null) return -1;
305 for (int i=0; i < conflicts.size();i++) {
306 if (conflicts.get(i).isMatchingMy(my))
307 return i;
308 }
309 return -1;
310 }
311
312 public OsmPrimitive get(int idx) {
313 if (conflicts == null) return null;
314 return conflicts.get(idx).getMy();
315 }
316 }
317
318 class ResolveAction extends AbstractAction implements ListSelectionListener {
319 public ResolveAction() {
320 putValue(NAME, tr("Resolve"));
321 putValue(SHORT_DESCRIPTION, tr("Open a merge dialog of all selected items in the list above."));
322 putValue(SMALL_ICON, ImageProvider.get("dialogs", "conflict"));
323 putValue("help", "Dialogs/ConflictListDialog#ResolveAction");
324 }
325
326 public void actionPerformed(ActionEvent e) {
327 resolve();
328 }
329
330 public void valueChanged(ListSelectionEvent e) {
331 ListSelectionModel model = (ListSelectionModel)e.getSource();
332 boolean enabled = model.getMinSelectionIndex() >= 0
333 && model.getMaxSelectionIndex() >= model.getMinSelectionIndex();
334 setEnabled(enabled);
335 }
336 }
337
338 class SelectAction extends AbstractAction implements ListSelectionListener {
339 public SelectAction() {
340 putValue(NAME, tr("Select"));
341 putValue(SHORT_DESCRIPTION, tr("Set the selected elements on the map to the selected items in the list above."));
342 putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
343 putValue("help", "Dialogs/ConflictListDialog#SelectAction");
344 }
345
346 public void actionPerformed(ActionEvent e) {
347 Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
348 for (Object o : lstConflicts.getSelectedValues()) {
349 sel.add((OsmPrimitive)o);
350 }
351 Main.main.getCurrentDataSet().setSelected(sel);
352 }
353
354 public void valueChanged(ListSelectionEvent e) {
355 ListSelectionModel model = (ListSelectionModel)e.getSource();
356 boolean enabled = model.getMinSelectionIndex() >= 0
357 && model.getMaxSelectionIndex() >= model.getMinSelectionIndex();
358 setEnabled(enabled);
359 }
360 }
361
362}
Note: See TracBrowser for help on using the repository browser.