Initial refactoring to support CommandManager
This commit is contained in:
62
Filtration.ObjectModel/Commands/CommandManager.cs
Normal file
62
Filtration.ObjectModel/Commands/CommandManager.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Filtration.ObjectModel.Commands
|
||||
{
|
||||
public interface ICommandManager
|
||||
{
|
||||
void ExecuteCommand(ICommand command);
|
||||
void Undo(int undoLevels = 1);
|
||||
void Redo(int redoLevels = 1);
|
||||
}
|
||||
|
||||
public interface ICommandManagerInternal : ICommandManager
|
||||
{
|
||||
void SetScript(IItemFilterScriptInternal layout);
|
||||
}
|
||||
|
||||
internal class CommandManager : ICommandManagerInternal
|
||||
{
|
||||
private readonly Stack<IUndoableCommand> _undoCommandStack = new Stack<IUndoableCommand>();
|
||||
private readonly Stack<IUndoableCommand> _redoCommandStack = new Stack<IUndoableCommand>();
|
||||
private IItemFilterScriptInternal _itemFilterScript;
|
||||
|
||||
public void SetScript(IItemFilterScriptInternal itemFilterScript)
|
||||
{
|
||||
_itemFilterScript = itemFilterScript;
|
||||
}
|
||||
|
||||
public void ExecuteCommand(ICommand command)
|
||||
{
|
||||
command.Execute();
|
||||
var undoableCommand = command as IUndoableCommand;
|
||||
if (undoableCommand != null)
|
||||
{
|
||||
_undoCommandStack.Push(undoableCommand);
|
||||
_redoCommandStack.Clear();
|
||||
}
|
||||
_itemFilterScript.SetIsDirty(true);
|
||||
}
|
||||
|
||||
public void Undo(int undoLevels = 1)
|
||||
{
|
||||
for (var index = undoLevels; _undoCommandStack.Count > 0 && index > 0; --index)
|
||||
{
|
||||
var undoableCommand = _undoCommandStack.Pop();
|
||||
undoableCommand.Undo();
|
||||
_redoCommandStack.Push(undoableCommand);
|
||||
}
|
||||
_itemFilterScript.SetIsDirty(true);
|
||||
}
|
||||
|
||||
public void Redo(int redoLevels = 1)
|
||||
{
|
||||
for (int index = redoLevels; _redoCommandStack.Count > 0 && index > 0; --index)
|
||||
{
|
||||
var undoableCommand = _redoCommandStack.Pop();
|
||||
undoableCommand.Redo();
|
||||
_undoCommandStack.Push(undoableCommand);
|
||||
}
|
||||
_itemFilterScript.SetIsDirty(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Filtration.ObjectModel/Commands/ICommand.cs
Normal file
7
Filtration.ObjectModel/Commands/ICommand.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Filtration.ObjectModel.Commands
|
||||
{
|
||||
public interface ICommand
|
||||
{
|
||||
void Execute();
|
||||
}
|
||||
}
|
||||
8
Filtration.ObjectModel/Commands/IUndoableCommand.cs
Normal file
8
Filtration.ObjectModel/Commands/IUndoableCommand.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Filtration.ObjectModel.Commands
|
||||
{
|
||||
internal interface IUndoableCommand : ICommand
|
||||
{
|
||||
void Undo();
|
||||
void Redo();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user