source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/pair/AbstractListMergeModel.java@ 16824

Last change on this file since 16824 was 16824, checked in by simon04, 4 years ago

Remove Collection.contains check for Collection.remove

  • Property svn:eol-style set to native
File size: 31.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.pair;
3
4import static org.openstreetmap.josm.gui.conflict.pair.ComparePairType.MY_WITH_MERGED;
5import static org.openstreetmap.josm.gui.conflict.pair.ComparePairType.MY_WITH_THEIR;
6import static org.openstreetmap.josm.gui.conflict.pair.ComparePairType.THEIR_WITH_MERGED;
7import static org.openstreetmap.josm.gui.conflict.pair.ListRole.MERGED_ENTRIES;
8import static org.openstreetmap.josm.gui.conflict.pair.ListRole.MY_ENTRIES;
9import static org.openstreetmap.josm.gui.conflict.pair.ListRole.THEIR_ENTRIES;
10import static org.openstreetmap.josm.tools.I18n.tr;
11
12import java.beans.PropertyChangeEvent;
13import java.beans.PropertyChangeListener;
14import java.util.ArrayList;
15import java.util.Arrays;
16import java.util.EnumMap;
17import java.util.HashSet;
18import java.util.List;
19import java.util.Map;
20import java.util.Set;
21import java.util.stream.Collectors;
22import java.util.stream.IntStream;
23
24import javax.swing.AbstractListModel;
25import javax.swing.ComboBoxModel;
26import javax.swing.DefaultListSelectionModel;
27import javax.swing.JOptionPane;
28import javax.swing.JTable;
29import javax.swing.ListSelectionModel;
30import javax.swing.table.DefaultTableModel;
31import javax.swing.table.TableModel;
32
33import org.openstreetmap.josm.command.conflict.ConflictResolveCommand;
34import org.openstreetmap.josm.data.conflict.Conflict;
35import org.openstreetmap.josm.data.osm.DataSet;
36import org.openstreetmap.josm.data.osm.OsmPrimitive;
37import org.openstreetmap.josm.data.osm.PrimitiveId;
38import org.openstreetmap.josm.data.osm.RelationMember;
39import org.openstreetmap.josm.gui.HelpAwareOptionPane;
40import org.openstreetmap.josm.gui.MainApplication;
41import org.openstreetmap.josm.gui.help.HelpUtil;
42import org.openstreetmap.josm.gui.util.ChangeNotifier;
43import org.openstreetmap.josm.gui.util.TableHelper;
44import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTableModel;
45import org.openstreetmap.josm.tools.CheckParameterUtil;
46import org.openstreetmap.josm.tools.Logging;
47import org.openstreetmap.josm.tools.Utils;
48
49/**
50 * ListMergeModel is a model for interactively comparing and merging two list of entries
51 * of type T. It maintains three lists of entries of type T:
52 * <ol>
53 * <li>the list of <em>my</em> entries</li>
54 * <li>the list of <em>their</em> entries</li>
55 * <li>the list of <em>merged</em> entries</li>
56 * </ol>
57 *
58 * A ListMergeModel is a factory for three {@link TableModel}s and three {@link ListSelectionModel}s:
59 * <ol>
60 * <li>the table model and the list selection for for a {@link JTable} which shows my entries.
61 * See {@link #getMyTableModel()} and {@link AbstractListMergeModel#getMySelectionModel()}</li>
62 * <li>dito for their entries and merged entries</li>
63 * </ol>
64 *
65 * A ListMergeModel can be ''frozen''. If it's frozen, it doesn't accept additional merge
66 * decisions. {@link PropertyChangeListener}s can register for property value changes of
67 * {@link #FROZEN_PROP}.
68 *
69 * ListMergeModel is an abstract class. Three methods have to be implemented by subclasses:
70 * <ul>
71 * <li>{@link AbstractListMergeModel#cloneEntryForMergedList} - clones an entry of type T</li>
72 * <li>{@link AbstractListMergeModel#isEqualEntry} - checks whether two entries are equals </li>
73 * <li>{@link AbstractListMergeModel#setValueAt(DefaultTableModel, Object, int, int)} - handles values edited in
74 * a JTable, dispatched from {@link TableModel#setValueAt(Object, int, int)} </li>
75 * </ul>
76 * A ListMergeModel is used in combination with a {@link AbstractListMerger}.
77 *
78 * @param <T> the type of the list entries
79 * @param <C> the type of conflict resolution command
80 * @see AbstractListMerger
81 * @see PairTable For the table displaying this model
82 */
83public abstract class AbstractListMergeModel<T extends PrimitiveId, C extends ConflictResolveCommand> extends ChangeNotifier {
84 /**
85 * The property name to listen for frozen changes.
86 * @see #setFrozen(boolean)
87 * @see #isFrozen()
88 */
89 public static final String FROZEN_PROP = AbstractListMergeModel.class.getName() + ".frozen";
90
91 private static final int MAX_DELETED_PRIMITIVE_IN_DIALOG = 5;
92
93 protected Map<ListRole, ArrayList<T>> entries;
94
95 protected EntriesTableModel myEntriesTableModel;
96 protected EntriesTableModel theirEntriesTableModel;
97 protected EntriesTableModel mergedEntriesTableModel;
98
99 protected EntriesSelectionModel myEntriesSelectionModel;
100 protected EntriesSelectionModel theirEntriesSelectionModel;
101 protected EntriesSelectionModel mergedEntriesSelectionModel;
102
103 private final Set<PropertyChangeListener> listeners;
104 private boolean isFrozen;
105 private final ComparePairListModel comparePairListModel;
106
107 private DataSet myDataset;
108 private Map<PrimitiveId, PrimitiveId> mergedMap;
109
110 /**
111 * Creates a clone of an entry of type T suitable to be included in the
112 * list of merged entries
113 *
114 * @param entry the entry
115 * @return the cloned entry
116 */
117 protected abstract T cloneEntryForMergedList(T entry);
118
119 /**
120 * checks whether two entries are equal. This is not necessarily the same as
121 * e1.equals(e2).
122 *
123 * @param e1 the first entry
124 * @param e2 the second entry
125 * @return true, if the entries are equal, false otherwise.
126 */
127 public abstract boolean isEqualEntry(T e1, T e2);
128
129 /**
130 * Handles method dispatches from {@link TableModel#setValueAt(Object, int, int)}.
131 *
132 * @param model the table model
133 * @param value the value to be set
134 * @param row the row index
135 * @param col the column index
136 *
137 * @see TableModel#setValueAt(Object, int, int)
138 */
139 protected abstract void setValueAt(DefaultTableModel model, Object value, int row, int col);
140
141 /**
142 * Replies primitive from my dataset referenced by entry
143 * @param entry entry
144 * @return Primitive from my dataset referenced by entry
145 */
146 public OsmPrimitive getMyPrimitive(T entry) {
147 return getMyPrimitiveById(entry);
148 }
149
150 public final OsmPrimitive getMyPrimitiveById(PrimitiveId entry) {
151 OsmPrimitive result = myDataset.getPrimitiveById(entry);
152 if (result == null && mergedMap != null) {
153 PrimitiveId id = mergedMap.get(entry);
154 if (id == null && entry instanceof OsmPrimitive) {
155 id = mergedMap.get(((OsmPrimitive) entry).getPrimitiveId());
156 }
157 if (id != null) {
158 result = myDataset.getPrimitiveById(id);
159 }
160 }
161 return result;
162 }
163
164 protected void buildMyEntriesTableModel() {
165 myEntriesTableModel = new EntriesTableModel(MY_ENTRIES);
166 }
167
168 protected void buildTheirEntriesTableModel() {
169 theirEntriesTableModel = new EntriesTableModel(THEIR_ENTRIES);
170 }
171
172 protected void buildMergedEntriesTableModel() {
173 mergedEntriesTableModel = new EntriesTableModel(MERGED_ENTRIES);
174 }
175
176 protected List<T> getMergedEntries() {
177 return entries.get(MERGED_ENTRIES);
178 }
179
180 protected List<T> getMyEntries() {
181 return entries.get(MY_ENTRIES);
182 }
183
184 protected List<T> getTheirEntries() {
185 return entries.get(THEIR_ENTRIES);
186 }
187
188 public int getMyEntriesSize() {
189 return getMyEntries().size();
190 }
191
192 public int getMergedEntriesSize() {
193 return getMergedEntries().size();
194 }
195
196 public int getTheirEntriesSize() {
197 return getTheirEntries().size();
198 }
199
200 /**
201 * Constructs a new {@code ListMergeModel}.
202 */
203 protected AbstractListMergeModel() {
204 entries = new EnumMap<>(ListRole.class);
205 for (ListRole role : ListRole.values()) {
206 entries.put(role, new ArrayList<T>());
207 }
208
209 buildMyEntriesTableModel();
210 buildTheirEntriesTableModel();
211 buildMergedEntriesTableModel();
212
213 myEntriesSelectionModel = new EntriesSelectionModel(entries.get(MY_ENTRIES));
214 theirEntriesSelectionModel = new EntriesSelectionModel(entries.get(THEIR_ENTRIES));
215 mergedEntriesSelectionModel = new EntriesSelectionModel(entries.get(MERGED_ENTRIES));
216
217 listeners = new HashSet<>();
218 comparePairListModel = new ComparePairListModel();
219
220 setFrozen(true);
221 }
222
223 public void addPropertyChangeListener(PropertyChangeListener listener) {
224 synchronized (listeners) {
225 if (listener != null) {
226 listeners.add(listener);
227 }
228 }
229 }
230
231 public void removePropertyChangeListener(PropertyChangeListener listener) {
232 synchronized (listeners) {
233 if (listener != null) {
234 listeners.remove(listener);
235 }
236 }
237 }
238
239 protected void fireFrozenChanged(boolean oldValue, boolean newValue) {
240 synchronized (listeners) {
241 PropertyChangeEvent evt = new PropertyChangeEvent(this, FROZEN_PROP, oldValue, newValue);
242 listeners.forEach(listener -> listener.propertyChange(evt));
243 }
244 }
245
246 /**
247 * Sets the frozen status for this model.
248 * @param isFrozen <code>true</code> if it should be frozen.
249 */
250 public final void setFrozen(boolean isFrozen) {
251 boolean oldValue = this.isFrozen;
252 this.isFrozen = isFrozen;
253 fireFrozenChanged(oldValue, this.isFrozen);
254 }
255
256 /**
257 * Check if the model is frozen.
258 * @return The current frozen state.
259 */
260 public final boolean isFrozen() {
261 return isFrozen;
262 }
263
264 public OsmPrimitivesTableModel getMyTableModel() {
265 return myEntriesTableModel;
266 }
267
268 public OsmPrimitivesTableModel getTheirTableModel() {
269 return theirEntriesTableModel;
270 }
271
272 public OsmPrimitivesTableModel getMergedTableModel() {
273 return mergedEntriesTableModel;
274 }
275
276 public EntriesSelectionModel getMySelectionModel() {
277 return myEntriesSelectionModel;
278 }
279
280 public EntriesSelectionModel getTheirSelectionModel() {
281 return theirEntriesSelectionModel;
282 }
283
284 public EntriesSelectionModel getMergedSelectionModel() {
285 return mergedEntriesSelectionModel;
286 }
287
288 protected void fireModelDataChanged() {
289 myEntriesTableModel.fireTableDataChanged();
290 theirEntriesTableModel.fireTableDataChanged();
291 mergedEntriesTableModel.fireTableDataChanged();
292 fireStateChanged();
293 }
294
295 protected void copyToTop(ListRole role, int... rows) {
296 copy(role, rows, 0);
297 mergedEntriesSelectionModel.setSelectionInterval(0, rows.length -1);
298 }
299
300 /**
301 * Copies the nodes given by indices in rows from the list of my nodes to the
302 * list of merged nodes. Inserts the nodes at the top of the list of merged
303 * nodes.
304 *
305 * @param rows the indices
306 */
307 public void copyMyToTop(int... rows) {
308 copyToTop(MY_ENTRIES, rows);
309 }
310
311 /**
312 * Copies the nodes given by indices in rows from the list of their nodes to the
313 * list of merged nodes. Inserts the nodes at the top of the list of merged
314 * nodes.
315 *
316 * @param rows the indices
317 */
318 public void copyTheirToTop(int... rows) {
319 copyToTop(THEIR_ENTRIES, rows);
320 }
321
322 /**
323 * Copies the nodes given by indices in rows from the list of nodes in source to the
324 * list of merged nodes. Inserts the nodes at the end of the list of merged
325 * nodes.
326 *
327 * @param source the list of nodes to copy from
328 * @param rows the indices
329 */
330
331 public void copyToEnd(ListRole source, int... rows) {
332 copy(source, rows, getMergedEntriesSize());
333 mergedEntriesSelectionModel.setSelectionInterval(getMergedEntriesSize()-rows.length, getMergedEntriesSize() -1);
334
335 }
336
337 /**
338 * Copies the nodes given by indices in rows from the list of my nodes to the
339 * list of merged nodes. Inserts the nodes at the end of the list of merged
340 * nodes.
341 *
342 * @param rows the indices
343 */
344 public void copyMyToEnd(int... rows) {
345 copyToEnd(MY_ENTRIES, rows);
346 }
347
348 /**
349 * Copies the nodes given by indices in rows from the list of their nodes to the
350 * list of merged nodes. Inserts the nodes at the end of the list of merged
351 * nodes.
352 *
353 * @param rows the indices
354 */
355 public void copyTheirToEnd(int... rows) {
356 copyToEnd(THEIR_ENTRIES, rows);
357 }
358
359 public void clearMerged() {
360 getMergedEntries().clear();
361 fireModelDataChanged();
362 }
363
364 protected final void initPopulate(OsmPrimitive my, OsmPrimitive their, Map<PrimitiveId, PrimitiveId> mergedMap) {
365 CheckParameterUtil.ensureParameterNotNull(my, "my");
366 CheckParameterUtil.ensureParameterNotNull(their, "their");
367 this.myDataset = my.getDataSet();
368 this.mergedMap = mergedMap;
369 getMergedEntries().clear();
370 getMyEntries().clear();
371 getTheirEntries().clear();
372 }
373
374 protected void alertCopyFailedForDeletedPrimitives(List<PrimitiveId> deletedIds) {
375 List<String> items = deletedIds.stream().limit(MAX_DELETED_PRIMITIVE_IN_DIALOG).map(Object::toString).collect(Collectors.toList());
376 if (deletedIds.size() > MAX_DELETED_PRIMITIVE_IN_DIALOG) {
377 items.add(tr("{0} more...", deletedIds.size() - MAX_DELETED_PRIMITIVE_IN_DIALOG));
378 }
379 StringBuilder sb = new StringBuilder();
380 sb.append("<html>")
381 .append(tr("The following objects could not be copied to the target object<br>because they are deleted in the target dataset:"))
382 .append(Utils.joinAsHtmlUnorderedList(items))
383 .append("</html>");
384 HelpAwareOptionPane.showOptionDialog(
385 MainApplication.getMainFrame(),
386 sb.toString(),
387 tr("Merging deleted objects failed"),
388 JOptionPane.WARNING_MESSAGE,
389 HelpUtil.ht("/Dialog/Conflict#MergingDeletedPrimitivesFailed")
390 );
391 }
392
393 private void copy(ListRole sourceRole, int[] rows, int position) {
394 if (position < 0 || position > getMergedEntriesSize())
395 throw new IllegalArgumentException("Position must be between 0 and "+getMergedEntriesSize()+" but is "+position);
396 List<T> newItems = new ArrayList<>(rows.length);
397 List<T> source = entries.get(sourceRole);
398 List<PrimitiveId> deletedIds = new ArrayList<>();
399 for (int row: rows) {
400 T entry = source.get(row);
401 OsmPrimitive primitive = getMyPrimitive(entry);
402 if (primitive != null) {
403 if (!primitive.isDeleted()) {
404 T clone = cloneEntryForMergedList(entry);
405 newItems.add(clone);
406 } else {
407 deletedIds.add(primitive.getPrimitiveId());
408 }
409 }
410 }
411 getMergedEntries().addAll(position, newItems);
412 fireModelDataChanged();
413 if (!deletedIds.isEmpty()) {
414 alertCopyFailedForDeletedPrimitives(deletedIds);
415 }
416 }
417
418 /**
419 * Copies over all values from the given side to the merged table..
420 * @param source The source side to copy from.
421 */
422 public void copyAll(ListRole source) {
423 getMergedEntries().clear();
424
425 int[] rows = IntStream.range(0, entries.get(source).size()).toArray();
426 copy(source, rows, 0);
427 }
428
429 /**
430 * Copies the nodes given by indices in rows from the list of nodes <code>source</code> to the
431 * list of merged nodes. Inserts the nodes before row given by current.
432 *
433 * @param source the list of nodes to copy from
434 * @param rows the indices
435 * @param current the row index before which the nodes are inserted
436 * @throws IllegalArgumentException if current &lt; 0 or &gt;= #nodes in list of merged nodes
437 */
438 protected void copyBeforeCurrent(ListRole source, int[] rows, int current) {
439 copy(source, rows, current);
440 mergedEntriesSelectionModel.setSelectionInterval(current, current + rows.length-1);
441 }
442
443 /**
444 * Copies the nodes given by indices in rows from the list of my nodes to the
445 * list of merged nodes. Inserts the nodes before row given by current.
446 *
447 * @param rows the indices
448 * @param current the row index before which the nodes are inserted
449 * @throws IllegalArgumentException if current &lt; 0 or &gt;= #nodes in list of merged nodes
450 */
451 public void copyMyBeforeCurrent(int[] rows, int current) {
452 copyBeforeCurrent(MY_ENTRIES, rows, current);
453 }
454
455 /**
456 * Copies the nodes given by indices in rows from the list of their nodes to the
457 * list of merged nodes. Inserts the nodes before row given by current.
458 *
459 * @param rows the indices
460 * @param current the row index before which the nodes are inserted
461 * @throws IllegalArgumentException if current &lt; 0 or &gt;= #nodes in list of merged nodes
462 */
463 public void copyTheirBeforeCurrent(int[] rows, int current) {
464 copyBeforeCurrent(THEIR_ENTRIES, rows, current);
465 }
466
467 /**
468 * Copies the nodes given by indices in rows from the list of nodes <code>source</code> to the
469 * list of merged nodes. Inserts the nodes after the row given by current.
470 *
471 * @param source the list of nodes to copy from
472 * @param rows the indices
473 * @param current the row index after which the nodes are inserted
474 * @throws IllegalArgumentException if current &lt; 0 or &gt;= #nodes in list of merged nodes
475 */
476 protected void copyAfterCurrent(ListRole source, int[] rows, int current) {
477 copy(source, rows, current + 1);
478 mergedEntriesSelectionModel.setSelectionInterval(current+1, current + rows.length-1);
479 fireStateChanged();
480 }
481
482 /**
483 * Copies the nodes given by indices in rows from the list of my nodes to the
484 * list of merged nodes. Inserts the nodes after the row given by current.
485 *
486 * @param rows the indices
487 * @param current the row index after which the nodes are inserted
488 * @throws IllegalArgumentException if current &lt; 0 or &gt;= #nodes in list of merged nodes
489 */
490 public void copyMyAfterCurrent(int[] rows, int current) {
491 copyAfterCurrent(MY_ENTRIES, rows, current);
492 }
493
494 /**
495 * Copies the nodes given by indices in rows from the list of my nodes to the
496 * list of merged nodes. Inserts the nodes after the row given by current.
497 *
498 * @param rows the indices
499 * @param current the row index after which the nodes are inserted
500 * @throws IllegalArgumentException if current &lt; 0 or &gt;= #nodes in list of merged nodes
501 */
502 public void copyTheirAfterCurrent(int[] rows, int current) {
503 copyAfterCurrent(THEIR_ENTRIES, rows, current);
504 }
505
506 /**
507 * Moves the nodes given by indices in rows up by one position in the list
508 * of merged nodes.
509 *
510 * @param rows the indices
511 *
512 */
513 public void moveUpMerged(int... rows) {
514 if (rows == null || rows.length == 0)
515 return;
516 if (rows[0] == 0)
517 // can't move up
518 return;
519 List<T> mergedEntries = getMergedEntries();
520 for (int row: rows) {
521 T n = mergedEntries.get(row);
522 mergedEntries.remove(row);
523 mergedEntries.add(row -1, n);
524 }
525 fireModelDataChanged();
526 TableHelper.setSelectedIndices(mergedEntriesSelectionModel, Arrays.stream(rows).map(row -> row - 1));
527 }
528
529 /**
530 * Moves the nodes given by indices in rows down by one position in the list
531 * of merged nodes.
532 *
533 * @param rows the indices
534 */
535 public void moveDownMerged(int... rows) {
536 if (rows == null || rows.length == 0)
537 return;
538 List<T> mergedEntries = getMergedEntries();
539 if (rows[rows.length -1] == mergedEntries.size() -1)
540 // can't move down
541 return;
542 for (int i = rows.length-1; i >= 0; i--) {
543 int row = rows[i];
544 T n = mergedEntries.get(row);
545 mergedEntries.remove(row);
546 mergedEntries.add(row +1, n);
547 }
548 fireModelDataChanged();
549 TableHelper.setSelectedIndices(mergedEntriesSelectionModel, Arrays.stream(rows).map(row -> row + 1));
550 }
551
552 /**
553 * Removes the nodes given by indices in rows from the list
554 * of merged nodes.
555 *
556 * @param rows the indices
557 */
558 public void removeMerged(int... rows) {
559 if (rows == null || rows.length == 0)
560 return;
561
562 List<T> mergedEntries = getMergedEntries();
563
564 for (int i = rows.length-1; i >= 0; i--) {
565 mergedEntries.remove(rows[i]);
566 }
567 fireModelDataChanged();
568 mergedEntriesSelectionModel.clearSelection();
569 }
570
571 /**
572 * Replies true if the list of my entries and the list of their
573 * entries are equal
574 *
575 * @return true, if the lists are equal; false otherwise
576 */
577 protected boolean myAndTheirEntriesEqual() {
578 return getMyEntriesSize() == getTheirEntriesSize()
579 && IntStream.range(0, getMyEntriesSize()).allMatch(i -> isEqualEntry(getMyEntries().get(i), getTheirEntries().get(i)));
580 }
581
582 /**
583 * This an adapter between a {@link JTable} and one of the three entry lists
584 * in the role {@link ListRole} managed by the {@link AbstractListMergeModel}.
585 *
586 * From the point of view of the {@link JTable} it is a {@link TableModel}.
587 *
588 * @see AbstractListMergeModel#getMyTableModel()
589 * @see AbstractListMergeModel#getTheirTableModel()
590 * @see AbstractListMergeModel#getMergedTableModel()
591 */
592 public class EntriesTableModel extends DefaultTableModel implements OsmPrimitivesTableModel {
593 private final ListRole role;
594
595 /**
596 *
597 * @param role the role
598 */
599 public EntriesTableModel(ListRole role) {
600 this.role = role;
601 }
602
603 @Override
604 public int getRowCount() {
605 int count = Math.max(getMyEntries().size(), getMergedEntries().size());
606 return Math.max(count, getTheirEntries().size());
607 }
608
609 @Override
610 public Object getValueAt(int row, int column) {
611 if (row < entries.get(role).size())
612 return entries.get(role).get(row);
613 return null;
614 }
615
616 @Override
617 public boolean isCellEditable(int row, int column) {
618 return false;
619 }
620
621 @Override
622 public void setValueAt(Object value, int row, int col) {
623 AbstractListMergeModel.this.setValueAt(this, value, row, col);
624 }
625
626 /**
627 * Returns the list merge model.
628 * @return the list merge model
629 */
630 public AbstractListMergeModel<T, C> getListMergeModel() {
631 return AbstractListMergeModel.this;
632 }
633
634 /**
635 * replies true if the {@link ListRole} of this {@link EntriesTableModel}
636 * participates in the current {@link ComparePairType}
637 *
638 * @return true, if the if the {@link ListRole} of this {@link EntriesTableModel}
639 * participates in the current {@link ComparePairType}
640 *
641 * @see AbstractListMergeModel.ComparePairListModel#getSelectedComparePair()
642 */
643 public boolean isParticipatingInCurrentComparePair() {
644 return getComparePairListModel()
645 .getSelectedComparePair()
646 .isParticipatingIn(role);
647 }
648
649 /**
650 * replies true if the entry at <code>row</code> is equal to the entry at the
651 * same position in the opposite list of the current {@link ComparePairType}.
652 *
653 * @param row the row number
654 * @return true if the entry at <code>row</code> is equal to the entry at the
655 * same position in the opposite list of the current {@link ComparePairType}
656 * @throws IllegalStateException if this model is not participating in the
657 * current {@link ComparePairType}
658 * @see ComparePairType#getOppositeRole(ListRole)
659 * @see #getRole()
660 * @see #getOppositeEntries()
661 */
662 public boolean isSamePositionInOppositeList(int row) {
663 if (!isParticipatingInCurrentComparePair())
664 throw new IllegalStateException(tr("List in role {0} is currently not participating in a compare pair.", role.toString()));
665 if (row >= getEntries().size()) return false;
666 if (row >= getOppositeEntries().size()) return false;
667
668 T e1 = getEntries().get(row);
669 T e2 = getOppositeEntries().get(row);
670 return isEqualEntry(e1, e2);
671 }
672
673 /**
674 * replies true if the entry at the current position is present in the opposite list
675 * of the current {@link ComparePairType}.
676 *
677 * @param row the current row
678 * @return true if the entry at the current position is present in the opposite list
679 * of the current {@link ComparePairType}.
680 * @throws IllegalStateException if this model is not participating in the
681 * current {@link ComparePairType}
682 * @see ComparePairType#getOppositeRole(ListRole)
683 * @see #getRole()
684 * @see #getOppositeEntries()
685 */
686 public boolean isIncludedInOppositeList(int row) {
687 if (!isParticipatingInCurrentComparePair())
688 throw new IllegalStateException(tr("List in role {0} is currently not participating in a compare pair.", role.toString()));
689
690 if (row >= getEntries().size()) return false;
691 T e1 = getEntries().get(row);
692 return getOppositeEntries().stream().anyMatch(e2 -> isEqualEntry(e1, e2));
693 }
694
695 protected List<T> getEntries() {
696 return entries.get(role);
697 }
698
699 /**
700 * replies the opposite list of entries with respect to the current {@link ComparePairType}
701 *
702 * @return the opposite list of entries
703 */
704 protected List<T> getOppositeEntries() {
705 ListRole opposite = getComparePairListModel().getSelectedComparePair().getOppositeRole(role);
706 return entries.get(opposite);
707 }
708
709 /**
710 * Get the role of the table.
711 * @return The role.
712 */
713 public ListRole getRole() {
714 return role;
715 }
716
717 @Override
718 public OsmPrimitive getReferredPrimitive(int idx) {
719 Object value = getValueAt(idx, 1);
720 if (value instanceof OsmPrimitive) {
721 return (OsmPrimitive) value;
722 } else if (value instanceof RelationMember) {
723 return ((RelationMember) value).getMember();
724 } else {
725 Logging.error("Unknown object type: "+value);
726 return null;
727 }
728 }
729 }
730
731 /**
732 * This is the selection model to be used in a {@link JTable} which displays
733 * an entry list managed by {@link AbstractListMergeModel}.
734 *
735 * The model ensures that only rows displaying an entry in the entry list
736 * can be selected. "Empty" rows can't be selected.
737 *
738 * @see AbstractListMergeModel#getMySelectionModel()
739 * @see AbstractListMergeModel#getMergedSelectionModel()
740 * @see AbstractListMergeModel#getTheirSelectionModel()
741 *
742 */
743 protected class EntriesSelectionModel extends DefaultListSelectionModel {
744 private final transient List<T> entries;
745
746 public EntriesSelectionModel(List<T> nodes) {
747 this.entries = nodes;
748 }
749
750 @Override
751 public void addSelectionInterval(int index0, int index1) {
752 if (entries.isEmpty()) return;
753 if (index0 > entries.size() - 1) return;
754 index0 = Math.min(entries.size()-1, index0);
755 index1 = Math.min(entries.size()-1, index1);
756 super.addSelectionInterval(index0, index1);
757 }
758
759 @Override
760 public void insertIndexInterval(int index, int length, boolean before) {
761 if (entries.isEmpty()) return;
762 if (before) {
763 int newindex = Math.min(entries.size()-1, index);
764 if (newindex < index - length) return;
765 length = length - (index - newindex);
766 super.insertIndexInterval(newindex, length, before);
767 } else {
768 if (index > entries.size() -1) return;
769 length = Math.min(entries.size()-1 - index, length);
770 super.insertIndexInterval(index, length, before);
771 }
772 }
773
774 @Override
775 public void moveLeadSelectionIndex(int leadIndex) {
776 if (entries.isEmpty()) return;
777 leadIndex = Math.max(0, leadIndex);
778 leadIndex = Math.min(entries.size() - 1, leadIndex);
779 super.moveLeadSelectionIndex(leadIndex);
780 }
781
782 @Override
783 public void removeIndexInterval(int index0, int index1) {
784 if (entries.isEmpty()) return;
785 index0 = Math.max(0, index0);
786 index0 = Math.min(entries.size() - 1, index0);
787
788 index1 = Math.max(0, index1);
789 index1 = Math.min(entries.size() - 1, index1);
790 super.removeIndexInterval(index0, index1);
791 }
792
793 @Override
794 public void removeSelectionInterval(int index0, int index1) {
795 if (entries.isEmpty()) return;
796 index0 = Math.max(0, index0);
797 index0 = Math.min(entries.size() - 1, index0);
798
799 index1 = Math.max(0, index1);
800 index1 = Math.min(entries.size() - 1, index1);
801 super.removeSelectionInterval(index0, index1);
802 }
803
804 @Override
805 public void setAnchorSelectionIndex(int anchorIndex) {
806 if (entries.isEmpty()) return;
807 anchorIndex = Math.min(entries.size() - 1, anchorIndex);
808 super.setAnchorSelectionIndex(anchorIndex);
809 }
810
811 @Override
812 public void setLeadSelectionIndex(int leadIndex) {
813 if (entries.isEmpty()) return;
814 leadIndex = Math.min(entries.size() - 1, leadIndex);
815 super.setLeadSelectionIndex(leadIndex);
816 }
817
818 @Override
819 public void setSelectionInterval(int index0, int index1) {
820 if (entries.isEmpty()) return;
821 index0 = Math.max(0, index0);
822 index0 = Math.min(entries.size() - 1, index0);
823
824 index1 = Math.max(0, index1);
825 index1 = Math.min(entries.size() - 1, index1);
826
827 super.setSelectionInterval(index0, index1);
828 }
829 }
830
831 public ComparePairListModel getComparePairListModel() {
832 return this.comparePairListModel;
833 }
834
835 public class ComparePairListModel extends AbstractListModel<ComparePairType> implements ComboBoxModel<ComparePairType> {
836
837 private int selectedIdx;
838 private final List<ComparePairType> compareModes;
839
840 /**
841 * Constructs a new {@code ComparePairListModel}.
842 */
843 public ComparePairListModel() {
844 this.compareModes = new ArrayList<>();
845 compareModes.add(MY_WITH_THEIR);
846 compareModes.add(MY_WITH_MERGED);
847 compareModes.add(THEIR_WITH_MERGED);
848 selectedIdx = 0;
849 }
850
851 @Override
852 public ComparePairType getElementAt(int index) {
853 if (index < compareModes.size())
854 return compareModes.get(index);
855 throw new IllegalArgumentException(tr("Unexpected value of parameter ''index''. Got {0}.", index));
856 }
857
858 @Override
859 public int getSize() {
860 return compareModes.size();
861 }
862
863 @Override
864 public Object getSelectedItem() {
865 return compareModes.get(selectedIdx);
866 }
867
868 @Override
869 public void setSelectedItem(Object anItem) {
870 int i = compareModes.indexOf(anItem);
871 if (i < 0)
872 throw new IllegalStateException(tr("Item {0} not found in list.", anItem));
873 selectedIdx = i;
874 fireModelDataChanged();
875 }
876
877 public ComparePairType getSelectedComparePair() {
878 return compareModes.get(selectedIdx);
879 }
880 }
881
882 /**
883 * Builds the command to resolve conflicts in the list.
884 *
885 * @param conflict the conflict data set
886 * @return the command
887 * @throws IllegalStateException if the merge is not yet frozen
888 */
889 public abstract C buildResolveCommand(Conflict<? extends OsmPrimitive> conflict);
890}
Note: See TracBrowser for help on using the repository browser.