Android Open Source - pixel-art Undo Manager






From Project

Back to project page pixel-art.

License

The source code is released under:

Apache License

If you think the Android project pixel-art listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jaween.pixelart.ui.undo;
//  w  w  w .  j  ava  2 s .  co m
import java.util.LinkedList;

/**
 * Manages the undo and redo stacks.
 *
 * Used by first constructing an instance with a max undo stack size. When the user performs an
 * undoable operation, an instance of UndoItem must be created that contains both the type of
 * operation and the data needed to undo the operation. This is passed the function pushUndoItem().
 * When performing the undo, call the function popUndoItem() which will first push the UndoItem to
 * the redo stack and also return it to the caller. The details of unrolling an action is left up to
 * the receiver. To redo an action, call popRedoItem() to have it pushed to the UndoStack and have
 * it returned.

 */
public class UndoManager {

    private LinkedList<UndoItem> undoItems = new LinkedList<UndoItem>();
    private LinkedList<UndoItem> redoItems = new LinkedList<UndoItem>();

    private int maxUndos;

    public UndoManager(int maxUndos) {
        this.maxUndos = maxUndos;
    }

    /**
     * Pushes an item onto the undo stack. If there isn't enough space, removes the first/oldest item
     * on the stack
     * @param undoItem The item to be stacked
     */
    public void pushUndoItem(UndoItem undoItem) {
        // Loses any future commands that were in the redo stack
        if (redoItems.size() > 0) {
            redoItems.clear();
        }

        // Maintains the maximum number of undos (and hence the maximum number of redos)
        undoItems.push(undoItem);
        if (undoItems.size() > maxUndos) {
            undoItems.pollLast();
        }
    }

    /**
     * Pops the top of the undo stack and pushes it to the redo stack
     * @return The UndoItem on the top of the stack or null if there was no such element
     */
    public UndoItem popUndoItem() {
        UndoItem undoItem = undoItems.pollFirst();

        // Pushes item onto redo stack
        if (undoItem != null) {
            redoItems.push(undoItem);
        }
        return undoItem;
    }

    /**
     * Pops the top of the redo stack and pushes it to the undo stack
     * @return The UndoItem on the top of the stack or null if there was no such element
     */
    public UndoItem popRedoItem() {
        UndoItem redoItem = redoItems.pollFirst();

        // Pushes item onto undo stack
        if (redoItem != null) {
            undoItems.push(redoItem);
        }
        return redoItem;
    }

    /**
     * Clears the both the undo and redo stacks, a fresh start.
     */
    public void clear() {
        undoItems.clear();
        redoItems.clear();
    }
}




Java Source Code List

com.jaween.pixelart.ContainerActivity.java
com.jaween.pixelart.ContainerFragment.java
com.jaween.pixelart.PanelManagerFragment.java
com.jaween.pixelart.io.AnimationFile.java
com.jaween.pixelart.io.FileAdapter.java
com.jaween.pixelart.io.ImportExport.java
com.jaween.pixelart.io.LoadFileDialog.java
com.jaween.pixelart.tools.Command.java
com.jaween.pixelart.tools.Dropper.java
com.jaween.pixelart.tools.Eraser.java
com.jaween.pixelart.tools.FloodFill.java
com.jaween.pixelart.tools.FreeSelect.java
com.jaween.pixelart.tools.MagicWand.java
com.jaween.pixelart.tools.Oval.java
com.jaween.pixelart.tools.Pen.java
com.jaween.pixelart.tools.RectSelect.java
com.jaween.pixelart.tools.Rect.java
com.jaween.pixelart.tools.Selection.java
com.jaween.pixelart.tools.ToolReport.java
com.jaween.pixelart.tools.Tool.java
com.jaween.pixelart.tools.attributes.EraserToolAttributes.java
com.jaween.pixelart.tools.attributes.MagicWandToolAttributes.java
com.jaween.pixelart.tools.attributes.OvalToolAttributes.java
com.jaween.pixelart.tools.attributes.PenToolAttributes.java
com.jaween.pixelart.tools.attributes.RectToolAttributes.java
com.jaween.pixelart.tools.attributes.ToolAttributes.java
com.jaween.pixelart.tools.options.EraserOptionsView.java
com.jaween.pixelart.tools.options.MagicWandOptionsView.java
com.jaween.pixelart.tools.options.OvalOptionsView.java
com.jaween.pixelart.tools.options.PenOptionsView.java
com.jaween.pixelart.tools.options.RectOptionsView.java
com.jaween.pixelart.tools.options.ToolOptionsView.java
com.jaween.pixelart.ui.ColourButton.java
com.jaween.pixelart.ui.ColourSelector.java
com.jaween.pixelart.ui.DrawingFragment.java
com.jaween.pixelart.ui.DrawingSurface.java
com.jaween.pixelart.ui.PaletteFragment.java
com.jaween.pixelart.ui.PanelFragment.java
com.jaween.pixelart.ui.PixelGrid.java
com.jaween.pixelart.ui.Thumbnail.java
com.jaween.pixelart.ui.ToolButton.java
com.jaween.pixelart.ui.ToolboxFragment.java
com.jaween.pixelart.ui.TransparencyCheckerboard.java
com.jaween.pixelart.ui.animation.AnimationFragment.java
com.jaween.pixelart.ui.animation.FrameAdapter.java
com.jaween.pixelart.ui.animation.Frame.java
com.jaween.pixelart.ui.colourpicker.ColourPickerDialog.java
com.jaween.pixelart.ui.colourpicker.ColourPickerFragment.java
com.jaween.pixelart.ui.colourpicker.ColourPickerView.java
com.jaween.pixelart.ui.layer.LayerAdapter.java
com.jaween.pixelart.ui.layer.LayerFragment.java
com.jaween.pixelart.ui.layer.Layer.java
com.jaween.pixelart.ui.undo.DrawOpManager.java
com.jaween.pixelart.ui.undo.DrawOpUndoData.java
com.jaween.pixelart.ui.undo.FrameUndoData.java
com.jaween.pixelart.ui.undo.LayerUndoData.java
com.jaween.pixelart.ui.undo.UndoItem.java
com.jaween.pixelart.ui.undo.UndoManager.java
com.jaween.pixelart.util.AbsVerticalSeekBar.java
com.jaween.pixelart.util.AnimatedGifEncoder.java
com.jaween.pixelart.util.AutoSaver.java
com.jaween.pixelart.util.BitmapEncoder.java
com.jaween.pixelart.util.Color.java
com.jaween.pixelart.util.ConfigChangeFragment.java
com.jaween.pixelart.util.Debug.java
com.jaween.pixelart.util.MarchingAnts.java
com.jaween.pixelart.util.PreferenceManager.java
com.jaween.pixelart.util.ScaleListener.java
com.jaween.pixelart.util.SlideAnimator.java
com.jaween.pixelart.util.SlidingLinearLayout.java
com.jaween.pixelart.util.VerticalProgressBar.java
com.tokaracamara.android.verticalslidevar.VerticalSeekBar.java