001package icy.roi.edit;
002
003import icy.roi.ROI;
004import icy.type.rectangle.Rectangle5D;
005
006import javax.swing.undo.CannotRedoException;
007import javax.swing.undo.CannotUndoException;
008import javax.swing.undo.UndoableEdit;
009
010/**
011 * Position change implementation for ROI undoable edition.
012 * 
013 * @author Stephane
014 */
015public class BoundsROIEdit extends AbstractROIEdit
016{
017    Rectangle5D prevBounds;
018    Rectangle5D currentBounds;
019
020    public BoundsROIEdit(ROI roi, Rectangle5D prevBounds, boolean mergeable)
021    {
022        super(roi, "ROI bounds changed");
023
024        this.prevBounds = prevBounds;
025        this.currentBounds = roi.getBounds5D();
026
027        setMergeable(mergeable);
028    }
029
030    public BoundsROIEdit(ROI roi, Rectangle5D prevBounds)
031    {
032        this(roi, prevBounds, true);
033    }
034
035    @Override
036    public void undo() throws CannotUndoException
037    {
038        super.undo();
039
040        // undo
041        getROI().setBounds5D(prevBounds);
042    }
043
044    @Override
045    public void redo() throws CannotRedoException
046    {
047        super.redo();
048
049        // redo
050        getROI().setBounds5D(currentBounds);
051    }
052
053    @Override
054    public boolean addEdit(UndoableEdit edit)
055    {
056        if (!isMergeable())
057            return false;
058
059        if (edit instanceof BoundsROIEdit)
060        {
061            final BoundsROIEdit bndEdit = (BoundsROIEdit) edit;
062
063            // same ROI ?
064            if (bndEdit.getROI() == getROI())
065            {
066                // collapse edits
067                currentBounds = bndEdit.currentBounds;
068                return true;
069            }
070        }
071
072        return false;
073    }
074
075    @Override
076    public void die()
077    {
078        super.die();
079
080        prevBounds = null;
081        currentBounds = null;
082    }
083}