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