001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.gui.conflict.pair.nodes;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.util.ArrayList;
007    import java.util.Map;
008    
009    import javax.swing.table.DefaultTableModel;
010    
011    import org.openstreetmap.josm.command.WayNodesConflictResolverCommand;
012    import org.openstreetmap.josm.data.conflict.Conflict;
013    import org.openstreetmap.josm.data.osm.Node;
014    import org.openstreetmap.josm.data.osm.OsmPrimitive;
015    import org.openstreetmap.josm.data.osm.PrimitiveId;
016    import org.openstreetmap.josm.data.osm.Way;
017    import org.openstreetmap.josm.gui.conflict.pair.ListMergeModel;
018    import org.openstreetmap.josm.gui.conflict.pair.ListRole;
019    
020    public 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 my  my way. Must not be null.
055         * @param their  their way. Must not be null
056         * @return the command
057         * @exception IllegalStateException thrown, if the merge is not yet frozen
058         */
059        public WayNodesConflictResolverCommand buildResolveCommand(Conflict<? extends OsmPrimitive> conflict) {
060            if (! isFrozen())
061                throw new IllegalArgumentException(tr("Merged nodes not frozen yet. Cannot build resolution command."));
062            return new WayNodesConflictResolverCommand(conflict, getMergedEntries());
063        }
064    
065        @Override
066        public boolean isEqualEntry(Node e1, Node e2) {
067            if (!e1.isNew())
068                return e1.getId() == e2.getId();
069            else
070                return e1 == e2;
071        }
072    
073        @Override
074        protected void setValueAt(DefaultTableModel model, Object value, int row, int col) {
075            // do nothing - node list tables are not editable
076        }
077    
078        @Override
079        protected Node cloneEntryForMergedList(Node entry) {
080            return (Node) getMyPrimitive(entry);
081        }
082    }