001/**
002 * 
003 */
004package plugins.tutorial.undo;
005
006import icy.gui.dialog.ConfirmDialog;
007import icy.math.ArrayMath;
008import icy.plugin.abstract_.PluginActionable;
009import icy.sequence.Sequence;
010import icy.type.collection.array.Array1DUtil;
011
012/**
013 * This plugin demonstrate how to use the Undo framework in a very lazy / simple way
014 * 
015 * @author Stephane
016 */
017public class SimpleUndo extends PluginActionable
018{
019    @Override
020    public void run()
021    {
022        final Sequence seq = getActiveSequence();
023
024        if (seq != null)
025        {
026            final Object data = seq.getDataXY(0, 0, 0);
027
028            // have some data here ?
029            if (data != null)
030            {
031                // create simple undo point
032                if (!seq.createUndoDataPoint("Half intensity"))
033                {
034                    // failed to create undo point (not enough memory)
035                    if (!ConfirmDialog.confirm("Cannot undo operation, are you sure you want to continue ?"))
036                        return;
037                }
038
039                // convert to double
040                final double[] doubleArray = Array1DUtil.arrayToDoubleArray(data, seq.isSignedDataType());
041                // divide intensity by 2
042                ArrayMath.divide(doubleArray, 2d, doubleArray);
043                // copy data back to image
044                Array1DUtil.doubleArrayToArray(doubleArray, data);
045                // notify sequence we changed its data
046                seq.dataChanged();
047            }
048        }
049    }
050}