SvgAttributeCollection.cs 7.97 KB
Newer Older
davescriven's avatar
davescriven committed
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Svg
{
    /// <summary>
    /// A collection of Scalable Vector Attributes that can be inherited from the owner elements ancestors.
    /// </summary>
    public sealed class SvgAttributeCollection : Dictionary<string, object>
    {
        private SvgElement _owner;

        /// <summary>
        /// Initialises a new instance of a <see cref="SvgAttributeCollection"/> with the given <see cref="SvgElement"/> as the owner.
        /// </summary>
        /// <param name="owner">The <see cref="SvgElement"/> owner of the collection.</param>
        public SvgAttributeCollection(SvgElement owner)
        {
            this._owner = owner;
        }

        /// <summary>
        /// Gets the attribute with the specified name.
        /// </summary>
        /// <typeparam name="TAttributeType">The type of the attribute value.</typeparam>
        /// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
        /// <returns>The attribute value if available; otherwise the default value of <typeparamref name="TAttributeType"/>.</returns>
        public TAttributeType GetAttribute<TAttributeType>(string attributeName)
        {
            if (this.ContainsKey(attributeName) && base[attributeName] != null)
            {
                return (TAttributeType)base[attributeName];
            }

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
            return this.GetAttribute<TAttributeType>(attributeName, default(TAttributeType));
        }

        /// <summary>
        /// Gets the attribute with the specified name.
        /// </summary>
        /// <typeparam name="T">The type of the attribute value.</typeparam>
        /// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
        /// <param name="defaultValue">The value to return if a value hasn't already been specified.</param>
        /// <returns>The attribute value if available; otherwise the default value of <typeparamref name="T"/>.</returns>
        public T GetAttribute<T>(string attributeName, T defaultValue)
        {
            if (this.ContainsKey(attributeName) && base[attributeName] != null)
            {
                return (T)base[attributeName];
            }

            return defaultValue;
davescriven's avatar
davescriven committed
56
57
58
59
60
61
62
63
64
65
        }

        /// <summary>
        /// Gets the attribute with the specified name and inherits from ancestors if there is no attribute set.
        /// </summary>
        /// <typeparam name="TAttributeType">The type of the attribute value.</typeparam>
        /// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
        /// <returns>The attribute value if available; otherwise the ancestors value for the same attribute; otherwise the default value of <typeparamref name="TAttributeType"/>.</returns>
        public TAttributeType GetInheritedAttribute<TAttributeType>(string attributeName)
        {
Tebjan Halm's avatar
Tebjan Halm committed
66
            if (this.ContainsKey(attributeName) /*&& base[attributeName] != null*/)
davescriven's avatar
davescriven committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
            {
                return (TAttributeType)base[attributeName];
            }

            if (this._owner.Parent != null)
            {
                if (this._owner.Parent.Attributes[attributeName] != null)
                {
                    return (TAttributeType)this._owner.Parent.Attributes[attributeName];
                }
            }

            return default(TAttributeType);
        }

        /// <summary>
        /// Gets the attribute with the specified name.
        /// </summary>
        /// <param name="attributeName">A <see cref="string"/> containing the attribute name.</param>
        /// <returns>The attribute value associated with the specified name; If there is no attribute the parent's value will be inherited.</returns>
        public new object this[string attributeName]
        {
            get { return this.GetInheritedAttribute<object>(attributeName); }
90
91
92
93
            set 
            {
            	if(base.ContainsKey(attributeName))
            	{
94
95
96
97
98
99
	            	var oldVal = base[attributeName];	            	
	            	if(TryUnboxAndCheck(oldVal, value))
	            	{
	            		base[attributeName] = value;
	            		OnAttributeChanged(attributeName, value);
	            	}
100
101
102
103
104
105
106
107
108
            	}
            	else
            	{
            		base[attributeName] = value;
	            	OnAttributeChanged(attributeName, value);
            	}
            }
        }
        
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
140
141
142
        private bool TryUnboxAndCheck(object a, object b)
        {
        	System.Diagnostics.Debug.WriteLine("object type: " + a.GetType().ToString());
        	if(IsValueType(a))
        	{
        		System.Diagnostics.Debug.WriteLine("is value type");
        		
        		if(a is SvgUnit)
        			return UnboxAndCheck<SvgUnit>(a, b);
        		else if(a is bool)
        			return UnboxAndCheck<bool>(a, b);
        		else if(a is int)
        			return UnboxAndCheck<int>(a, b);
        		else if(a is float)
        			return UnboxAndCheck<float>(a, b);
        		else
        			return true;
        	}
        	else
        	{
        		return a != b;
        	}
        }
        
        private bool UnboxAndCheck<T>(object a, object b)
        {
        	return !((T)a).Equals((T)b);
        }
        
        private bool IsValueType(object obj) 
        {
        	return obj != null && obj.GetType().IsValueType;
        }
        
143
144
145
146
147
        /// <summary>
        /// Fired when an Atrribute has changed
        /// </summary>
        public event EventHandler<AttributeEventArgs> AttributeChanged;
        
148
        private void OnAttributeChanged(string attribute, object value)
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
198
199
200
201
202
203
        {
        	var handler = AttributeChanged;
        	if(handler != null)
        	{
        		handler(this._owner, new AttributeEventArgs { Attribute = attribute, Value = value });
        	}
        }
    }
    
    
    /// <summary>
    /// A collection of Custom Attributes
    /// </summary>
    public sealed class SvgCustomAttributeCollection : Dictionary<string, string>
    {
        private SvgElement _owner;

        /// <summary>
        /// Initialises a new instance of a <see cref="SvgAttributeCollection"/> with the given <see cref="SvgElement"/> as the owner.
        /// </summary>
        /// <param name="owner">The <see cref="SvgElement"/> owner of the collection.</param>
        public SvgCustomAttributeCollection(SvgElement owner)
        {
            this._owner = owner;
        }

        /// <summary>
        /// Gets the attribute with the specified name.
        /// </summary>
        /// <param name="attributeName">A <see cref="string"/> containing the attribute name.</param>
        /// <returns>The attribute value associated with the specified name; If there is no attribute the parent's value will be inherited.</returns>
        public new string this[string attributeName]
        {
        	get { return base[attributeName]; }
            set 
            {
            	if(base.ContainsKey(attributeName))
            	{
	            	var oldVal = base[attributeName];
	            	base[attributeName] = value;
	            	if(oldVal != value) OnAttributeChanged(attributeName, value);
            	}
            	else
            	{
            		base[attributeName] = value;
	            	OnAttributeChanged(attributeName, value);
            	}
            }
        }
        
        /// <summary>
        /// Fired when an Atrribute has changed
        /// </summary>
        public event EventHandler<AttributeEventArgs> AttributeChanged;
        
204
        private void OnAttributeChanged(string attribute, object value)
205
206
207
208
209
210
        {
        	var handler = AttributeChanged;
        	if(handler != null)
        	{
        		handler(this._owner, new AttributeEventArgs { Attribute = attribute, Value = value });
        	}
davescriven's avatar
davescriven committed
211
212
213
        }
    }
}