Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CollectionChangedActionItem.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Collections.Specialized;
7 using System.Linq;
8 
9 namespace SiliconStudio.ActionStack
10 {
12  {
13  private readonly int index;
14  private IList list;
15  private IReadOnlyCollection<object> items;
16  private NotifyCollectionChangedAction actionToUndo;
17 
18  private CollectionChangedActionItem(IList list, NotifyCollectionChangedAction actionToUndo)
19  {
20  if (list == null) throw new ArgumentNullException("list");
21  if (actionToUndo == NotifyCollectionChangedAction.Reset) throw new ArgumentException("Reset is not supported by the undo stack.");
22  this.list = list;
23  this.actionToUndo = actionToUndo;
24  }
25 
26  public CollectionChangedActionItem(IList list, NotifyCollectionChangedAction actionToUndo, IReadOnlyCollection<object> items, int index)
27  : this(list, actionToUndo)
28  {
29  if (items == null) throw new ArgumentNullException("items");
30  this.items = items;
31  this.index = index;
32  }
33 
34  public CollectionChangedActionItem(IList list, NotifyCollectionChangedEventArgs args)
35  : this(list, args.Action)
36  {
37  switch (args.Action)
38  {
39  case NotifyCollectionChangedAction.Add:
40  items = args.NewItems.Cast<object>().ToArray();
41  index = args.NewStartingIndex;
42  break;
43  case NotifyCollectionChangedAction.Move:
44  // Intentionally ignored, move in collection are not tracked
45  return;
46  case NotifyCollectionChangedAction.Remove:
47  items = args.OldItems.Cast<object>().ToArray();
48  index = args.OldStartingIndex;
49  break;
50  case NotifyCollectionChangedAction.Replace:
51  items = args.OldItems.Cast<object>().ToArray();
52  index = args.OldStartingIndex;
53  break;
54  case NotifyCollectionChangedAction.Reset:
55  throw new NotSupportedException("Reset is not supported by the undo stack.");
56  default:
57  items = new object[] { };
58  index = -1;
59  break;
60  }
61  }
62 
63  public NotifyCollectionChangedAction ActionToUndo { get { return actionToUndo; } }
64 
65  public int ItemCount { get { return items.Count; } }
66 
67  /// <inheritdoc/>
68  protected override void FreezeMembers()
69  {
70  list = null;
71  items = null;
72  }
73 
74  /// <inheritdoc/>
75  protected override void UndoAction()
76  {
77  switch (actionToUndo)
78  {
79  case NotifyCollectionChangedAction.Add:
80  actionToUndo = NotifyCollectionChangedAction.Remove;
81  for (int i = 0; i < items.Count(); ++i)
82  {
83  list.RemoveAt(index);
84  }
85  break;
86  case NotifyCollectionChangedAction.Remove:
87  actionToUndo = NotifyCollectionChangedAction.Add;
88  foreach (var item in items)
89  {
90  list.Insert(index, item);
91  }
92  break;
93  case NotifyCollectionChangedAction.Replace:
94  var replacedItems = new List<object>();
95  int j = 0;
96  foreach (var item in items)
97  {
98  replacedItems.Add(list[index+j]);
99  list[index + j] = item;
100  ++j;
101  }
102  items = replacedItems;
103  break;
104  case NotifyCollectionChangedAction.Move:
105  throw new NotSupportedException("Move is not supported by the undo stack.");
106  case NotifyCollectionChangedAction.Reset:
107  throw new NotSupportedException("Reset is not supported by the undo stack.");
108  }
109  }
110 
111  /// <inheritdoc/>
112  protected override void RedoAction()
113  {
114  // Once we have un-done, the previous value is updated so Redo is just Undoing the Undo
115  UndoAction();
116  }
117  }
118 }
override void FreezeMembers()
Invoked by Freeze before setting IsFrozen to true. This method will not be invoked if IsFrozen is alr...
override void UndoAction()
Invoked by Undo after setting IsDone to true.
Base class for action items.
Definition: ActionItem.cs:13
CollectionChangedActionItem(IList list, NotifyCollectionChangedEventArgs args)
CollectionChangedActionItem(IList list, NotifyCollectionChangedAction actionToUndo, IReadOnlyCollection< object > items, int index)
override void RedoAction()
Invoked by Redo after setting IsDone to true.