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