001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.conflict.pair.nodes;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.ArrayList;
007import java.util.Map;
008
009import javax.swing.table.DefaultTableModel;
010
011import org.openstreetmap.josm.command.conflict.WayNodesConflictResolverCommand;
012import org.openstreetmap.josm.data.conflict.Conflict;
013import org.openstreetmap.josm.data.osm.Node;
014import org.openstreetmap.josm.data.osm.OsmPrimitive;
015import org.openstreetmap.josm.data.osm.PrimitiveId;
016import org.openstreetmap.josm.data.osm.Way;
017import org.openstreetmap.josm.gui.conflict.pair.AbstractListMergeModel;
018import org.openstreetmap.josm.gui.conflict.pair.ListRole;
019
020/**
021 * The model for merging two lists of way nodes
022 * @since 1622
023 */
024public class NodeListMergeModel extends AbstractListMergeModel<Node, WayNodesConflictResolverCommand> {
025
026    /**
027     * Populates the model with the nodes in the two {@link Way}s <code>my</code> and
028     * <code>their</code>.
029     *
030     * @param my  my way (i.e. the way in the local dataset)
031     * @param their their way (i.e. the way in the server dataset)
032     * @param mergedMap The map of merged primitives if the conflict results from merging two layers
033     * @throws IllegalArgumentException if my is null
034     * @throws IllegalArgumentException if their is null
035     */
036    public void populate(Way my, Way their, Map<PrimitiveId, PrimitiveId> mergedMap) {
037        initPopulate(my, their, mergedMap);
038
039        for (Node n : my.getNodes()) {
040            getMyEntries().add(n);
041        }
042        for (Node n : their.getNodes()) {
043            getTheirEntries().add(n);
044        }
045        if (myAndTheirEntriesEqual()) {
046            entries.put(ListRole.MERGED_ENTRIES, new ArrayList<>(getMyEntries()));
047            setFrozen(true);
048        } else {
049            setFrozen(false);
050        }
051
052        fireModelDataChanged();
053    }
054
055    @Override
056    public WayNodesConflictResolverCommand buildResolveCommand(Conflict<? extends OsmPrimitive> conflict) {
057        if (!isFrozen())
058            throw new IllegalArgumentException(tr("Merged nodes not frozen yet. Cannot build resolution command."));
059        return new WayNodesConflictResolverCommand(conflict, new ArrayList<>(getMergedEntries()));
060    }
061
062    @Override
063    public boolean isEqualEntry(Node e1, Node e2) {
064        if (!e1.isNew())
065            return e1.getId() == e2.getId();
066        else
067            return e1 == e2;
068    }
069
070    @Override
071    protected void setValueAt(DefaultTableModel model, Object value, int row, int col) {
072        // do nothing - node list tables are not editable
073    }
074
075    @Override
076    protected Node cloneEntryForMergedList(Node entry) {
077        return (Node) getMyPrimitive(entry);
078    }
079}