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

Last change on this file since 12151 was 12151, checked in by michael2402, 7 years ago

AbstractListMergeModel: Add documentation

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