SvgElementCollection.cs 6.89 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
using System;
using System.Collections.Generic;
3
using System.Linq;
davescriven's avatar
davescriven committed
4
5
6
7
8
9
10
11
12
13
14
using System.Text;

namespace Svg
{
    /// <summary>
    /// Represents a collection of <see cref="SvgElement"/>s.
    /// </summary>
    public sealed class SvgElementCollection : IList<SvgElement>
    {
        private List<SvgElement> _elements;
        private SvgElement _owner;
15
        private bool _mock;
davescriven's avatar
davescriven committed
16
17
18
19
20
21

        /// <summary>
        /// Initialises a new instance of an <see cref="SvgElementCollection"/> class.
        /// </summary>
        /// <param name="owner">The owner <see cref="SvgElement"/> of the collection.</param>
        internal SvgElementCollection(SvgElement owner)
22
23
24
25
26
27
            : this(owner, false)
        {

        }

        internal SvgElementCollection(SvgElement owner, bool mock)
davescriven's avatar
davescriven committed
28
29
30
31
32
33
34
35
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            this._elements = new List<SvgElement>();
            this._owner = owner;
36
            this._mock = mock;
davescriven's avatar
davescriven committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        }

        /// <summary>
        /// Returns the index of the specified <see cref="SvgElement"/> in the collection.
        /// </summary>
        /// <param name="item">The <see cref="SvgElement"/> to search for.</param>
        /// <returns>The index of the element if it is present; otherwise -1.</returns>
        public int IndexOf(SvgElement item)
        {
            return this._elements.IndexOf(item);
        }

        /// <summary>
        /// Inserts the given <see cref="SvgElement"/> to the collection at the specified index.
        /// </summary>
        /// <param name="index">The index that the <paramref name="item"/> should be added at.</param>
        /// <param name="item">The <see cref="SvgElement"/> to be added.</param>
        public void Insert(int index, SvgElement item)
        {
56
57
58
59
60
61
            InsertAndForceUniqueID(index, item, true, true, LogIDChange);
        }
        
        private void LogIDChange(SvgElement elem, string oldId, string newID)
        {
        	Console.WriteLine("ID of SVG element " + elem.ToString() + " changed from " + oldId + " to " + newID);
62
63
        }

tebjan's avatar
tebjan committed
64
        public void InsertAndForceUniqueID(int index, SvgElement item, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action<SvgElement, string, string> logElementOldIDNewID = null)
65
        {
tebjan's avatar
tebjan committed
66
            AddToIdManager(item, this._elements[index], autoForceUniqueID, autoFixChildrenID, logElementOldIDNewID);
davescriven's avatar
davescriven committed
67
            this._elements.Insert(index, item);
68
            item._parent.OnElementAdded(item, index);
davescriven's avatar
davescriven committed
69
70
71
72
73
74
75
        }

        public void RemoveAt(int index)
        {
            SvgElement element = this[index];

            if (element != null)
76
            {
davescriven's avatar
davescriven committed
77
                this.Remove(element);
78
            }
davescriven's avatar
davescriven committed
79
80
81
82
83
84
85
86
87
        }

        public SvgElement this[int index]
        {
            get { return this._elements[index]; }
            set { this._elements[index] = value; }
        }

        public void Add(SvgElement item)
88
        {
89
            this.AddAndForceUniqueID(item, true, true, LogIDChange);
90
91
        }

tebjan's avatar
tebjan committed
92
        public void AddAndForceUniqueID(SvgElement item, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action<SvgElement, string, string> logElementOldIDNewID = null)
93
        {
tebjan's avatar
tebjan committed
94
            AddToIdManager(item, null, autoForceUniqueID, autoFixChildrenID, logElementOldIDNewID);
95
96
97
98
            this._elements.Add(item);
            item._parent.OnElementAdded(item, this.Count - 1);
        }

tebjan's avatar
tebjan committed
99
        private void AddToIdManager(SvgElement item, SvgElement sibling, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action<SvgElement, string, string> logElementOldIDNewID = null)
davescriven's avatar
davescriven committed
100
        {
101
            if (!this._mock)
davescriven's avatar
davescriven committed
102
            {
103
104
            	if (this._owner.OwnerDocument != null)
            	{
tebjan's avatar
tebjan committed
105
                    this._owner.OwnerDocument.IdManager.AddAndForceUniqueID(item, sibling, autoForceUniqueID, logElementOldIDNewID);
106
107
108
109
110

                    if (!(item is SvgDocument)) //don't add subtree of a document to parent document
                    {
                        foreach (var child in item.Children)
                        {
tebjan's avatar
tebjan committed
111
                            child.ApplyRecursive(e => this._owner.OwnerDocument.IdManager.AddAndForceUniqueID(e, null, autoFixChildrenID, logElementOldIDNewID));
112
113
114
                        }
                    }
                }
tebjan's avatar
tebjan committed
115
116
                
                //if all checked, set parent
117
                item._parent = this._owner;
davescriven's avatar
davescriven committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
            }
        }

        public void Clear()
        {
            while (this.Count > 0)
            {
                SvgElement element = this[0];
                this.Remove(element);
            }
        }

        public bool Contains(SvgElement item)
        {
            return this._elements.Contains(item);
        }

        public void CopyTo(SvgElement[] array, int arrayIndex)
        {
            this._elements.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this._elements.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(SvgElement item)
        {
            bool removed = this._elements.Remove(item);

            if (removed)
            {
                this._owner.OnElementRemoved(item);

158
                if (!this._mock)
davescriven's avatar
davescriven committed
159
                {
160
161
162
163
                    item._parent = null;

                    if (this._owner.OwnerDocument != null)
                    {
164
                        item.ApplyRecursiveDepthFirst(this._owner.OwnerDocument.IdManager.Remove);
165
                    }
davescriven's avatar
davescriven committed
166
167
168
169
170
171
                }
            }

            return removed;
        }

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		/// <summary>
		/// expensive recursive search for nodes of type T
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public IEnumerable<T> FindSvgElementsOf<T>() where T : SvgElement
		{
			return _elements.Where(x => x is T).Select(x => x as T).Concat(_elements.SelectMany(x => x.Children.FindSvgElementsOf<T>()));
		}

		/// <summary>
		/// expensive recursive search for first node of type T
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public T FindSvgElementOf<T>() where T : SvgElement
		{
			return _elements.OfType<T>().FirstOrDefault() ?? _elements.Select(x => x.Children.FindSvgElementOf<T>()).FirstOrDefault<T>(x => x != null);
		}

		public T GetSvgElementOf<T>() where T : SvgElement
		{
			return _elements.FirstOrDefault(x => x is T) as T;
		}


davescriven's avatar
davescriven committed
198
199
200
201
202
203
204
205
206
207
208
        public IEnumerator<SvgElement> GetEnumerator()
        {
            return this._elements.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this._elements.GetEnumerator();
        }
    }
}