001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.command; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.util.Collection; 007import java.util.List; 008 009import javax.swing.Icon; 010 011import org.openstreetmap.josm.data.osm.Node; 012import org.openstreetmap.josm.data.osm.OsmPrimitive; 013import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 014import org.openstreetmap.josm.data.osm.Way; 015import org.openstreetmap.josm.gui.DefaultNameFormatter; 016import org.openstreetmap.josm.tools.ImageProvider; 017 018/** 019 * Command that changes the nodes list of a way. 020 * The same can be done with ChangeCommand, but this is more 021 * efficient. (Needed for the duplicate node fixing 022 * tool of the validator plugin, when processing large data sets.) 023 * 024 * @author Imi 025 */ 026public class ChangeNodesCommand extends Command { 027 028 private final Way way; 029 private final List<Node> newNodes; 030 031 /** 032 * Constructs a new {@code ChangeNodesCommand}. 033 * @param way The way to modify 034 * @param newNodes The new list of nodes for the given way 035 */ 036 public ChangeNodesCommand(Way way, List<Node> newNodes) { 037 this.way = way; 038 this.newNodes = newNodes; 039 } 040 041 @Override 042 public boolean executeCommand() { 043 super.executeCommand(); 044 way.setNodes(newNodes); 045 way.setModified(true); 046 return true; 047 } 048 049 @Override 050 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { 051 modified.add(way); 052 } 053 054 @Override 055 public String getDescriptionText() { 056 return tr("Changed nodes of {0}", way.getDisplayName(DefaultNameFormatter.getInstance())); 057 } 058 059 @Override 060 public Icon getDescriptionIcon() { 061 return ImageProvider.get(OsmPrimitiveType.WAY); 062 } 063 064 @Override 065 public int hashCode() { 066 final int prime = 31; 067 int result = super.hashCode(); 068 result = prime * result + ((newNodes == null) ? 0 : newNodes.hashCode()); 069 result = prime * result + ((way == null) ? 0 : way.hashCode()); 070 return result; 071 } 072 073 @Override 074 public boolean equals(Object obj) { 075 if (this == obj) 076 return true; 077 if (!super.equals(obj)) 078 return false; 079 if (getClass() != obj.getClass()) 080 return false; 081 ChangeNodesCommand other = (ChangeNodesCommand) obj; 082 if (newNodes == null) { 083 if (other.newNodes != null) 084 return false; 085 } else if (!newNodes.equals(other.newNodes)) 086 return false; 087 if (way == null) { 088 if (other.way != null) 089 return false; 090 } else if (!way.equals(other.way)) 091 return false; 092 return true; 093 } 094}