Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SingleObservableNode.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 using System.Linq;
6 using System.Text.RegularExpressions;
7 
8 using SiliconStudio.Core;
9 using SiliconStudio.Core.Reflection;
10 using SiliconStudio.Quantum;
11 
12 namespace SiliconStudio.Presentation.Quantum
13 {
14  public abstract class SingleObservableNode : ObservableNode
15  {
16  public readonly string[] ReservedNames = { "Owner", "Name", "DisplayName", "Path", "Parent", "Root", "Type", "IsPrimitive", "IsVisible", "IsReadOnly", "Value", "TypedValue", "Index", "Guid", "Children", "Commands", "AssociatedData", "HasList", "HasDictionary", "CombinedNodes", "HasMultipleValues", "HasMultipleInitialValues", "ResetInitialValues", "DistinctInitialValues" };
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref="SingleObservableNode"/> class.
20  /// </summary>
21  /// <param name="ownerViewModel">The <see cref="ObservableViewModel"/> that owns the new <see cref="SingleObservableNode"/>.</param>
22  /// <param name="baseName">The base name of this node. Can be null if <see cref="index"/> is not. If so a name will be automatically generated from the index.</param>
23  /// <param name="parentNode">The parent node of the new <see cref="SingleObservableNode"/>, or <c>null</c> if the node being created is the root node of the view model.</param>
24  /// <param name="index">The index of this content in the model node, when this node represent an item of a collection. <c>null</c> must be passed otherwise</param>
25  protected SingleObservableNode(ObservableViewModel ownerViewModel, string baseName, ObservableNode parentNode, object index = null)
26  : base(ownerViewModel, parentNode, index)
27  {
28  if (baseName == null && index == null)
29  throw new ArgumentException("baseName and index can't be both null.");
30 
31  CombineMode = CombineMode.CombineOnlyForAll;
32  SetName(baseName);
33  }
34 
35  /// <summary>
36  /// Gets or sets the <see cref="CombineMode"/> of this single node.
37  /// </summary>
38  public CombineMode CombineMode { get; set; }
39 
40  public VirtualObservableNode CreateVirtualChild(string name, Type contentType, int? order, object initialValue, NodeCommandWrapperBase valueChangedCommand = null, IReadOnlyDictionary<string, object> nodeAssociatedData = null)
41  {
42  var observableChild = VirtualObservableNode.Create(Owner, name, this, order, contentType, initialValue, valueChangedCommand);
43  if (nodeAssociatedData != null)
44  {
45  foreach (var data in nodeAssociatedData)
46  {
47  observableChild.AddAssociatedData(data.Key, data.Value);
48  }
49  }
50  AddChild(observableChild);
51  return observableChild;
52  }
53 
54  /// <inheritdoc/>
55  public override string ToString()
56  {
57  return string.Format("{0}: [{1}]", Name, Value);
58  }
59 
60  private void SetName(string nodeName)
61  {
62  var index = Index;
63  nodeName = nodeName != null ? nodeName.Replace(".", "-") : null;
64 
65  if (!string.IsNullOrWhiteSpace(nodeName))
66  {
67  Name = nodeName;
68  DisplayName = Regex.Replace(nodeName, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ");
69  }
70  else if (index != null)
71  {
72  // TODO: make a better interface for custom naming specification
73  var propertyKey = index as PropertyKey;
74  if (propertyKey != null)
75  {
76  string name = propertyKey.Name.Replace(".", "-");
77 
78  if (name == "Key")
79  name = propertyKey.PropertyType.Name.Replace(".", "-");
80 
81  Name = name;
82  var parts = propertyKey.Name.Split('.');
83  DisplayName = parts.Length == 2 ? string.Format("{0} ({1})", parts[1], parts[0]) : name;
84  }
85  else
86  {
87  if (index.GetType().IsNumeric())
88  Name = "Item " + index.ToString().Replace(".", "-");
89  else
90  Name = index.ToString().Replace(".", "-");
91 
92  DisplayName = Name;
93  }
94  }
95 
96  if (ReservedNames.Contains(Name))
97  {
98  Name += "_";
99  }
100  }
101  }
102 }
CombineMode
An enum that describes what to do with a node or a command when combining view models.
Definition: CombineMode.cs:8
A base class to wrap one or multiple INodeCommand instances into a CancellableCommand.
SingleObservableNode(ObservableViewModel ownerViewModel, string baseName, ObservableNode parentNode, object index=null)
Initializes a new instance of the SingleObservableNode class.
VirtualObservableNode CreateVirtualChild(string name, Type contentType, int?order, object initialValue, NodeCommandWrapperBase valueChangedCommand=null, IReadOnlyDictionary< string, object > nodeAssociatedData=null)
A class that represents a tag propety.
Definition: PropertyKey.cs:17