Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ModelNode.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 
7 using SiliconStudio.Quantum.Contents;
8 
9 namespace SiliconStudio.Quantum
10 {
11  /// <summary>
12  /// This class is the default implementation of the <see cref="IModelNode"/>.
13  /// </summary>
14  public class ModelNode : IModelNode
15  {
16  private readonly List<IModelNode> children = new List<IModelNode>();
17  private readonly List<INodeCommand> commands = new List<INodeCommand>();
18  private IContent content;
19  private bool isSealed;
20 
21  /// <summary>
22  /// Initializes a new instance of the <see cref="ModelNode"/> class.
23  /// </summary>
24  /// <param name="name">The name of this node.</param>
25  /// <param name="content">The content of this node.</param>
26  /// <param name="guid">An unique identifier for this node.</param>
27  internal ModelNode(string name, IContent content, Guid guid)
28  {
29  if (name == null) throw new ArgumentNullException("name");
30  if (content == null) throw new ArgumentNullException("content");
31  if (guid == Guid.Empty) throw new ArgumentException(@"The guid must be differ from Guid.Empty.", "content");
32  this.content = content;
33  Name = name;
34  Guid = guid;
35  }
36 
37  /// <inheritdoc/>
38  public string Name { get; private set; }
39 
40  /// <inheritdoc/>
41  public Guid Guid { get; private set; }
42 
43  /// <inheritdoc/>
44  public virtual IContent Content { get { return content; } set { content = value; } }
45 
46  /// <inheritdoc/>
47  public virtual IModelNode Parent { get; private set; }
48 
49  /// <inheritdoc/>
50  public IReadOnlyCollection<IModelNode> Children { get { return children; } }
51 
52  /// <inheritdoc/>
53  public IReadOnlyCollection<INodeCommand> Commands { get { return commands; } }
54 
55  /// <summary>
56  /// Add a child to this node. The node must not have been sealed yet.
57  /// </summary>
58  /// <param name="child">The child node to add.</param>
59  public void AddChild(ModelNode child)
60  {
61  if (isSealed)
62  throw new InvalidOperationException("Unable to add a child to a ModelNode that has been sealed");
63 
64  if (child.Parent != null)
65  throw new ArgumentException(@"This node has already been registered to a different parent", "child");
66 
67  if (Content.Reference != null)
68  throw new InvalidOperationException("A ModelNode cannot have children when its content hold a reference.");
69 
70  child.Parent = this;
71  children.Add(child);
72  }
73 
74  /// <summary>
75  /// Add a command to this node. The node must not have been sealed yet.
76  /// </summary>
77  /// <param name="command">The node command to add.</param>
78  public void AddCommand(INodeCommand command)
79  {
80  if (isSealed)
81  throw new InvalidOperationException("Unable to add a command to a ModelNode that has been sealed");
82 
83  commands.Add(command);
84  }
85 
86  /// <summary>
87  /// Remove a command from this node. The node must not have been sealed yet.
88  /// </summary>
89  /// <param name="command">The node command to remove.</param>
90  public void RemoveCommand(INodeCommand command)
91  {
92  if (isSealed)
93  throw new InvalidOperationException("Unable to add a child to a ModelNode that has been sealed");
94 
95  commands.Remove(command);
96  }
97 
98  /// <summary>
99  /// Seal the node, indicating its construction is finished and that no more children or commands will be added.
100  /// </summary>
101  public void Seal()
102  {
103  isSealed = true;
104  }
105 
106  /// <inheritdoc/>
107  public override string ToString()
108  {
109  return string.Format("{0}: [{1}]", Name, Content.Value);
110  }
111  }
112 }
virtual IModelNode Parent
Definition: ModelNode.cs:47
This class is the default implementation of the IModelNode.
Definition: ModelNode.cs:14
override string ToString()
Definition: ModelNode.cs:107
Content of a IModelNode.
Definition: IContent.cs:13
Base interface for node commands.
Definition: INodeCommand.cs:11
void RemoveCommand(INodeCommand command)
Remove a command from this node. The node must not have been sealed yet.
Definition: ModelNode.cs:90
void AddCommand(INodeCommand command)
Add a command to this node. The node must not have been sealed yet.
Definition: ModelNode.cs:78
The IModelNode interface represents a node in a model object. A model object is represented by a grap...
Definition: IModelNode.cs:16
void Seal()
Seal the node, indicating its construction is finished and that no more children or commands will be ...
Definition: ModelNode.cs:101
void AddChild(ModelNode child)
Add a child to this node. The node must not have been sealed yet.
Definition: ModelNode.cs:59