-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommandHistory.cs
255 lines (217 loc) · 6.59 KB
/
CommandHistory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NSFW.TimingEditor
{
/// <summary>
/// Base class for a command.
/// </summary>
public abstract class Command
{
public abstract void Execute();
public abstract void Undo();
}
/// <summary>
/// Change the value of a cell.
/// </summary>
public class EditCell : Command
{
private ITable table;
private double oldValue;
private double newValue;
private int x;
private int y;
public ITable Table { get { return this.table; } }
public int Y { get { return this.y; } }
public int X { get { return this.x; } }
public double OldValue { get { return this.oldValue; } }
public double NewValue { get { return this.newValue; } }
public EditCell(ITable table, int x, int y, double newValue)
{
this.table = table;
this.x = x;
this.y = y;
this.newValue = newValue;
this.oldValue = this.table.GetCell(this.x, this.y);
}
public override void Execute()
{
this.table.SetCell(this.x, this.y, this.newValue);
}
public override void Undo()
{
this.table.SetCell(this.x, this.y, this.oldValue);
}
}
/* public class EditMultipleCells : Command
{
private IList<EditCell> cells;
public EditMultipleCells(IList<EditCell> cells)
{
this.cells = cells;
}
public override void Execute()
{
foreach (EditCell cell in this.cells)
{
cell.Execute();
}
}
public override void Undo()
{
foreach (EditCell cell in this.cells)
{
cell.Undo();
}
}
}
public class Paste : Command
{
private ITable source;
private ITable destination;
private ITable backup;
public Paste(ITable source, ITable destination)
{
this.source = source;
this.destination = destination;
this.backup = destination.Clone();
}
public override void Execute()
{
this.source.CopyTo(destination);
}
public override void Undo()
{
this.backup.CopyTo(this.destination);
}
}
public class DoublePaste : Command
{
private Paste initial;
private Paste modified;
public DoublePaste(Paste initial, Paste modified)
{
this.initial = initial;
this.modified = modified;
}
public override void Execute()
{
this.initial.Execute();
this.modified.Execute();
}
public override void Undo()
{
this.initial.Undo();
this.modified.Undo();
}
}
*/
/// <summary>
/// Allows the undo/redo buttons to be updated appropriately.
/// </summary>
public delegate void UpdateCommandHistoryButtons(object sender, EventArgs args);
/// <summary>
/// Implements the CommandHistory patter from the Gang-Of-Four book.
/// </summary>
public class CommandHistory
{
/// <summary>
/// Singleton.
/// </summary>
private static CommandHistory instance = new CommandHistory();
/// <summary>
/// List of commands that can be undone.
/// </summary>
private List<Command> commands;
/// <summary>
/// List of commands that have been undone - and therefore can be redone.
/// </summary>
private List<Command> undone;
/// <summary>
/// Allows updating the undo/redo buttons.
/// </summary>
public event UpdateCommandHistoryButtons UpdateCommandHistoryButtons;
/// <summary>
/// Constructor.
/// </summary>
private CommandHistory()
{
this.commands = new List<Command>();
this.undone = new List<Command>();
}
/// <summary>
/// Singleton accessor.
/// </summary>
public static CommandHistory Instance
{
[System.Diagnostics.DebuggerStepThrough]
get
{
return instance;
}
}
/// <summary>
/// Indicates whether we're in a state where a command can be undone.
/// </summary>
public bool CanUndo { get { return this.commands.Count > 0; } }
/// <summary>
/// Indicates whether we're in a state when a command can be redone.
/// </summary>
public bool CanRedo { get { return this.undone.Count > 0; } }
/// <summary>
/// Execute a new command.
/// </summary>
public void Execute(Command command)
{
command.Execute();
this.commands.Add(command);
this.undone.Clear();
this.UpdateButtons();
}
/// <summary>
/// Undo a recent command.
/// </summary>
public Command Undo()
{
if (this.commands.Count == 0)
{
return null;
}
int lastIndex = this.commands.Count - 1;
Command command = this.commands[lastIndex];
this.commands.RemoveAt(lastIndex);
command.Undo();
this.undone.Add(command);
this.UpdateButtons();
return command;
}
/// <summary>
/// Redo a recently undone command.
/// </summary>
public Command Redo()
{
if (this.undone.Count == 0)
{
return null;
}
int lastIndex = this.undone.Count - 1;
Command command = this.undone[lastIndex];
this.undone.RemoveAt(lastIndex);
command.Execute();
this.commands.Add(command);
this.UpdateButtons();
return command;
}
/// <summary>
/// Update the undo/redo buttons.
/// </summary>
private void UpdateButtons()
{
if (this.UpdateCommandHistoryButtons != null)
{
this.UpdateCommandHistoryButtons(this, new EventArgs());
}
}
}
}