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.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.ListMergeModel;
018import org.openstreetmap.josm.gui.conflict.pair.ListRole;
019
020public class NodeListMergeModel extends ListMergeModel<Node>{
021
022    /**
023     * Populates the model with the nodes in the two {@link Way}s <code>my</code> and
024     * <code>their</code>.
025     *
026     * @param my  my way (i.e. the way in the local dataset)
027     * @param their their way (i.e. the way in the server dataset)
028     * @param mergedMap The map of merged primitives if the conflict results from merging two layers
029     * @exception IllegalArgumentException thrown, if my is null
030     * @exception IllegalArgumentException  thrown, if their is null
031     */
032    public void populate(Way my, Way their, Map<PrimitiveId, PrimitiveId> mergedMap) {
033        initPopulate(my, their, mergedMap);
034
035        for (Node n : my.getNodes()) {
036            getMyEntries().add(n);
037        }
038        for (Node n : their.getNodes()) {
039            getTheirEntries().add(n);
040        }
041        if (myAndTheirEntriesEqual()) {
042            entries.put(ListRole.MERGED_ENTRIES, new ArrayList<Node>(getMyEntries()));
043            setFrozen(true);
044        } else {
045            setFrozen(false);
046        }
047
048        fireModelDataChanged();
049    }
050
051    /**
052     * Builds the command to resolve conflicts in the node list of a way
053     *
054     * @param conflict the conflict data set
055     * @return the command
056     * @exception IllegalStateException thrown, if the merge is not yet frozen
057     */
058    public WayNodesConflictResolverCommand buildResolveCommand(Conflict<? extends OsmPrimitive> conflict) {
059        if (! isFrozen())
060            throw new IllegalArgumentException(tr("Merged nodes not frozen yet. Cannot build resolution command."));
061        return new WayNodesConflictResolverCommand(conflict, getMergedEntries());
062    }
063
064    @Override
065    public boolean isEqualEntry(Node e1, Node e2) {
066        if (!e1.isNew())
067            return e1.getId() == e2.getId();
068        else
069            return e1 == e2;
070    }
071
072    @Override
073    protected void setValueAt(DefaultTableModel model, Object value, int row, int col) {
074        // do nothing - node list tables are not editable
075    }
076
077    @Override
078    protected Node cloneEntryForMergedList(Node entry) {
079        return (Node) getMyPrimitive(entry);
080    }
081}