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