Files
Pilz/Pilz.Collections/SimpleHistory/SimpleHistory.cs
2020-09-24 11:21:53 +02:00

104 lines
2.6 KiB
C#

using System.Collections.Generic;
using Microsoft.VisualBasic.CompilerServices;
namespace Pilz.Collections.SimpleHistory
{
public class HistoryStack
{
private Stack<HistoryPoint> stackPast = new Stack<HistoryPoint>();
private Stack<HistoryPoint> stackFuture = new Stack<HistoryPoint>();
/// <summary>
/// Gets the count of history points.
/// </summary>
/// <returns></returns>
public bool ChangesCount
{
get
{
return Conversions.ToBoolean(stackPast.Count);
}
}
/// <summary>
/// Checks if the History has past changes.
/// </summary>
/// <returns></returns>
public bool HasChanges()
{
return stackPast.Count > 0;
}
/// <summary>
/// Patch Object States and call Undo Actions.
/// </summary>
public HistoryPoint Undo()
{
HistoryPoint ret;
if (stackPast.Count > 0)
{
var hp = stackPast.Pop();
hp.Undo();
stackFuture.Push(hp);
ret = hp;
}
else
{
ret = null;
}
return ret;
}
/// <summary>
/// Patch Object States and call Redo Actions.
/// </summary>
public HistoryPoint Redo()
{
HistoryPoint ret;
if (stackFuture.Count > 0)
{
var hp = stackFuture.Pop();
hp.Redo();
stackPast.Push(hp);
ret = hp;
}
else
{
ret = null;
}
return ret;
}
/// <summary>
/// Clear the History.
/// </summary>
public void Clear()
{
stackPast.Clear();
stackFuture.Clear();
}
/// <summary>
/// Store a History Point.
/// </summary>
/// <param name="point">The History Point to add to the past changes.</param>
/// <param name="newName">The name to set for the History Point.</param>
public void Store(HistoryPoint point, string newName)
{
point.Name = newName;
Store(point);
}
/// <summary>
/// Store a History Point.
/// </summary>
/// <param name="point">The History Point to add to the past changes.</param>
public void Store(HistoryPoint point)
{
stackPast.Push(point);
stackFuture.Clear();
}
}
}