Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AddNewItemCommand.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 using SiliconStudio.Quantum.Attributes;
8 
9 namespace SiliconStudio.Quantum.Commands
10 {
11  /// <summary>
12  /// This command construct a new item and add it to the list contained in the value of the node. In order to be used,
13  /// the node owning this command must contains a non-null value of type IList{T}. An new item of type T will be created,
14  /// or an exception will be thrown if T could not be determinated or has no parameterless constructor.
15  /// </summary>
16  /// <remarks>No parameter is required when invoking this command.</remarks>
18  {
19  /// <inheritdoc/>
20  public string Name { get { return "AddNewItem"; } }
21 
22  /// <inheritdoc/>
23  public CombineMode CombineMode { get { return CombineMode.CombineOnlyForAll; } }
24 
25  /// <inheritdoc/>
26  public bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
27  {
28  if (memberDescriptor != null)
29  {
30  var attrib = TypeDescriptorFactory.Default.AttributeRegistry.GetAttribute<SealedCollectionAttribute>(memberDescriptor.MemberInfo);
31  if (attrib != null && attrib.CollectionSealed)
32  return false;
33  }
34 
35  var collectionDescriptor = typeDescriptor as CollectionDescriptor;
36  if (collectionDescriptor == null)
37  return false;
38 
39  var elementType = collectionDescriptor.ElementType;
40  return collectionDescriptor.HasAdd && (!elementType.IsClass || elementType.GetConstructor(Type.EmptyTypes) != null || elementType.IsAbstract || elementType.IsNullable() || elementType == typeof(string));
41  }
42 
43  /// <inheritdoc/>
44  public object Invoke(object currentValue, ITypeDescriptor descriptor, object parameter, out UndoToken undoToken)
45  {
46  var collectionDescriptor = (CollectionDescriptor)descriptor;
47  if (collectionDescriptor.ElementType.IsAbstract || collectionDescriptor.ElementType.IsNullable())
48  {
49  undoToken = new UndoToken(true, collectionDescriptor.GetCollectionCount(currentValue));
50  collectionDescriptor.Add(currentValue, parameter);
51  }
52  else if (collectionDescriptor.ElementType == typeof(string))
53  {
54  undoToken = new UndoToken(true, collectionDescriptor.GetCollectionCount(currentValue));
55  collectionDescriptor.Add(currentValue, parameter ?? "");
56  }
57  else
58  {
59  var newItem = Activator.CreateInstance(collectionDescriptor.ElementType);
60  undoToken = new UndoToken(true, collectionDescriptor.GetCollectionCount(currentValue));
61  collectionDescriptor.Add(currentValue, parameter ?? newItem);
62  }
63  return currentValue;
64  }
65 
66  /// <inheritdoc/>
67  public object Undo(object currentValue, ITypeDescriptor descriptor, UndoToken undoToken)
68  {
69  var index = (int)undoToken.TokenValue;
70  var collectionDescriptor = (CollectionDescriptor)descriptor;
71  collectionDescriptor.RemoveAt(currentValue, index);
72  return currentValue;
73  }
74  }
75 }
Provides a descriptor for a System.Collections.ICollection.
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...
This command construct a new item and add it to the list contained in the value of the node...
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 Undo(object currentValue, ITypeDescriptor descriptor, UndoToken undoToken)
Undoes an invoke of the node command. The current value of the associated object or member...
bool CanAttach(ITypeDescriptor typeDescriptor, MemberDescriptorBase memberDescriptor)
Indicates whether this command can be attached to an object or a member with the given descriptors...
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