001/**
002 * 
003 */
004package icy.sequence.edit;
005
006import icy.painter.Overlay;
007import icy.roi.ROI;
008import icy.sequence.Sequence;
009
010import java.awt.Image;
011import java.util.Set;
012
013import javax.swing.undo.CannotUndoException;
014
015/**
016 * Default lazy sequence undoable edit (do a complete sequence copy to restore previous state).<br>
017 * Do not handle redo operation to not consume too much memory.
018 * 
019 * @author Stephane
020 */
021public class DefaultSequenceEdit extends AbstractSequenceEdit
022{
023    Sequence previous;
024    Set<ROI> previousRois;
025    Set<Overlay> previousOverlays;
026
027    public DefaultSequenceEdit(Sequence previous, Sequence sequence, Image icon)
028    {
029        super(sequence, icon);
030
031        this.previous = previous;
032        // need to store ROI and overlays
033        previousRois = previous.getROISet();
034        previousOverlays = previous.getOverlaySet();
035    }
036
037    public DefaultSequenceEdit(Sequence previous, Sequence sequence)
038    {
039        this(previous, sequence, null);
040    }
041
042    @Override
043    public void undo() throws CannotUndoException
044    {
045        super.undo();
046
047        // undo
048        final Sequence sequence = getSequence();
049        // restore data & metadata
050        sequence.copyFrom(previous, true);
051
052        sequence.beginUpdate();
053        try
054        {
055            // restore ROIs
056            for (ROI roi : previousRois)
057                if (!sequence.contains(roi))
058                    sequence.addROI(roi);
059            for (ROI roi : sequence.getROIs())
060                if (!previousRois.contains(roi))
061                    sequence.removeROI(roi);
062
063            // restore Overlays
064            for (Overlay overlay : previousOverlays)
065                if (!sequence.contains(overlay))
066                    sequence.addOverlay(overlay);
067            for (Overlay overlay : sequence.getOverlays())
068                if (!previousOverlays.contains(overlay))
069                    sequence.removeOverlay(overlay);
070        }
071        finally
072        {
073            sequence.endUpdate();
074        }
075    }
076
077    @Override
078    public boolean canRedo()
079    {
080        return false;
081    }
082}