Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CombinedNodeCommandWrapper.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;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 using SiliconStudio.ActionStack;
8 using SiliconStudio.Presentation.ViewModel;
9 using SiliconStudio.Quantum;
10 
11 namespace SiliconStudio.Presentation.Quantum
12 {
14  {
15  private readonly IEnumerable<ModelNodeCommandWrapper> commands;
16  private readonly ITransactionalActionStack actionStack;
17  private readonly string name;
18  private readonly ObservableViewModelService service;
19  private readonly ObservableViewModelIdentifier identifier;
20 
21  public CombinedNodeCommandWrapper(IViewModelServiceProvider serviceProvider, string name, string observableNodePath, ObservableViewModelIdentifier identifier, IEnumerable<ModelNodeCommandWrapper> commands)
22  : base(serviceProvider, null)
23  {
24  if (commands == null) throw new ArgumentNullException("commands");
25  service = serviceProvider.Get<ObservableViewModelService>();
26  this.commands = commands;
27  this.name = name;
28  this.identifier = identifier;
29  actionStack = serviceProvider.Get<ITransactionalActionStack>();
30  ObservableNodePath = observableNodePath;
31  }
32 
33  public override string Name { get { return name; } }
34 
35  public override CombineMode CombineMode { get { return CombineMode.DoNotCombine; } }
36 
37  public override void Execute(object parameter)
38  {
39  actionStack.BeginTransaction();
40  Redo(parameter, true);
41  var displayName = "Executing " + Name;
42 
43  var observableViewModel = service.ViewModelProvider(identifier);
44  if (observableViewModel != null && !commands.Any(x => observableViewModel.MatchCombinedRootNode(x.GetCommandRootNode())))
45  observableViewModel = null;
46 
47  var node = observableViewModel != null ? observableViewModel.ResolveObservableNode(ObservableNodePath) as CombinedObservableNode : null;
48  // TODO: this need to be verified but I suppose node is never null
49  actionStack.EndTransaction(displayName, x => new CombinedValueChangedActionItem(displayName, service, node != null ? node.Path : null, identifier, x));
50  }
51 
52  protected override UndoToken Redo(object parameter, bool creatingActionItem)
53  {
54  var undoTokens = new Dictionary<ModelNodeCommandWrapper, UndoToken>();
55  bool canUndo = false;
56  foreach (var command in commands)
57  {
58  var undoToken = command.ExecuteCommand(parameter, creatingActionItem);
59  undoTokens.Add(command, undoToken);
60  canUndo = canUndo || undoToken.CanUndo;
61  }
62  Refresh();
63  return new UndoToken(canUndo, undoTokens);
64  }
65 
66  protected override void Undo(object parameter, UndoToken token)
67  {
68  var undoTokens = (Dictionary<ModelNodeCommandWrapper, UndoToken>)token.TokenValue;
69  foreach (var command in commands)
70  {
71  command.UndoCommand(parameter, undoTokens[command]);
72  }
73  Refresh();
74  }
75 
76  private void Refresh()
77  {
78  var observableViewModel = service.ViewModelProvider(identifier);
79  if (observableViewModel != null && !commands.Any(x => observableViewModel.MatchCombinedRootNode(x.GetCommandRootNode())))
80  observableViewModel = null;
81 
82  var observableNode = observableViewModel != null ? observableViewModel.ResolveObservableNode(ObservableNodePath) as CombinedObservableNode : null;
83 
84  // Recreate observable nodes to apply changes
85  if (observableNode != null)
86  {
87  observableNode.Refresh();
88  observableNode.Owner.NotifyNodeChanged(observableNode.Path);
89  }
90  }
91  }
92 }
override void Undo(object parameter, UndoToken token)
Undoes the execution of the command.
override UndoToken Redo(object parameter, bool creatingActionItem)
Does or redoes the execution of the command.
CombineMode
An enum that describes what to do with a node or a command when combining view models.
Definition: CombineMode.cs:8
A class that provides various services to ObservableViewModel objects
A base class to wrap one or multiple INodeCommand instances into a CancellableCommand.
CombinedNodeCommandWrapper(IViewModelServiceProvider serviceProvider, string name, string observableNodePath, ObservableViewModelIdentifier identifier, IEnumerable< ModelNodeCommandWrapper > commands)
object TokenValue
Gets a user-defined object hosted by the token that should store all information needed to undo a com...
Definition: UndoToken.cs:43
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.