#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Data {
[StorableClass]
public class CommandChain : IEnumerable {
private LinkedList commands;
private LinkedListNode lastExecutedCommand;
private LinkedListNode startMacroRecordingCommand;
private bool isMacroRecording;
[Storable]
private List CommandStorable {
get {
LinkedListNode cmdNode = null;
if (this.isMacroRecording && startMacroRecordingCommand != null)
cmdNode = startMacroRecordingCommand.Next;
else
cmdNode = commands.First;
List list = new List();
while (cmdNode != lastExecutedCommand.Next) {
list.Add(cmdNode.Value);
cmdNode = cmdNode.Next;
}
return list;
}
set {
foreach (ICommand command in value)
commands.AddLast(command);
this.lastExecutedCommand = commands.Last;
this.FireChanged();
}
}
[StorableConstructor]
protected CommandChain(bool deserializing)
: base() {
this.commands = new LinkedList();
}
public CommandChain() {
this.commands = new LinkedList();
}
public ICommand LastExecutedCommand {
get { return lastExecutedCommand == null ? null : lastExecutedCommand.Value; }
}
public int CommandsCount {
get { return this.commands.Count; }
}
public bool FirstCommandReached {
get { return this.commands.Count == 0 || this.lastExecutedCommand == null; }
}
public bool LastCommandReached {
get { return this.commands.Count == 0 || this.lastExecutedCommand == this.commands.Last; }
}
public bool IsMacroRecording {
get { return isMacroRecording; }
}
public string NextCommandDescription {
get {
if (LastCommandReached)
return "Redo";
else if (lastExecutedCommand == null)
return commands.First.Value.Description;
else
return lastExecutedCommand.Next.Value.Description;
}
}
public string PreviousCommandDescription {
get { return FirstCommandReached ? "Undo" : lastExecutedCommand.Value.Description; }
}
public event EventHandler Changed;
protected virtual void OnChanged() {
if (this.Changed != null)
this.Changed(this, EventArgs.Empty);
}
public void FireChanged() {
this.OnChanged();
}
public void Add(ICommand command) {
this.Add(command, true);
}
public void Add(ICommand command, bool fireChangedEvent) {
try {
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
command.Execute();
while (this.commands.Count != 0 && this.commands.Last != this.lastExecutedCommand)
this.commands.RemoveLast();
this.commands.AddLast(command);
this.lastExecutedCommand = this.commands.Last;
GC.Collect();
if (fireChangedEvent)
this.OnChanged();
}
finally {
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
}
}
public void Clear() {
this.commands.Clear();
this.OnChanged();
}
public void Redo() {
if (this.lastExecutedCommand != this.commands.Last) {
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
if (this.lastExecutedCommand == null)
this.lastExecutedCommand = this.commands.First;
else
this.lastExecutedCommand = this.lastExecutedCommand.Next;
try {
this.lastExecutedCommand.Value.Execute();
this.OnChanged();
}
catch (CommandExecutionException) {
this.lastExecutedCommand = this.lastExecutedCommand.Previous;
throw;
}
finally {
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
}
}
}
public void Undo() {
if (this.lastExecutedCommand != null) {
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
try {
this.lastExecutedCommand.Value.UndoExecute();
if (this.lastExecutedCommand == this.startMacroRecordingCommand)
isMacroRecording = false;
this.lastExecutedCommand = this.lastExecutedCommand.Previous;
this.OnChanged();
}
finally {
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
}
}
}
public void ToggleMacroRecording() {
if (!isMacroRecording)
this.startMacroRecordingCommand = lastExecutedCommand;
isMacroRecording = !isMacroRecording;
this.OnChanged();
}
#region IEnumerable Members
public IEnumerator GetEnumerator() {
return commands.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return commands.GetEnumerator();
}
#endregion
}
}