Android Open Source - Sketcher-Tab Document History






From Project

Back to project page Sketcher-Tab.

License

The source code is released under:

Apache License

If you think the Android project Sketcher-Tab 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 org.sketchertab;
//from  w w w.  j a  va 2 s.  c  o  m
import java.util.ArrayList;
import java.util.List;

/**
 * Paint history. Undo system inspired by Pinta graphical editor.
 * todo: Keep undo level under memory footprint but at least 12 step.
 */
public class DocumentHistory {
    private static final DocumentHistory INSTANCE = new DocumentHistory();
    private static final int UNDO_LIMIT = 12;

    private List<HistoryItem> historyList = new ArrayList<HistoryItem>();
    private int historyPointer = -1;
    private boolean canUndo = false;
    private boolean canRedo = false;

    private DocumentHistory() {
    }

    public static DocumentHistory getInstance() {
        return INSTANCE;
    }

    //    Memory limit for undo operations about 40 MB, Acer Iconia A500
    public void pushNewItem(HistoryItem historyItem) {
        //Remove all old redos starting from the end of the list
        for (int i = historyList.size() - 1; i >= 0; i--) {
            HistoryItem item = historyList.get(i);
            if (item.getState() == HistoryItem.HistoryItemState.REDO) {
                historyList.remove(i);
            } else if (item.getState() == HistoryItem.HistoryItemState.UNDO)
                break;
        }
        if (UNDO_LIMIT == historyList.size()) {
            historyList.remove(0);
        }
        historyList.add(historyItem);
        historyPointer = historyList.size() - 1;

        if (historyList.size() > 0)
            canUndo = true;

        canRedo = false;
    }

    public void undo() {
        if (historyPointer < 0)
            return;

        HistoryItem historyItem = historyList.get(historyPointer);
        historyItem.undo();
        historyItem.setState(HistoryItem.HistoryItemState.REDO);
        historyPointer -= 1;

        if (historyPointer < 0)
            canUndo = false;

        canRedo = true;
    }

    public void redo() {
        if (historyPointer >= historyList.size() - 1)
            return;

        historyPointer += 1;
        HistoryItem historyItem = historyList.get(historyPointer);
        historyItem.redo();
        historyItem.setState(HistoryItem.HistoryItemState.UNDO);

        if (historyPointer == historyList.size() - 1)
            canRedo = false;

        if (historyList.size() > 1)
            canUndo = true;
    }

    public void clear() {
        historyList.clear();
        historyPointer = -1;
        canRedo = false;
        canUndo = false;
    }

    public boolean canUndo() {
        return canUndo;
    }

    public boolean canRedo() {
        return canRedo;
    }
}




Java Source Code List

org.sketchertab.AboutDialog.java
org.sketchertab.BrushProperties.java
org.sketchertab.DocumentHistory.java
org.sketchertab.DrawController.java
org.sketchertab.FileHelper.java
org.sketchertab.HistoryItem.java
org.sketchertab.Sketcher.java
org.sketchertab.Style.java
org.sketchertab.SurfaceDiff.java
org.sketchertab.Surface.java
org.sketchertab.colorpicker.HuePicker.java
org.sketchertab.colorpicker.PickerDialog.java
org.sketchertab.colorpicker.Picker.java
org.sketchertab.colorpicker.PreviewView.java
org.sketchertab.colorpicker.SatValPicker.java
org.sketchertab.colorpicker.Utils.java
org.sketchertab.style.CirclesStyle.java
org.sketchertab.style.FurStyle.java
org.sketchertab.style.RibbonStyle.java
org.sketchertab.style.ShadedStyle.java
org.sketchertab.style.SimpleStyle.java
org.sketchertab.style.SketchyStyle.java
org.sketchertab.style.StyleBrush.java
org.sketchertab.style.StylesFactory.java
org.sketchertab.style.WebStyle.java