PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/release 0.1.1/Designer/Undo data/ClusterUndoItem.cs

https://gitlab.com/Tiger66639/neural_network_designer
C# | 275 lines | 202 code | 27 blank | 46 comment | 34 complexity | 408207b0b6498b95159a19600a5614cd MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NeuralNetworkDesigne.UndoSystem;
  6. using System.Collections.Specialized;
  7. namespace NeuralNetworkDesigne.HAB.Designer
  8. {
  9. /// <summary>
  10. /// A custom undo item that stores changes in a <see cref="NeuronCluster"/>. This is done seperatly, outside the undo event system
  11. /// because this uses object references to wrappers, while we need neuron references because the wrappers for the neurons get
  12. /// created and destroyed.
  13. /// </summary>
  14. public abstract class ClusterUndoItem : UndoItem
  15. {
  16. public NotifyCollectionChangedAction Action { get; internal set; }
  17. public NeuronCluster Cluster { get; set; }
  18. /// <summary>
  19. /// Checks if this undo item contains data from the specified source object.
  20. /// </summary>
  21. /// <param name="source">The object to check for.</param>
  22. /// <returns>
  23. /// True if this object contains undo data for the specified object.
  24. /// </returns>
  25. /// <remarks>
  26. /// This function is used by the undo system to clean up undo data from items that are no longer available. This can happen for
  27. /// undo data generated from user interface controls which are no longer valid (loaded).
  28. /// </remarks>
  29. public override bool StoresDataFrom(object source)
  30. {
  31. return Cluster == source;
  32. }
  33. public override bool HasSameTarget(UndoItem toCompare)
  34. {
  35. ClusterUndoItem iToCompare = toCompare as ClusterUndoItem;
  36. if (iToCompare != null)
  37. return iToCompare.Cluster == Cluster && iToCompare.Action == Action;
  38. else
  39. return false;
  40. }
  41. }
  42. public abstract class ClusterItemsUndoItem : ClusterUndoItem
  43. {
  44. /// <summary>
  45. /// Gets or sets the items that were removed during the reset.
  46. /// </summary>
  47. /// <value>The items.</value>
  48. public IList<Neuron> Items { get; set; }
  49. public override bool HasSameTarget(UndoItem toCompare)
  50. {
  51. bool iRes = base.HasSameTarget(toCompare);
  52. if (iRes == true)
  53. {
  54. ClusterItemsUndoItem iItem = toCompare as ClusterItemsUndoItem;
  55. if (iItem != null && iItem.Items.Count == Items.Count)
  56. {
  57. foreach (Neuron i in iItem.Items)
  58. if (Items.Contains(i) == false)
  59. return false;
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. }
  66. public class ResetClusterUndoItem : ClusterItemsUndoItem
  67. {
  68. public ResetClusterUndoItem()
  69. {
  70. Action = NotifyCollectionChangedAction.Reset;
  71. }
  72. public override void Execute(UndoStore caller)
  73. {
  74. AddClusterUndoItem iUndo = new AddClusterUndoItem();
  75. iUndo.Cluster = Cluster;
  76. iUndo.Items = Items;
  77. iUndo.FromReset = true;
  78. WindowMain.UndoStore.AddCustomUndoItem(iUndo);
  79. using (ChildrenAccessor iChildren = Cluster.ChildrenW)
  80. {
  81. foreach (Neuron i in Items)
  82. iChildren.Add(i);
  83. }
  84. }
  85. }
  86. public class AddClusterUndoItem : ClusterItemsUndoItem
  87. {
  88. public AddClusterUndoItem()
  89. {
  90. FromReset = false;
  91. Action = NotifyCollectionChangedAction.Add;
  92. }
  93. /// <summary>
  94. /// Gets or sets a value indicating whether the undo came from a reversed reset, in which case we can do a reset again.
  95. /// False by default.
  96. /// </summary>
  97. /// <value><c>true</c> if [from reset]; otherwise, <c>false</c>.</value>
  98. public bool FromReset { get; set; }
  99. public override void Execute(UndoStore caller)
  100. {
  101. if (FromReset == true)
  102. DoReset(caller);
  103. else
  104. DoRemove(caller);
  105. }
  106. private void DoRemove(UndoStore caller)
  107. {
  108. RemoveClusterUndoItem iUndo = new RemoveClusterUndoItem();
  109. iUndo.Cluster = Cluster;
  110. iUndo.Items = Items;
  111. WindowMain.UndoStore.AddCustomUndoItem(iUndo);
  112. using (ChildrenAccessor iChildren = Cluster.ChildrenW)
  113. {
  114. iUndo.Index = iChildren.IndexOf(Items[0].ID); //when the action gets reversed, we need to know the index of the first object (usually there is only 1 item, except during a reset).
  115. if (iUndo.Index != -1)
  116. {
  117. foreach (Neuron i in Items)
  118. iChildren.Remove(i);
  119. }
  120. else
  121. throw new InvalidOperationException("Item not found in cluster, can't execute the undo item.");
  122. }
  123. }
  124. private void DoReset(UndoStore caller)
  125. {
  126. ResetClusterUndoItem iUndo = new ResetClusterUndoItem();
  127. iUndo.Cluster = Cluster;
  128. iUndo.Items = Items;
  129. WindowMain.UndoStore.AddCustomUndoItem(iUndo);
  130. using (ChildrenAccessor iChildren = Cluster.ChildrenW)
  131. iChildren.Clear();
  132. }
  133. }
  134. public class RemoveClusterUndoItem : ClusterItemsUndoItem
  135. {
  136. /// <summary>
  137. /// Gets or sets the index at which the first item was located when the remove happened. -1 indicates to do
  138. /// an add instead of an insert when reversing the action.
  139. /// </summary>
  140. /// <value>The index.</value>
  141. public int Index { get; set; }
  142. public RemoveClusterUndoItem()
  143. {
  144. Action = NotifyCollectionChangedAction.Remove;
  145. }
  146. public override void Execute(UndoStore caller)
  147. {
  148. AddClusterUndoItem iUndo = new AddClusterUndoItem();
  149. iUndo.Cluster = Cluster;
  150. iUndo.Items = Items;
  151. WindowMain.UndoStore.AddCustomUndoItem(iUndo);
  152. using (ChildrenAccessor iChildren = Cluster.ChildrenW)
  153. {
  154. if (Index == -1)
  155. {
  156. foreach (Neuron i in Items)
  157. iChildren.Add(i);
  158. }
  159. else
  160. {
  161. foreach (Neuron i in Items)
  162. iChildren.Insert(Index++, i);
  163. }
  164. }
  165. }
  166. }
  167. public class MoveClusterUndoItem : ClusterUndoItem
  168. {
  169. /// <summary>
  170. /// Gets or sets the item that was moved.
  171. /// </summary>
  172. /// <value>The item.</value>
  173. public Neuron Item { get; set; }
  174. /// <summary>
  175. /// Gets or sets the index that the item had.
  176. /// </summary>
  177. /// <value>The index.</value>
  178. public int Index { get; set; }
  179. public MoveClusterUndoItem()
  180. {
  181. Action = NotifyCollectionChangedAction.Move;
  182. }
  183. public override void Execute(UndoStore caller)
  184. {
  185. MoveClusterUndoItem iUndo = new MoveClusterUndoItem();
  186. iUndo.Cluster = Cluster;
  187. iUndo.Item = Item;
  188. WindowMain.UndoStore.AddCustomUndoItem(iUndo);
  189. using (ChildrenAccessor iChildren = Cluster.ChildrenW)
  190. {
  191. int iIndex = iChildren.IndexOf(Item.ID);
  192. iUndo.Index = iIndex;
  193. iChildren.RemoveAt(iIndex);
  194. iChildren.Insert(Index, Item);
  195. }
  196. }
  197. public override bool HasSameTarget(UndoItem toCompare)
  198. {
  199. if (base.HasSameTarget(toCompare) == true)
  200. {
  201. MoveClusterUndoItem iToCompare = toCompare as MoveClusterUndoItem;
  202. if (iToCompare != null)
  203. return iToCompare.Index == Index && iToCompare.Item == Item;
  204. }
  205. return false;
  206. }
  207. }
  208. public class ReplaceClusterUndoItem: ClusterUndoItem
  209. {
  210. public ReplaceClusterUndoItem()
  211. {
  212. Action = NotifyCollectionChangedAction.Replace;
  213. }
  214. /// <summary>
  215. /// Gets or sets the item that was replaced.
  216. /// </summary>
  217. /// <value>The item.</value>
  218. public Neuron Item { get; set; }
  219. /// <summary>
  220. /// Gets or sets the index that the item had.
  221. /// </summary>
  222. /// <value>The index.</value>
  223. public int Index { get; set; }
  224. public override void Execute(UndoStore caller)
  225. {
  226. ReplaceClusterUndoItem iUndo = new ReplaceClusterUndoItem();
  227. iUndo.Cluster = Cluster;
  228. iUndo.Index = Index;
  229. WindowMain.UndoStore.AddCustomUndoItem(iUndo);
  230. using (ChildrenAccessor iChildren = Cluster.ChildrenW)
  231. {
  232. iUndo.Item = Brain.Current[iChildren[Index]];
  233. iChildren.RemoveAt(Index);
  234. iChildren.Insert(Index, Item);
  235. }
  236. }
  237. public override bool HasSameTarget(UndoItem toCompare)
  238. {
  239. if (base.HasSameTarget(toCompare) == true)
  240. {
  241. MoveClusterUndoItem iToCompare = toCompare as MoveClusterUndoItem;
  242. if (iToCompare != null)
  243. return iToCompare.Index == Index && iToCompare.Item == Item;
  244. }
  245. return false;
  246. }
  247. }
  248. }