Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/CommandChain.cs @ 9614

Last change on this file since 9614 was 9614, checked in by mkommend, 11 years ago

#1734: Added StorableConstructor to all storable DataImporter classes.

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.DataImporter.Data.CommandBase;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27
28namespace HeuristicLab.DataImporter.Data {
29  [StorableClass]
30  public class CommandChain : IEnumerable<ICommand> {
31    private LinkedList<ICommand> commands;
32    private LinkedListNode<ICommand> lastExecutedCommand;
33    private LinkedListNode<ICommand> startMacroRecordingCommand;
34    private bool isMacroRecording;
35
36    [Storable]
37    private List<ICommand> CommandStorable {
38      get {
39        LinkedListNode<ICommand> cmdNode = null;
40        if (this.isMacroRecording && startMacroRecordingCommand != null)
41          cmdNode = startMacroRecordingCommand.Next;
42        else
43          cmdNode = commands.First;
44
45        List<ICommand> list = new List<ICommand>();
46        while (cmdNode != lastExecutedCommand.Next) {
47          list.Add(cmdNode.Value);
48          cmdNode = cmdNode.Next;
49        }
50        return list;
51      }
52      set {
53        foreach (ICommand command in value)
54          commands.AddLast(command);
55        this.lastExecutedCommand = commands.Last;
56        this.FireChanged();
57      }
58    }
59
60    [StorableConstructor]
61    protected CommandChain(bool deserializing)
62      : base() {
63      this.commands = new LinkedList<ICommand>();
64    }
65    public CommandChain() {
66      this.commands = new LinkedList<ICommand>();
67    }
68
69    public ICommand LastExecutedCommand {
70      get { return lastExecutedCommand == null ? null : lastExecutedCommand.Value; }
71    }
72
73    public int CommandsCount {
74      get { return this.commands.Count; }
75    }
76
77    public bool FirstCommandReached {
78      get { return this.commands.Count == 0 || this.lastExecutedCommand == null; }
79    }
80
81    public bool LastCommandReached {
82      get { return this.commands.Count == 0 || this.lastExecutedCommand == this.commands.Last; }
83    }
84
85    public bool IsMacroRecording {
86      get { return isMacroRecording; }
87    }
88
89    public string NextCommandDescription {
90      get {
91        if (LastCommandReached)
92          return "Redo";
93        else if (lastExecutedCommand == null)
94          return commands.First.Value.Description;
95        else
96          return lastExecutedCommand.Next.Value.Description;
97      }
98    }
99
100    public string PreviousCommandDescription {
101      get { return FirstCommandReached ? "Undo" : lastExecutedCommand.Value.Description; }
102    }
103
104    public event EventHandler Changed;
105    protected virtual void OnChanged() {
106      if (this.Changed != null)
107        this.Changed(this, EventArgs.Empty);
108    }
109    public void FireChanged() {
110      this.OnChanged();
111    }
112
113    public void Add(ICommand command) {
114      this.Add(command, true);
115    }
116
117    public void Add(ICommand command, bool fireChangedEvent) {
118      try {
119        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
120        command.Execute();
121        while (this.commands.Count != 0 && this.commands.Last != this.lastExecutedCommand)
122          this.commands.RemoveLast();
123        this.commands.AddLast(command);
124        this.lastExecutedCommand = this.commands.Last;
125        GC.Collect();
126        if (fireChangedEvent)
127          this.OnChanged();
128      }
129      finally {
130        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
131      }
132    }
133
134    public void Clear() {
135      this.commands.Clear();
136      this.OnChanged();
137    }
138
139    public void Redo() {
140      if (this.lastExecutedCommand != this.commands.Last) {
141        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
142        if (this.lastExecutedCommand == null)
143          this.lastExecutedCommand = this.commands.First;
144        else
145          this.lastExecutedCommand = this.lastExecutedCommand.Next;
146        try {
147          this.lastExecutedCommand.Value.Execute();
148          this.OnChanged();
149        }
150        catch (CommandExecutionException) {
151          this.lastExecutedCommand = this.lastExecutedCommand.Previous;
152          throw;
153        }
154        finally {
155          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
156        }
157      }
158    }
159
160    public void Undo() {
161      if (this.lastExecutedCommand != null) {
162        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
163        try {
164          this.lastExecutedCommand.Value.UndoExecute();
165          if (this.lastExecutedCommand == this.startMacroRecordingCommand)
166            isMacroRecording = false;
167          this.lastExecutedCommand = this.lastExecutedCommand.Previous;
168          this.OnChanged();
169        }
170        finally {
171          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
172        }
173      }
174    }
175
176    public void ToggleMacroRecording() {
177      if (!isMacroRecording)
178        this.startMacroRecordingCommand = lastExecutedCommand;
179      isMacroRecording = !isMacroRecording;
180      this.OnChanged();
181    }
182
183    #region IEnumerable<ICommand> Members
184    public IEnumerator<ICommand> GetEnumerator() {
185      return commands.GetEnumerator();
186    }
187    #endregion
188
189    #region IEnumerable Members
190    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
191      return commands.GetEnumerator();
192    }
193    #endregion
194  }
195}
Note: See TracBrowser for help on using the repository browser.