5 using SiliconStudio.ActionStack;
6 using SiliconStudio.Core.Reflection;
7 using SiliconStudio.Quantum.Attributes;
9 namespace SiliconStudio.Quantum.
Commands
13 private struct UndoTokenData
15 private readonly
object indexer;
16 private readonly
object item;
17 public UndoTokenData(
object indexer,
object item)
19 this.indexer = indexer;
23 public object Indexer {
get {
return indexer; } }
25 public object Item {
get {
return item; } }
29 public string Name {
get {
return "RemoveItem"; } }
38 if (memberDescriptor != null)
40 var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<
SealedCollectionAttribute>(memberDescriptor.MemberInfo);
41 if (attrib != null && attrib.CollectionSealed)
47 if (collectionDescriptor != null)
49 var elementType = collectionDescriptor.ElementType;
51 return collectionDescriptor.HasRemoveAt && (!elementType.IsClass || elementType.GetConstructor(Type.EmptyTypes) != null || elementType.IsAbstract || elementType.IsNullable() || elementType == typeof(
string));
54 return dictionaryDescriptor != null;
62 if (collectionDescriptor != null)
64 var position = (int)parameter;
65 var removedObject = collectionDescriptor.GetValue(currentValue, position);
66 undoToken =
new UndoToken(
true,
new UndoTokenData(parameter, removedObject));
67 collectionDescriptor.RemoveAt(currentValue, position);
69 else if (dictionaryDescriptor != null)
71 var removedObject = dictionaryDescriptor.GetValue(currentValue, parameter);
72 undoToken =
new UndoToken(
true,
new UndoTokenData(parameter, removedObject));
73 dictionaryDescriptor.Remove(currentValue, parameter);
76 throw new InvalidOperationException(
"This command cannot be executed on the given object.");
86 var undoData = (UndoTokenData)undoToken.
TokenValue;
87 if (collectionDescriptor != null)
89 var position = (int)undoData.Indexer;
90 if (collectionDescriptor.HasInsert)
91 collectionDescriptor.Insert(currentValue, position, undoData.Item);
93 collectionDescriptor.Add(currentValue, undoData.Item);
95 else if (dictionaryDescriptor != null)
97 if (dictionaryDescriptor.ContainsKey(currentValue, undoData.Indexer))
98 throw new InvalidOperationException(
"Unable to undo remove: the dictionary contains the key to re-add.");
99 dictionaryDescriptor.SetValue(currentValue, undoData.Indexer, undoData.Item);
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.ICollection.
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.
bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
Indicates whether this command can be attached to an object or a member with the given descriptors...
Base interface for node commands.
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...
Base class for IMemberDescriptor for a MemberInfo
Represents a token that stores an unique identifier and an object. This token should be generated by ...