// make Undo, i.e. revert the object to its previous state
 public bool Undo(bool storeSateteBeforOperation = false)
 {
     if (undoList.Count > 0)
     {
         // get the object state from the undo list
         ObjectPropertyValuePair undoObject = undoList[0];
         // suspend the object from notification
         // otherwise we will be notified for an action which we perform and should be inserted in any list
         undoObject.O.SuspendListener = true;
         // revert to the state
         var prevState = undoObject.SetState();
         // after we execute the operation we won to resume the notifications
         undoObject.O.SuspendListener = false;
         // remove this state from undo list
         undoList.Remove(undoObject);
         // IF WE WANT TO SAVE CURRENT STATE
         if (storeSateteBeforOperation)
         {
             this.AddToRedo(prevState);
         }
         // add this state to the redo list
         this.AddToRedo(undoObject);
         return(true);
     }
     return(false);
 }
        // this actually reverts to the state
        /// <summary>
        ///
        /// </summary>
        /// <returns>returns the state before revert</returns>
        public ObjectPropertyValuePair SetState()
        {
            ObjectPropertyValuePair prevState = this.GetStateFor(this);

            // we get the meta data about the property based on the object type
            // then get the corresponding PropertyDescriptor, which allows us to call its set method
            objectsProperties[this.o.GetType()][this.property].SetValue(this.o, this.oldeValue);

            return(prevState);
        }
 private void AddToList(ObjectPropertyValuePair item, List <ObjectPropertyValuePair> list)
 {
     // we want to keep the capacity of the list to 30 items
     // but since this list should behave as stack we should remove the items from the bottom
     if (list.Count >= capacity)
     {
         for (int i = capacity - 1; i < list.Count; i++)
         {
             list.RemoveAt(i);
         }
     }
     list.Insert(0, item);
 }
 public bool Redo(bool storeSateteBeforOperation = false)
 {
     if (redoList.Count > 0)
     {
         ObjectPropertyValuePair redoObject = redoList[0];
         redoObject.O.SuspendListener = true;
         var prevState = redoObject.SetState();
         redoObject.O.SuspendListener = false;
         redoList.Remove(redoObject);
         // IF WE WANT TO SAVE CURRENT STATE
         if (storeSateteBeforOperation)
         {
             this.AddToUndo(prevState);
         }
         this.AddToUndo(redoObject);
         return(true);
     }
     return(false);
 }
        private void RemoveObjectFromUndoReduManager(object sender)
        {
            // unsubscribe from the events
            INotify followedObject = sender as INotify;

            followedObject.OnNotifyPropertyChanging -= FollowedObject_NotifyPropertyChanging;
            followedObject.OnDispose -= FollowedObject_OnDispose;
            // remove the object from all lists
            objects.Remove(sender);
            // here we use LINQ query to find the object we need
            ObjectPropertyValuePair followedObjectWrapper = this.undoList.FirstOrDefault(o => o.O.Equals(sender));

            if (followedObjectWrapper != null)
            {
                this.undoList.Remove(followedObjectWrapper);
            }

            // here we use LINQ query to find the object we need
            followedObjectWrapper = this.redoList.FirstOrDefault(o => o.O.Equals(sender));
            if (followedObjectWrapper != null)
            {
                this.redoList.Remove(followedObjectWrapper);
            }
        }
 private ObjectPropertyValuePair GetStateFor(ObjectPropertyValuePair state)
 {
     return(new ObjectPropertyValuePair(ref state.o, state.property));
 }
 // add item to the redo list
 private void AddToRedo(ObjectPropertyValuePair item)
 {
     AddToList(item, redoList);
 }
 private ObjectPropertyValuePair GetStateFor(ObjectPropertyValuePair state)
 {
     return new ObjectPropertyValuePair(ref state.o, state.property);
 }
 // add item to the Undo list
 private void AddToUndo(ObjectPropertyValuePair item)
 {
     AddToList(item, undoList);
 }
 private void AddToList(ObjectPropertyValuePair item, List<ObjectPropertyValuePair> list)
 {
     // we want to keep the capacity of the list to 30 items
     // but since this list should behave as stack we should remove the items from the bottom
     if (list.Count >= capacity)
     {
         for (int i = capacity-1; i < list.Count; i++)
         {
             list.RemoveAt(i);
         }
     }
     list.Insert(0, item);
 }