Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/CommandChain.cs @ 16575

Last change on this file since 16575 was 16567, checked in by gkronber, 5 years ago

#2520: changed StorableConstructors and added StorableType attributes in HeuristicLab.DataImporter addon

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
26using HEAL.Attic;
27
28namespace HeuristicLab.DataImporter.Data {
29  [StorableType("B58CF6F7-9B02-4528-A590-3A050A8A8F7A")]
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(StorableConstructorFlag _) {
62      this.commands = new LinkedList<ICommand>();
63    }
64    public CommandChain() {
65      this.commands = new LinkedList<ICommand>();
66    }
67
68    public ICommand LastExecutedCommand {
69      get { return lastExecutedCommand == null ? null : lastExecutedCommand.Value; }
70    }
71
72    public int CommandsCount {
73      get { return this.commands.Count; }
74    }
75
76    public bool FirstCommandReached {
77      get { return this.commands.Count == 0 || this.lastExecutedCommand == null; }
78    }
79
80    public bool LastCommandReached {
81      get { return this.commands.Count == 0 || this.lastExecutedCommand == this.commands.Last; }
82    }
83
84    public bool IsMacroRecording {
85      get { return isMacroRecording; }
86    }
87
88    public string NextCommandDescription {
89      get {
90        if (LastCommandReached)
91          return "Redo";
92        else if (lastExecutedCommand == null)
93          return commands.First.Value.Description;
94        else
95          return lastExecutedCommand.Next.Value.Description;
96      }
97    }
98
99    public string PreviousCommandDescription {
100      get { return FirstCommandReached ? "Undo" : lastExecutedCommand.Value.Description; }
101    }
102
103    public event EventHandler Changed;
104    protected virtual void OnChanged() {
105      if (this.Changed != null)
106        this.Changed(this, EventArgs.Empty);
107    }
108    public void FireChanged() {
109      this.OnChanged();
110    }
111
112    public void Add(ICommand command) {
113      this.Add(command, true);
114    }
115
116    public void Add(ICommand command, bool fireChangedEvent) {
117      try {
118        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
119        command.Execute();
120        while (this.commands.Count != 0 && this.commands.Last != this.lastExecutedCommand)
121          this.commands.RemoveLast();
122        this.commands.AddLast(command);
123        this.lastExecutedCommand = this.commands.Last;
124        GC.Collect();
125        if (fireChangedEvent)
126          this.OnChanged();
127      }
128      finally {
129        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
130      }
131    }
132
133    public void Clear() {
134      this.commands.Clear();
135      this.OnChanged();
136    }
137
138    public void Redo() {
139      if (this.lastExecutedCommand != this.commands.Last) {
140        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
141        if (this.lastExecutedCommand == null)
142          this.lastExecutedCommand = this.commands.First;
143        else
144          this.lastExecutedCommand = this.lastExecutedCommand.Next;
145        try {
146          this.lastExecutedCommand.Value.Execute();
147          this.OnChanged();
148        }
149        catch (CommandExecutionException) {
150          this.lastExecutedCommand = this.lastExecutedCommand.Previous;
151          throw;
152        }
153        finally {
154          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
155        }
156      }
157    }
158
159    public void Undo() {
160      if (this.lastExecutedCommand != null) {
161        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
162        try {
163          this.lastExecutedCommand.Value.UndoExecute();
164          if (this.lastExecutedCommand == this.startMacroRecordingCommand)
165            isMacroRecording = false;
166          this.lastExecutedCommand = this.lastExecutedCommand.Previous;
167          this.OnChanged();
168        }
169        finally {
170          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
171        }
172      }
173    }
174
175    public void ToggleMacroRecording() {
176      if (!isMacroRecording)
177        this.startMacroRecordingCommand = lastExecutedCommand;
178      isMacroRecording = !isMacroRecording;
179      this.OnChanged();
180    }
181
182    #region IEnumerable<ICommand> Members
183    public IEnumerator<ICommand> GetEnumerator() {
184      return commands.GetEnumerator();
185    }
186    #endregion
187
188    #region IEnumerable Members
189    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
190      return commands.GetEnumerator();
191    }
192    #endregion
193  }
194}
Note: See TracBrowser for help on using the repository browser.