Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17507 was 16994, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

File size: 6.1 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 HEAL.Attic;
26
27namespace HeuristicLab.DataImporter.Data {
28  [StorableType("B58CF6F7-9B02-4528-A590-3A050A8A8F7A")]
29  public class CommandChain : IEnumerable<ICommand> {
30    private LinkedList<ICommand> commands;
31    private LinkedListNode<ICommand> lastExecutedCommand;
32    private LinkedListNode<ICommand> startMacroRecordingCommand;
33    private bool isMacroRecording;
34
35    [Storable]
36    private List<ICommand> CommandStorable {
37      get {
38        LinkedListNode<ICommand> cmdNode = null;
39        if (this.isMacroRecording && startMacroRecordingCommand != null)
40          cmdNode = startMacroRecordingCommand.Next;
41        else
42          cmdNode = commands.First;
43
44        List<ICommand> list = new List<ICommand>();
45        while (cmdNode != lastExecutedCommand.Next) {
46          list.Add(cmdNode.Value);
47          cmdNode = cmdNode.Next;
48        }
49        return list;
50      }
51      set {
52        foreach (ICommand command in value)
53          commands.AddLast(command);
54        this.lastExecutedCommand = commands.Last;
55        this.FireChanged();
56      }
57    }
58
59    [StorableConstructor]
60    protected CommandChain(StorableConstructorFlag _) {
61      this.commands = new LinkedList<ICommand>();
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}
Note: See TracBrowser for help on using the repository browser.