QuickArch /Model/System.cs

Language C# Lines 140
MD5 Hash c5dbb538348c1594e6a79a602bfc0de1
Repository https://github.com/shader/QuickArch.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

using QuickArch.ViewModel;
using QuickArch.Utilities;
using QuickArch.Properties;

namespace QuickArch.Model
{
    public class System : Component
    {
        private List<Component> _components;
        
        //raised when a Component is added to the construct
        public event EventHandler<NotifyCollectionChangedEventArgs> ComponentsChanged;

        public List<Component> Components
        {
            get { return _components; }
            set { _components = value; }
        }

        #region Constructors
        public System(String filename) : this(XElement.Load(filename))
        {
            Filename = filename;
        }

        public System(XElement source) : this(source, null) { }
            

        public System(XElement source, System parent)
            : this(source.Attribute("Title") != null ? source.Attribute("Title").Value : Resources.DefaultFilename,
                   null)
        {
            foreach (var child in source.Elements())
            {
                if (child.Name.LocalName == Resources.SubsystemTagName)
                {
                    AddComponent(new System(child, this));
                }
                else if (child.Name.LocalName == Resources.SequenceTagName)
                {
                    AddComponent(new Sequence(child, this));
                }
            }
        }

        public System(String title, System parent) : base(title, parent)
        {
            _components = new List<Component>();
        }
        #endregion

        public Component AddComponent(Component comp)
        {
            if(!_components.Contains(comp))
            {
                _components.Add(comp);

                if(this.ComponentsChanged != null)
                    this.ComponentsChanged(this, 
                        new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, comp));
            }
            return comp;
        }

        public void RemoveComponent(Component comp)
        {
            _components.Remove(comp);

            if(this.ComponentsChanged != null)
                    this.ComponentsChanged(this, 
                        new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, comp));
        }

        public void AddConnector(System start, System end)
        {
            AddComponent(new Connector(start, end));
        }

        public void AddSubsystem(string title)
        {
            AddComponent(new System(title, this));
        }

        public SystemDiagram AddSystemDiagram(string title, Document parent)
        {
            return AddComponent(new SystemDiagram(title, parent)) as SystemDiagram;
        }

        public override void Save()
        {
            if ( Filename != null)
            {
                XElement doc = new XElement(Resources.SystemTagName,
                                            new XAttribute("Title", Name));
                foreach ( Component cmp in _components ) 
                {
                    cmp.Save(doc); //add each component recursively to the save tree.
                }
                doc.Save(Filename); //write to file
            }
        }

        public override void Save(XElement parent)
        {
            if (Filename != null)
            {
                parent.Add(new XElement(Resources.SubsystemTagName,
                                        new XAttribute("Title", Name),
                                        new XAttribute("Filename", Filename)));
                Save();
            }
            else
            {
                XElement elm = new XElement(Resources.SubsystemTagName,
                                            new XAttribute("Title", Name));
                parent.Add(elm);
                foreach (Component cmp in _components)
                {
                    cmp.Save(elm); //add each component recursively to the save tree.
                }
            }
        }
        
        public void Open(string filename)
        {
            FileStream stream = File.OpenRead(Filename = filename);
            XElement.Load(stream);
        }
    }
}
Back to Top