001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.commonplugin.undo.undo;
005    /*
006    author:azin azadi
007    
008    */
009    
010    import graphlab.platform.core.AbstractAction;
011    import graphlab.platform.core.BlackBoard;
012    import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData;
013    
014    public class UndoLogManager extends AbstractAction {
015        public static final String EVENT_KEY = "Undo Log Manager";
016    
017        public UndoLogManager(BlackBoard bb) {
018            super(bb);
019            listen4Event(UndoableActionOccuredData.EVENT_KEY);
020            enable();
021            current = lastNode;
022        }
023    
024        Node current;
025        Node lastNode = new Node();
026    
027        /**
028         * Occurs when the undo log adds by an action
029         *
030         * @param eventName
031         * @param value
032         */
033        public void performAction(String eventName, Object value) {
034            UndoableActionOccuredData uaod = blackboard.getData(UndoableActionOccuredData.EVENT_KEY);
035            Node first = new Node(); //we can delet this line
036            first.val = uaod;
037            first.setNext(current);
038            this.current = first;
039        }
040    
041        /**
042         * returns the data for the next undo action
043         */
044        public UndoableActionOccuredData getNextUndoData() {
045            if (current == lastNode)
046                return null;    //no action to undo
047            UndoableActionOccuredData val = current.val;
048            current = current.next;
049            return val;
050        }
051    
052        /**
053         * returns the data for the next undo action
054         */
055        public UndoableActionOccuredData getNextRedoData() {
056            if (current.prev == null)
057                return null;    //no action to undo
058            current = current.prev;
059            return current.val;
060        }
061    
062    }
063    
064    class Node {
065        Node next, prev;
066        UndoableActionOccuredData val;
067    
068        /**
069         * sets the next node linked to this node and also updates the previous node of the next to this
070         */
071        public void setNext(Node next) {
072            this.next = next;
073            if (next != null)
074                next.prev = this;
075        }
076    }