PageRenderTime 22ms CodeModel.GetById 30ms app.highlight 8ms RepoModel.GetById 22ms app.codeStats 0ms

/Model/System.cs

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