6 using SiliconStudio.ActionStack;
7 using SiliconStudio.Core.Reflection;
8 using SiliconStudio.Quantum.Attributes;
10 namespace SiliconStudio.Quantum.
Commands
15 public string Name {
get {
return "AddPrimitiveKey"; } }
23 if (memberDescriptor != null)
25 var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<
SealedCollectionAttribute>(memberDescriptor.MemberInfo);
26 if (attrib != null && attrib.CollectionSealed)
31 if (dictionaryDescriptor == null)
33 return !dictionaryDescriptor.KeyType.IsClass || dictionaryDescriptor.KeyType == typeof(
string) || dictionaryDescriptor.KeyType.GetConstructor(
new Type[0]) != null;
40 var newKey = dictionaryDescriptor.KeyType != typeof(
string) ? Activator.CreateInstance(dictionaryDescriptor.KeyType) : GenerateStringKey(currentValue, descriptor, parameter);
41 var newItem = !dictionaryDescriptor.ValueType.IsAbstract ? Activator.CreateInstance(dictionaryDescriptor.ValueType) : null;
42 dictionaryDescriptor.SetValue(currentValue, newKey, newItem);
51 var key = undoToken.TokenValue;
52 dictionaryDescriptor.Remove(currentValue, key);
56 private static object GenerateStringKey(
object value,
ITypeDescriptor descriptor,
object baseValue)
59 string baseName = GenerateBaseName(baseValue);
63 while (dictionary.ContainsKey(value, baseName))
65 baseName = (baseValue != null ? baseValue.ToString() :
"Key") +
" " + ++i;
71 private static string GenerateBaseName(
object baseValue)
73 const string DefaultKey =
"Key";
75 if (baseValue == null)
78 var baseName = baseValue.ToString();
79 if (
string.IsNullOrWhiteSpace(baseName))
82 if (baseName.Any(x => !Char.IsLetterOrDigit(x) && x ==
' ' && x ==
'_'))
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...
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.
bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
Indicates whether this command can be attached to an object or a member with the given descriptors...
CombineMode
An enum that describes what to do with a node or a command when combining view models.
Base interface for node commands.
Provides access members of a type.
Base class for IMemberDescriptor for a MemberInfo
Represents a token that stores an unique identifier and an object. This token should be generated by ...