[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Xml;
|
---|
| 27 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.DataImporter.Data {
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public class CommandChain : IEnumerable<ICommand> {
|
---|
| 34 | private LinkedList<ICommand> commands;
|
---|
| 35 | private LinkedListNode<ICommand> lastExecutedCommand;
|
---|
| 36 | private LinkedListNode<ICommand> startMacroRecordingCommand;
|
---|
| 37 | private bool isMacroRecording;
|
---|
| 38 |
|
---|
| 39 | [Storable]
|
---|
| 40 | private List<ICommand> CommandStorable {
|
---|
| 41 | get {
|
---|
| 42 | LinkedListNode<ICommand> cmdNode = null;
|
---|
| 43 | if (this.isMacroRecording && startMacroRecordingCommand != null)
|
---|
| 44 | cmdNode = startMacroRecordingCommand.Next;
|
---|
| 45 | else
|
---|
| 46 | cmdNode = commands.First;
|
---|
| 47 |
|
---|
| 48 | List<ICommand> list = new List<ICommand>();
|
---|
| 49 | while (cmdNode != lastExecutedCommand.Next) {
|
---|
| 50 | list.Add(cmdNode.Value);
|
---|
| 51 | cmdNode = cmdNode.Next;
|
---|
| 52 | }
|
---|
| 53 | return list;
|
---|
| 54 | }
|
---|
| 55 | set {
|
---|
| 56 | foreach (ICommand command in value)
|
---|
| 57 | commands.AddLast(command);
|
---|
| 58 | this.lastExecutedCommand = commands.Last;
|
---|
| 59 | this.FireChanged();
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public CommandChain() {
|
---|
| 64 | this.commands = new LinkedList<ICommand>();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public ICommand LastExecutedCommand {
|
---|
| 68 | get { return lastExecutedCommand == null ? null : lastExecutedCommand.Value; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public int CommandsCount {
|
---|
| 72 | get { return this.commands.Count; }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public bool FirstCommandReached {
|
---|
| 76 | get { return this.commands.Count == 0 || this.lastExecutedCommand == null; }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public bool LastCommandReached {
|
---|
| 80 | get { return this.commands.Count == 0 || this.lastExecutedCommand == this.commands.Last; }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public bool IsMacroRecording {
|
---|
| 84 | get { return isMacroRecording; }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public string NextCommandDescription {
|
---|
| 88 | get {
|
---|
| 89 | if (LastCommandReached)
|
---|
| 90 | return "Redo";
|
---|
| 91 | else if (lastExecutedCommand == null)
|
---|
| 92 | return commands.First.Value.Description;
|
---|
| 93 | else
|
---|
| 94 | return lastExecutedCommand.Next.Value.Description;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public string PreviousCommandDescription {
|
---|
| 99 | get { return FirstCommandReached ? "Undo" : lastExecutedCommand.Value.Description; }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public event EventHandler Changed;
|
---|
| 103 | protected virtual void OnChanged() {
|
---|
| 104 | if (this.Changed != null)
|
---|
| 105 | this.Changed(this, EventArgs.Empty);
|
---|
| 106 | }
|
---|
| 107 | public void FireChanged() {
|
---|
| 108 | this.OnChanged();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public void Add(ICommand command) {
|
---|
| 112 | this.Add(command, true);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public void Add(ICommand command, bool fireChangedEvent) {
|
---|
| 116 | try {
|
---|
| 117 | System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
|
---|
| 118 | command.Execute();
|
---|
| 119 | while (this.commands.Count != 0 && this.commands.Last != this.lastExecutedCommand)
|
---|
| 120 | this.commands.RemoveLast();
|
---|
| 121 | this.commands.AddLast(command);
|
---|
| 122 | this.lastExecutedCommand = this.commands.Last;
|
---|
| 123 | GC.Collect();
|
---|
| 124 | if (fireChangedEvent)
|
---|
| 125 | this.OnChanged();
|
---|
| 126 | }
|
---|
| 127 | finally {
|
---|
| 128 | System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public void Clear() {
|
---|
| 133 | this.commands.Clear();
|
---|
| 134 | this.OnChanged();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | public void Redo() {
|
---|
| 138 | if (this.lastExecutedCommand != this.commands.Last) {
|
---|
| 139 | System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
|
---|
| 140 | if (this.lastExecutedCommand == null)
|
---|
| 141 | this.lastExecutedCommand = this.commands.First;
|
---|
| 142 | else
|
---|
| 143 | this.lastExecutedCommand = this.lastExecutedCommand.Next;
|
---|
| 144 | try {
|
---|
| 145 | this.lastExecutedCommand.Value.Execute();
|
---|
| 146 | this.OnChanged();
|
---|
| 147 | }
|
---|
| 148 | catch (CommandExecutionException) {
|
---|
| 149 | this.lastExecutedCommand = this.lastExecutedCommand.Previous;
|
---|
| 150 | throw;
|
---|
| 151 | }
|
---|
| 152 | finally {
|
---|
| 153 | System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | public void Undo() {
|
---|
| 159 | if (this.lastExecutedCommand != null) {
|
---|
| 160 | System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
|
---|
| 161 | try {
|
---|
| 162 | this.lastExecutedCommand.Value.UndoExecute();
|
---|
| 163 | if (this.lastExecutedCommand == this.startMacroRecordingCommand)
|
---|
| 164 | isMacroRecording = false;
|
---|
| 165 | this.lastExecutedCommand = this.lastExecutedCommand.Previous;
|
---|
| 166 | this.OnChanged();
|
---|
| 167 | }
|
---|
| 168 | finally {
|
---|
| 169 | System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public void ToggleMacroRecording() {
|
---|
| 175 | if (!isMacroRecording)
|
---|
| 176 | this.startMacroRecordingCommand = lastExecutedCommand;
|
---|
| 177 | isMacroRecording = !isMacroRecording;
|
---|
| 178 | this.OnChanged();
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | #region IEnumerable<ICommand> Members
|
---|
| 182 | public IEnumerator<ICommand> GetEnumerator() {
|
---|
| 183 | return commands.GetEnumerator();
|
---|
| 184 | }
|
---|
| 185 | #endregion
|
---|
| 186 |
|
---|
| 187 | #region IEnumerable Members
|
---|
| 188 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
|
---|
| 189 | return commands.GetEnumerator();
|
---|
| 190 | }
|
---|
| 191 | #endregion
|
---|
| 192 | }
|
---|
| 193 | }
|
---|