Index: src/Common/DelftTools.Utils/PropertyBag/PropertyBag.cs
===================================================================
diff -u -r18f9b18cab9da0c768badb3084415e993a5414ee -r5fc71a385897af92ccb092f2f969b5709afab85a
--- src/Common/DelftTools.Utils/PropertyBag/PropertyBag.cs (.../PropertyBag.cs) (revision 18f9b18cab9da0c768badb3084415e993a5414ee)
+++ src/Common/DelftTools.Utils/PropertyBag/PropertyBag.cs (.../PropertyBag.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a)
@@ -20,14 +20,68 @@
///
public class PropertyBag : ICustomTypeDescriptor
{
+ ///
+ /// Occurs when a PropertyGrid requests the value of a property.
+ ///
+ public event PropertySpecEventHandler GetValue;
+
+ ///
+ /// Occurs when the user changes the value of a property in a PropertyGrid.
+ ///
+ public event PropertySpecEventHandler SetValue;
+
+ ///
+ /// Initializes a new instance of the PropertyBag class.
+ ///
+ public PropertyBag()
+ {
+ DefaultProperty = null;
+ Properties = new PropertySpecCollection();
+ }
+
+ ///
+ /// Gets or sets the name of the default property in the collection.
+ ///
+ public string DefaultProperty { get; set; }
+
+ ///
+ /// Gets the collection of properties contained within this PropertyBag.
+ ///
+ public PropertySpecCollection Properties { get; private set; }
+
+ ///
+ /// Raises the GetValue event.
+ ///
+ /// A PropertySpecEventArgs that contains the event data.
+ protected virtual void OnGetValue(PropertySpecEventArgs e)
+ {
+ if (GetValue != null)
+ {
+ GetValue(this, e);
+ }
+ }
+
+ ///
+ /// Raises the SetValue event.
+ ///
+ /// A PropertySpecEventArgs that contains the event data.
+ protected virtual void OnSetValue(PropertySpecEventArgs e)
+ {
+ if (SetValue != null)
+ {
+ SetValue(this, e);
+ }
+ }
+
#region PropertySpecCollection class definition
+
///
/// Encapsulates a collection of PropertySpec objects.
///
[Serializable]
public class PropertySpecCollection : IList
{
- private ArrayList innerArray;
+ private readonly ArrayList innerArray;
///
/// Initializes a new instance of the PropertySpecCollection class.
@@ -38,14 +92,37 @@
}
///
+ /// Gets or sets the element at the specified index.
+ /// In C#, this property is the indexer for the PropertySpecCollection class.
+ ///
+ /// The zero-based index of the element to get or set.
+ ///
+ /// The element at the specified index.
+ ///
+ public PropertySpec this[int index]
+ {
+ get
+ {
+ return (PropertySpec) innerArray[index];
+ }
+ set
+ {
+ innerArray[index] = value;
+ }
+ }
+
+ ///
/// Gets the number of elements in the PropertySpecCollection.
///
///
/// The number of elements contained in the PropertySpecCollection.
///
public int Count
{
- get { return innerArray.Count; }
+ get
+ {
+ return innerArray.Count;
+ }
}
///
@@ -56,15 +133,21 @@
///
public bool IsFixedSize
{
- get { return false; }
+ get
+ {
+ return false;
+ }
}
///
/// Gets a value indicating whether the PropertySpecCollection is read-only.
///
public bool IsReadOnly
{
- get { return false; }
+ get
+ {
+ return false;
+ }
}
///
@@ -75,7 +158,10 @@
///
public bool IsSynchronized
{
- get { return false; }
+ get
+ {
+ return false;
+ }
}
///
@@ -86,24 +172,13 @@
///
object ICollection.SyncRoot
{
- get { return null; }
+ get
+ {
+ return null;
+ }
}
///
- /// Gets or sets the element at the specified index.
- /// In C#, this property is the indexer for the PropertySpecCollection class.
- ///
- /// The zero-based index of the element to get or set.
- ///
- /// The element at the specified index.
- ///
- public PropertySpec this[int index]
- {
- get { return (PropertySpec)innerArray[index]; }
- set { innerArray[index] = value; }
- }
-
- ///
/// Adds a PropertySpec to the end of the PropertySpecCollection.
///
/// The PropertySpec to be added to the end of the PropertySpecCollection.
@@ -126,14 +201,6 @@
}
///
- /// Removes all elements from the PropertySpecCollection.
- ///
- public void Clear()
- {
- innerArray.Clear();
- }
-
- ///
/// Determines whether a PropertySpec is in the PropertySpecCollection.
///
/// The PropertySpec to locate in the PropertySpecCollection. The element to locate
@@ -152,8 +219,12 @@
public bool Contains(string name)
{
foreach (PropertySpec spec in innerArray)
+ {
if (spec.Name == name)
+ {
return true;
+ }
+ }
return false;
}
@@ -181,15 +252,6 @@
}
///
- /// Returns an enumerator that can iterate through the PropertySpecCollection.
- ///
- /// An IEnumerator for the entire PropertySpecCollection.
- public IEnumerator GetEnumerator()
- {
- return innerArray.GetEnumerator();
- }
-
- ///
/// Searches for the specified PropertySpec and returns the zero-based index of the first
/// occurrence within the entire PropertySpecCollection.
///
@@ -215,7 +277,9 @@
foreach (PropertySpec spec in innerArray)
{
if (spec.Name == name)
+ {
return i;
+ }
i++;
}
@@ -253,46 +317,64 @@
}
///
- /// Removes the object at the specified index of the PropertySpecCollection.
+ /// Copies the elements of the PropertySpecCollection to a new PropertySpec array.
///
- /// The zero-based index of the element to remove.
- public void RemoveAt(int index)
+ /// A PropertySpec array containing copies of the elements of the PropertySpecCollection.
+ public PropertySpec[] ToArray()
{
- innerArray.RemoveAt(index);
+ return (PropertySpec[]) innerArray.ToArray(typeof(PropertySpec));
}
///
- /// Copies the elements of the PropertySpecCollection to a new PropertySpec array.
+ /// Removes all elements from the PropertySpecCollection.
///
- /// A PropertySpec array containing copies of the elements of the PropertySpecCollection.
- public PropertySpec[] ToArray()
+ public void Clear()
{
- return (PropertySpec[])innerArray.ToArray(typeof(PropertySpec));
+ innerArray.Clear();
}
+ ///
+ /// Returns an enumerator that can iterate through the PropertySpecCollection.
+ ///
+ /// An IEnumerator for the entire PropertySpecCollection.
+ public IEnumerator GetEnumerator()
+ {
+ return innerArray.GetEnumerator();
+ }
+
+ ///
+ /// Removes the object at the specified index of the PropertySpecCollection.
+ ///
+ /// The zero-based index of the element to remove.
+ public void RemoveAt(int index)
+ {
+ innerArray.RemoveAt(index);
+ }
+
#region Explicit interface implementations for ICollection and IList
+
///
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
///
void ICollection.CopyTo(Array array, int index)
{
- CopyTo((PropertySpec[])array, index);
+ CopyTo((PropertySpec[]) array, index);
}
///
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
///
int IList.Add(object value)
{
- return Add((PropertySpec)value);
+ return Add((PropertySpec) value);
}
///
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
///
bool IList.Contains(object obj)
{
- return Contains((PropertySpec)obj);
+ return Contains((PropertySpec) obj);
}
///
@@ -302,11 +384,11 @@
{
get
{
- return ((PropertySpecCollection)this)[index];
+ return ((PropertySpecCollection) this)[index];
}
set
{
- ((PropertySpecCollection)this)[index] = (PropertySpec)value;
+ ((PropertySpecCollection) this)[index] = (PropertySpec) value;
}
}
@@ -315,32 +397,36 @@
///
int IList.IndexOf(object obj)
{
- return IndexOf((PropertySpec)obj);
+ return IndexOf((PropertySpec) obj);
}
///
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
///
void IList.Insert(int index, object value)
{
- Insert(index, (PropertySpec)value);
+ Insert(index, (PropertySpec) value);
}
///
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
///
void IList.Remove(object value)
{
- Remove((PropertySpec)value);
+ Remove((PropertySpec) value);
}
+
#endregion
}
+
#endregion
+
#region PropertySpecDescriptor class definition
+
private class PropertySpecDescriptor : PropertyDescriptor
{
- private PropertyBag bag;
- private PropertySpec item;
+ private readonly PropertyBag bag;
+ private readonly PropertySpec item;
public PropertySpecDescriptor(PropertySpec item, PropertyBag bag, string name, Attribute[] attrs)
:
@@ -352,12 +438,18 @@
public override Type ComponentType
{
- get { return item.GetType(); }
+ get
+ {
+ return item.GetType();
+ }
}
public override bool IsReadOnly
{
- get { return (Attributes.Matches(ReadOnlyAttribute.Yes)); }
+ get
+ {
+ return (Attributes.Matches(ReadOnlyAttribute.Yes));
+ }
}
public override bool IsBrowsable
@@ -371,15 +463,22 @@
public override Type PropertyType
{
- get { return Type.GetType(item.TypeName); }
+ get
+ {
+ return Type.GetType(item.TypeName);
+ }
}
public override bool CanResetValue(object component)
{
if (item.DefaultValue == null)
+ {
return false;
+ }
else
- return !this.GetValue(component).Equals(item.DefaultValue);
+ {
+ return !GetValue(component).Equals(item.DefaultValue);
+ }
}
public override object GetValue(object component)
@@ -403,12 +502,16 @@
public override bool ShouldSerializeValue(object component)
{
- object val = this.GetValue(component);
+ object val = GetValue(component);
if (item.DefaultValue == null && val == null)
+ {
return false;
+ }
else
+ {
return !val.Equals(item.DefaultValue);
+ }
}
private PropertySpecEventArgs ReEvaluateAttributes()
@@ -417,72 +520,15 @@
// of the property and evaluate the dynamic attributes
var e = new PropertySpecEventArgs(item, null);
bag.OnGetValue(e);
- base.AttributeArray = e.Property.Attributes;
+ AttributeArray = e.Property.Attributes;
return e;
}
}
+
#endregion
- private string defaultProperty;
- private PropertySpecCollection properties;
-
- ///
- /// Initializes a new instance of the PropertyBag class.
- ///
- public PropertyBag()
- {
- defaultProperty = null;
- properties = new PropertySpecCollection();
- }
-
- ///
- /// Gets or sets the name of the default property in the collection.
- ///
- public string DefaultProperty
- {
- get { return defaultProperty; }
- set { defaultProperty = value; }
- }
-
- ///
- /// Gets the collection of properties contained within this PropertyBag.
- ///
- public PropertySpecCollection Properties
- {
- get { return properties; }
- }
-
- ///
- /// Occurs when a PropertyGrid requests the value of a property.
- ///
- public event PropertySpecEventHandler GetValue;
-
- ///
- /// Occurs when the user changes the value of a property in a PropertyGrid.
- ///
- public event PropertySpecEventHandler SetValue;
-
- ///
- /// Raises the GetValue event.
- ///
- /// A PropertySpecEventArgs that contains the event data.
- protected virtual void OnGetValue(PropertySpecEventArgs e)
- {
- if (GetValue != null)
- GetValue(this, e);
- }
-
- ///
- /// Raises the SetValue event.
- ///
- /// A PropertySpecEventArgs that contains the event data.
- protected virtual void OnSetValue(PropertySpecEventArgs e)
- {
- if (SetValue != null)
- SetValue(this, e);
- }
-
#region ICustomTypeDescriptor explicit interface definitions
+
// Most of the functions required by the ICustomTypeDescriptor are
// merely pssed on to the default TypeDescriptor for this type,
// which will do something appropriate. The exceptions are noted
@@ -520,22 +566,26 @@
// found that matches DefaultProperty, a null reference is
// returned instead.
- if(defaultProperty == null && properties.Count != 0)
+ if (DefaultProperty == null && Properties.Count != 0)
{
- defaultProperty = properties[0].Name;
+ DefaultProperty = Properties[0].Name;
}
PropertySpec propertySpec = null;
- if (defaultProperty != null)
+ if (DefaultProperty != null)
{
- int index = properties.IndexOf(defaultProperty);
- propertySpec = properties[index];
+ int index = Properties.IndexOf(DefaultProperty);
+ propertySpec = Properties[index];
}
if (propertySpec != null)
+ {
return new PropertySpecDescriptor(propertySpec, this, propertySpec.Name, null);
+ }
else
+ {
return null;
+ }
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
@@ -555,7 +605,7 @@
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
- return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
+ return ((ICustomTypeDescriptor) this).GetProperties(new Attribute[0]);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
@@ -567,34 +617,44 @@
var props = new List();
var propsToOrder = new List>();
- foreach (PropertySpec property in properties)
+ foreach (PropertySpec property in Properties)
{
var attrs = new ArrayList();
// If a category, description, editor, or type converter are specified
// in the PropertySpec, create attributes to define that relationship.
if (property.Category != null)
+ {
attrs.Add(new CategoryAttribute(property.Category));
+ }
if (property.Description != null)
+ {
attrs.Add(new DescriptionAttribute(property.Description));
+ }
if (property.EditorTypeName != null)
+ {
attrs.Add(new EditorAttribute(property.EditorTypeName, typeof(UITypeEditor)));
+ }
if (property.ConverterTypeName != null)
+ {
attrs.Add(new TypeConverterAttribute(property.ConverterTypeName));
+ }
// Additionally, append the custom attributes associated with the
// PropertySpec, if any.
if (property.Attributes != null)
+ {
attrs.AddRange(property.Attributes);
+ }
- Attribute[] attrArray = (Attribute[])attrs.ToArray(typeof(Attribute));
+ Attribute[] attrArray = (Attribute[]) attrs.ToArray(typeof(Attribute));
// Create a new property descriptor for the property item, and add
// it to the list.
- var pd = new PropertySpecDescriptor(property,this, property.Name, attrArray);
+ var pd = new PropertySpecDescriptor(property, this, property.Name, attrArray);
var propertyOrderAttribute = property.Attributes != null ? property.Attributes.OfType().FirstOrDefault() : null;
if (propertyOrderAttribute != null)
@@ -608,14 +668,14 @@
}
var orderedProperties = propsToOrder.OrderBy(p => p.First).Select(p => p.Second).ToList();
-
+
// Convert the list of PropertyDescriptors to a collection that the
// ICustomTypeDescriptor can use, and return it.
var browsableAttribute = attributes.OfType().FirstOrDefault();
var propertySpecDescriptors = (browsableAttribute != null)
- ? orderedProperties.Concat(props).Where(p => p.IsBrowsable == browsableAttribute.Browsable)
- : orderedProperties.Concat(props);
+ ? orderedProperties.Concat(props).Where(p => p.IsBrowsable == browsableAttribute.Browsable)
+ : orderedProperties.Concat(props);
return new PropertyDescriptorCollection(propertySpecDescriptors.ToArray());
}
@@ -624,6 +684,7 @@
{
return this;
}
+
#endregion
}
}
\ No newline at end of file