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

Last change on this file was 19106, checked in by taylor.smock, 2 years ago

Cleanup some new PMD warnings from PMD 7.x (followup of r19101)

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