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