001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.command;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import org.openstreetmap.josm.Main;
007    import org.openstreetmap.josm.data.conflict.Conflict;
008    import org.openstreetmap.josm.data.conflict.ConflictCollection;
009    import org.openstreetmap.josm.gui.layer.OsmDataLayer;
010    
011    /**
012     * This is the common base class for {@link Command}s which manipulate {@link Conflict}s in
013     * addition to {@link OsmPrimitive}s.
014     *
015     * A ConflictResolverCommand can remember a collection of conflicts it resolves. Upon undoing
016     * it reconstitutes them.
017     *
018     */
019    public abstract class ConflictResolveCommand extends Command {
020        /** the list of resolved conflicts */
021        private ConflictCollection resolvedConflicts;
022    
023        public ConflictResolveCommand() {
024            super();
025            resolvedConflicts = new ConflictCollection();
026        }
027    
028        public ConflictResolveCommand(OsmDataLayer layer) {
029            super(layer);
030            resolvedConflicts = new ConflictCollection();
031        }
032    
033        /**
034         * remembers a conflict in the internal list of remembered conflicts
035         *
036         * @param c the remembered conflict
037         */
038        protected void rememberConflict(Conflict<?> c) {
039            if (! resolvedConflicts.hasConflictForMy(c.getMy())) {
040                resolvedConflicts.add(c);
041            }
042        }
043    
044        /**
045         * reconstitutes all remembered conflicts. Add the remembered conflicts to the
046         * set of conflicts of the {@link OsmDataLayer} this command was applied to.
047         *
048         */
049        protected void reconstituteConflicts() {
050            OsmDataLayer editLayer = getLayer();
051            for(Conflict<?> c : resolvedConflicts) {
052                if (!editLayer.getConflicts().hasConflictForMy(c.getMy())) {
053                    editLayer.getConflicts().add(c);
054                }
055            }
056        }
057    
058        @Override
059        public void undoCommand() {
060            super.undoCommand();
061    
062            if (! Main.map.mapView.hasLayer(getLayer())) {
063                System.out.println(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
064                        this.toString(),
065                        getLayer().toString()
066                ));
067                return;
068            }
069    
070            Main.map.mapView.setActiveLayer(getLayer());
071            reconstituteConflicts();
072        }
073    }