PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Model/System.cs

https://github.com/shader/QuickArch
C# | 139 lines | 119 code | 19 blank | 1 comment | 15 complexity | ad5ce0f78dfe6c0260af010d40b69c91 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.Linq;
  6. using System.Text;
  7. using System.IO;
  8. using System.Xml.Linq;
  9. using QuickArch.ViewModel;
  10. using QuickArch.Utilities;
  11. using QuickArch.Properties;
  12. namespace QuickArch.Model
  13. {
  14. public class System : Component
  15. {
  16. private List<Component> _components;
  17. //raised when a Component is added to the construct
  18. public event EventHandler<NotifyCollectionChangedEventArgs> ComponentsChanged;
  19. public List<Component> Components
  20. {
  21. get { return _components; }
  22. set { _components = value; }
  23. }
  24. #region Constructors
  25. public System(String filename) : this(XElement.Load(filename))
  26. {
  27. Filename = filename;
  28. }
  29. public System(XElement source) : this(source, null) { }
  30. public System(XElement source, System parent)
  31. : this(source.Attribute("Title") != null ? source.Attribute("Title").Value : Resources.DefaultFilename,
  32. null)
  33. {
  34. foreach (var child in source.Elements())
  35. {
  36. if (child.Name.LocalName == Resources.SubsystemTagName)
  37. {
  38. AddComponent(new System(child, this));
  39. }
  40. else if (child.Name.LocalName == Resources.SequenceTagName)
  41. {
  42. AddComponent(new Sequence(child, this));
  43. }
  44. }
  45. }
  46. public System(String title, System parent) : base(title, parent)
  47. {
  48. _components = new List<Component>();
  49. }
  50. #endregion
  51. public Component AddComponent(Component comp)
  52. {
  53. if(!_components.Contains(comp))
  54. {
  55. _components.Add(comp);
  56. if(this.ComponentsChanged != null)
  57. this.ComponentsChanged(this,
  58. new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, comp));
  59. }
  60. return comp;
  61. }
  62. public void RemoveComponent(Component comp)
  63. {
  64. _components.Remove(comp);
  65. if(this.ComponentsChanged != null)
  66. this.ComponentsChanged(this,
  67. new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, comp));
  68. }
  69. public void AddConnector(System start, System end)
  70. {
  71. AddComponent(new Connector(start, end));
  72. }
  73. public void AddSubsystem(string title)
  74. {
  75. AddComponent(new System(title, this));
  76. }
  77. public SystemDiagram AddSystemDiagram(string title, Document parent)
  78. {
  79. return AddComponent(new SystemDiagram(title, parent)) as SystemDiagram;
  80. }
  81. public override void Save()
  82. {
  83. if ( Filename != null)
  84. {
  85. XElement doc = new XElement(Resources.SystemTagName,
  86. new XAttribute("Title", Name));
  87. foreach ( Component cmp in _components )
  88. {
  89. cmp.Save(doc); //add each component recursively to the save tree.
  90. }
  91. doc.Save(Filename); //write to file
  92. }
  93. }
  94. public override void Save(XElement parent)
  95. {
  96. if (Filename != null)
  97. {
  98. parent.Add(new XElement(Resources.SubsystemTagName,
  99. new XAttribute("Title", Name),
  100. new XAttribute("Filename", Filename)));
  101. Save();
  102. }
  103. else
  104. {
  105. XElement elm = new XElement(Resources.SubsystemTagName,
  106. new XAttribute("Title", Name));
  107. parent.Add(elm);
  108. foreach (Component cmp in _components)
  109. {
  110. cmp.Save(elm); //add each component recursively to the save tree.
  111. }
  112. }
  113. }
  114. public void Open(string filename)
  115. {
  116. FileStream stream = File.OpenRead(Filename = filename);
  117. XElement.Load(stream);
  118. }
  119. }
  120. }