001/**
002 * 
003 */
004package icy.roi.edit;
005
006import icy.roi.ROI;
007
008import javax.swing.undo.CannotRedoException;
009import javax.swing.undo.CannotUndoException;
010import javax.swing.undo.UndoableEdit;
011
012/**
013 * Default lazy implementation for ROI undoable edition (full copy)
014 * 
015 * @author Stephane
016 */
017public class DefaultROIEdit extends AbstractROIEdit
018{
019    ROI previous;
020    ROI current;
021
022    public DefaultROIEdit(ROI previous, ROI current)
023    {
024        super(current);
025
026        this.previous = previous;
027        this.current = current.getCopy();
028    }
029
030    @Override
031    public void undo() throws CannotUndoException
032    {
033        super.undo();
034
035        // undo
036        getROI().copyFrom(previous);
037    }
038
039    @Override
040    public void redo() throws CannotRedoException
041    {
042        super.redo();
043
044        // redo
045        if (current != null)
046            getROI().copyFrom(current);
047    }
048
049    @Override
050    public boolean addEdit(UndoableEdit edit)
051    {
052        if (!isMergeable())
053            return false;
054
055        if (edit instanceof DefaultROIEdit)
056        {
057            final DefaultROIEdit defEdit = (DefaultROIEdit) edit;
058
059            // same ROI ?
060            if (defEdit.getROI() == getROI())
061            {
062                // collapse edits
063                current = defEdit.current;
064                return true;
065            }
066        }
067
068        return false;
069    }
070
071    @Override
072    public void die()
073    {
074        super.die();
075
076        previous = null;
077        current = null;
078    }
079}