Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CancellableCommand.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.Collections.Generic;
4 
5 using SiliconStudio.ActionStack;
6 using SiliconStudio.Presentation.ViewModel;
7 
8 namespace SiliconStudio.Presentation.Commands
9 {
10  /// <summary>
11  /// A command that supports undo/redo
12  /// </summary>
13  public abstract class CancellableCommand : CommandBase
14  {
15  private readonly IActionStack actionStack;
16  private readonly IEnumerable<IDirtiableViewModel> dirtiables;
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref="CancellableCommand"/> class.
20  /// </summary>
21  /// <param name="serviceProvider">A service provider that can provide a <see cref="IActionStack"/> to use for this view model.</param>
22  /// <param name="dirtiables">The <see cref="IDirtiableViewModel"/> instances associated to this command.</param>
24  : base(serviceProvider)
25  {
26  actionStack = serviceProvider.Get<IActionStack>();
27  this.dirtiables = dirtiables;
28  }
29 
30  /// <summary>
31  /// The name of this command.
32  /// </summary>
33  public abstract string Name { get; }
34 
35  /// <inheritdoc/>
36  public override void Execute(object parameter)
37  {
38  ExecuteCommand(parameter, true);
39  }
40 
41  /// <summary>
42  /// Undoes the execution of this command.
43  /// </summary>
44  /// <param name="parameter">The parameter used to invoke the command.</param>
45  /// <param name="token">The <see cref="UndoToken"/> generated by the execution of this command.</param>
46  public void UndoCommand(object parameter, UndoToken token)
47  {
48  Undo(parameter, token);
49  }
50 
51  /// <summary>
52  /// Executes the command and return a token that can be used to undo it.
53  /// </summary>
54  /// <param name="parameter">The command parameter.</param>
55  /// <param name="createActionItem">Indicates whether to create an action item in the action stack. This should be false in case of a Redo operation.</param>
56  /// <returns>An <see cref="UndoToken"/> that can be used to undo the command.</returns>
57  public UndoToken ExecuteCommand(object parameter, bool createActionItem)
58  {
59  // TODO: Improve this - we're discarding any change made directly by the command invoke and create a CommandActionItem after.
60  var transactionalActionStack = actionStack as ITransactionalActionStack;
61  if (transactionalActionStack != null)
62  transactionalActionStack.BeginTransaction();
63 
64  UndoToken token = Redo(parameter, createActionItem);
65 
66  if (transactionalActionStack != null)
67  transactionalActionStack.DiscardTransaction();
68 
69  if (token.CanUndo && createActionItem)
70  {
71  var actionItem = new CommandActionItem(this, parameter, token, dirtiables);
72  actionStack.Add(actionItem);
73  }
74  return token;
75  }
76 
77  /// <summary>
78  /// Does or redoes the execution of the command.
79  /// </summary>
80  /// <param name="parameter">The command parameter.</param>
81  /// <param name="creatingActionItem">Indicates whether an action item is being created for the action stack</param>
82  /// <returns>An <see cref="UndoToken"/> that can be used to undo the command.</returns>
83  protected abstract UndoToken Redo(object parameter, bool creatingActionItem);
84 
85  /// <summary>
86  /// Undoes the execution of the command.
87  /// </summary>
88  /// <param name="parameter">The command parameter.</param>
89  /// <param name="token">The <see cref="UndoToken"/> generated by the execution of this command.</param>
90  protected abstract void Undo(object parameter, UndoToken token);
91  }
92 }
UndoToken ExecuteCommand(object parameter, bool createActionItem)
Executes the command and return a token that can be used to undo it.
Base interface to for an action stack.
Definition: IActionStack.cs:11
void UndoCommand(object parameter, UndoToken token)
Undoes the execution of this command.
An abstract class that is the base implementation of the ICommandBase interface.
Definition: CommandBase.cs:13
CancellableCommand(IViewModelServiceProvider serviceProvider, IEnumerable< IDirtiableViewModel > dirtiables)
Initializes a new instance of the CancellableCommand class.
Represents a token that stores an unique identifier and an object. This token should be generated by ...
Definition: UndoToken.cs:12
Base interface for a transactional action stack.