Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RenameStringKeyCommand.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 
5 using SiliconStudio.ActionStack;
6 using SiliconStudio.Core.Reflection;
7 
8 namespace SiliconStudio.Quantum.Commands
9 {
11  {
12  private struct UndoTokenData
13  {
14  private readonly object previousIndex;
15 
16  private readonly object newIndex;
17 
18  public UndoTokenData(object previousIndex, object newIndex)
19  {
20  this.previousIndex = previousIndex;
21  this.newIndex = newIndex;
22  }
23 
24  public object PreviousIndex { get { return previousIndex; } }
25 
26  public object NewIndex { get { return newIndex; } }
27  }
28 
29  /// <inheritdoc/>
30  public string Name { get { return "RenameStringKey"; } }
31 
32  /// <inheritdoc/>
33  public CombineMode CombineMode { get { return CombineMode.AlwaysCombine; } }
34 
35 
36  /// <inheritdoc/>
37  public bool CanAttach(ITypeDescriptor descriptor, MemberDescriptorBase memberDescriptor)
38  {
39  var dictionaryDescriptor = descriptor as DictionaryDescriptor;
40  return dictionaryDescriptor != null && dictionaryDescriptor.KeyType == typeof(string);
41  }
42 
43  /// <inheritdoc/>
44  public object Invoke(object currentValue, ITypeDescriptor descriptor, object parameter, out UndoToken undoToken)
45  {
46  var dictionaryDescriptor = descriptor as DictionaryDescriptor;
47  var tuple = parameter as Tuple<object, object>;
48  if (dictionaryDescriptor == null || tuple == null)
49  throw new InvalidOperationException("This command cannot be executed on the given object.");
50 
51  var removedObject = dictionaryDescriptor.GetValue(currentValue, tuple.Item1);
52  undoToken = new UndoToken(true, new UndoTokenData(tuple.Item1, tuple.Item2));
53  dictionaryDescriptor.Remove(currentValue, tuple.Item1);
54  dictionaryDescriptor.SetValue(currentValue, tuple.Item2, removedObject);
55 
56  return currentValue;
57  }
58 
59  /// <inheritdoc/>
60  public object Undo(object currentValue, ITypeDescriptor descriptor, UndoToken undoToken)
61  {
62  var dictionaryDescriptor = descriptor as DictionaryDescriptor;
63  var undoData = (UndoTokenData)undoToken.TokenValue;
64  if (dictionaryDescriptor == null)
65  throw new InvalidOperationException("This command cannot be cancelled on the given object.");
66 
67  if (dictionaryDescriptor.ContainsKey(currentValue, undoData.PreviousIndex))
68  throw new InvalidOperationException("Unable to undo remove: the dictionary contains the key to re-add.");
69 
70  var removedObject = dictionaryDescriptor.GetValue(currentValue, undoData.NewIndex);
71  dictionaryDescriptor.Remove(currentValue, undoData.NewIndex);
72  dictionaryDescriptor.SetValue(currentValue, undoData.PreviousIndex, removedObject);
73 
74  return currentValue;
75  }
76  }
77 }
bool CanAttach(ITypeDescriptor descriptor, MemberDescriptorBase memberDescriptor)
Indicates whether this command can be attached to an object or a member with the given descriptors...
object Undo(object currentValue, ITypeDescriptor descriptor, UndoToken undoToken)
Undoes an invoke of the node command. The current value of the associated object or member...
Provides a descriptor for a System.Collections.IDictionary.
CombineMode
An enum that describes what to do with a node or a command when combining view models.
Definition: CombineMode.cs:8
Base interface for node commands.
Definition: INodeCommand.cs:11
object Invoke(object currentValue, ITypeDescriptor descriptor, object parameter, out UndoToken undoToken)
Invokes the node command. The current value of the associated object or member.The type descriptor of...
Provides access members of a type.
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
Base class for IMemberDescriptor for a MemberInfo
Represents a token that stores an unique identifier and an object. This token should be generated by ...
Definition: UndoToken.cs:12