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 * 2D control point added implementation for ROI undoable edition.
018 * 
019 * @author Stephane
020 */
021public class Point2DAddedROIEdit extends AbstractPoint2DROIEdit
022{
023    Point2D position;
024    final int index;
025
026    public Point2DAddedROIEdit(ROI2DShape roi, Anchor2D point)
027    {
028        super(roi, point, "ROI point added");
029
030        position = point.getPosition();
031        index = roi.getControlPoints().indexOf(point);
032    }
033
034    @Override
035    public void undo() throws CannotUndoException
036    {
037        super.undo();
038
039        // undo
040        ((ROI2DShape) getROI()).removePoint(point);
041    }
042
043    @Override
044    public void redo() throws CannotRedoException
045    {
046        super.redo();
047
048        // redo
049        point.setPosition(position);
050        ((ROI2DShape) getROI()).addPoint(point, Math.min(index, getROI2DShape().getControlPoints().size()));
051    }
052
053    @Override
054    public boolean addEdit(UndoableEdit edit)
055    {
056        // don't collapse here
057        return false;
058    }
059
060    @Override
061    public void die()
062    {
063        super.die();
064
065        position = null;
066    }
067}