Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SectionViewModel.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Configuration;
6 using System.Collections.ObjectModel;
7 using System.Reflection;
8 using System.ComponentModel;
9 using System.Xml;
10 
11 using SiliconStudio.Presentation.ViewModel;
12 
13 namespace SiliconStudio.Paradox.ConfigEditor.ViewModels
14 {
16  {
17  public Assembly Assembly { get; private set; }
18  public Type Section { get; private set; }
19  public IEnumerable<PropertyViewModel> Properties { get; private set; }
20 
21  private readonly List<PropertyViewModel> workingProperties = new List<PropertyViewModel>();
22 
23  public SectionViewModel(Assembly assembly, Type section)
24  {
25  if (assembly == null)
26  throw new ArgumentNullException("assembly");
27  if (section == null)
28  throw new ArgumentNullException("section");
29 
30  Assembly = assembly;
31  Section = section;
32  Name = Section.DeclaringType != null ? Section.DeclaringType.Name : "";
33  Properties = new ReadOnlyCollection<PropertyViewModel>(workingProperties);
34  }
35 
36  public void AddProperty(PropertyViewModel property)
37  {
38  if (property.Parent != this)
39  throw new InvalidOperationException("Bad parent");
40  workingProperties.Add(property);
41 
42  property.PropertyChanged += OnPropertyViewModelPropertyChanged;
43  }
44 
45  private void OnPropertyViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
46  {
47  if (e.PropertyName == "IsUsed")
48  IsUsed = workingProperties.Any(p => p.IsUsed);
49  }
50 
51  private bool isUsed;
52  public bool IsUsed
53  {
54  get { return isUsed; }
55  set { SetValue(ref isUsed, value, "IsUsed"); }
56  }
57 
58  public string TypeName { get { return Section.FullName; } }
59 
60  private string name;
61  public string Name
62  {
63  get { return name; }
64  set
65  {
66  if (SetValue(ref name, value, "Name"))
67  IsValidName = !string.IsNullOrWhiteSpace(name);
68  }
69  }
70 
71  private bool isValidName;
72  public bool IsValidName
73  {
74  get { return isValidName; }
75  set { SetValue(ref isValidName, value, "IsValidName"); }
76  }
77 
78  public bool CreateXmlNodes(XmlDocument doc, out XmlNode sectionNode, out XmlNode definitionNode)
79  {
80  sectionNode = null;
81  definitionNode = null;
82 
83  if (IsUsed == false || string.IsNullOrWhiteSpace(Name))
84  return false;
85 
86  try
87  {
88  // ---------------------------------------------------------------------------------
89  var localSectionNode = doc.CreateElement("section");
90 
91  var attr = doc.CreateAttribute("name");
92  attr.Value = Name;
93  localSectionNode.Attributes.Append(attr);
94 
95  attr = doc.CreateAttribute("type");
96  attr.Value = Section.AssemblyQualifiedName;
97  localSectionNode.Attributes.Append(attr);
98  // ---------------------------------------------------------------------------------
99 
100  // ---------------------------------------------------------------------------------
101  var localDefinitionNode = doc.CreateElement(Name);
102  foreach (var property in workingProperties)
103  {
104  if (property.IsUsed == false)
105  continue;
106  attr = doc.CreateAttribute(property.Attribute.Name);
107  attr.Value = property.Value != null ? property.Value.ToString() : "";
108  localDefinitionNode.Attributes.Append(attr);
109  }
110  // ---------------------------------------------------------------------------------
111 
112  sectionNode = localSectionNode;
113  definitionNode = localDefinitionNode;
114  }
115  catch
116  {
117  return false;
118  }
119 
120  return true;
121  }
122  }
123 }
bool CreateXmlNodes(XmlDocument doc, out XmlNode sectionNode, out XmlNode definitionNode)
This abstract class represents a basic view model, implementing INotifyPropertyChanging and INotifyPr...