001/**
002 * 
003 */
004package icy.roi.edit;
005
006import icy.painter.Anchor2D;
007
008import java.awt.geom.Point2D;
009import java.util.List;
010
011import javax.swing.undo.CannotRedoException;
012import javax.swing.undo.CannotUndoException;
013import javax.swing.undo.UndoableEdit;
014
015import plugins.kernel.roi.roi2d.ROI2DShape;
016
017/**
018 * 2D control point removed implementation for ROI undoable edition.
019 * 
020 * @author Stephane
021 */
022public class Point2DRemovedROIEdit extends AbstractPoint2DROIEdit
023{
024    Point2D position;
025    final int index;
026
027    public Point2DRemovedROIEdit(ROI2DShape roi, List<Anchor2D> previousPoints, Anchor2D point)
028    {
029        super(roi, point, "ROI point removed");
030
031        position = point.getPosition();
032        
033        // we need to save the index in the old point list
034        int i = 0;
035        for(Anchor2D p: previousPoints)
036        {
037            if (p.getPosition().equals(position))
038                break;
039            
040            i++;
041        }
042        
043        index = i;
044    }
045
046    @Override
047    public void undo() throws CannotUndoException
048    {
049        super.undo();
050
051        // undo
052        point.setPosition(position);
053        ((ROI2DShape) getROI()).addPoint(point, Math.min(index, getROI2DShape().getControlPoints().size()));
054    }
055
056    @Override
057    public void redo() throws CannotRedoException
058    {
059        super.redo();
060
061        // redo
062        ((ROI2DShape) getROI()).removePoint(point);
063    }
064
065    @Override
066    public boolean addEdit(UndoableEdit edit)
067    {
068        // don't collapse here
069        return false;
070    }
071
072    @Override
073    public void die()
074    {
075        super.die();
076
077        position = null;
078    }
079
080}