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