Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C#

PropertyGrid

Rate me:
Please Sign up or sign in to vote.
3.22/5 (3 votes)
3 Nov 20069 min read 52.2K   1.3K   30  
A control which provides a convenient way to display data in a property grid.
using System;

namespace Puma.Xml
{

	public enum XsdSimpleTypeRestriction
	{
		MinLength = 0x1,
		MaxLength = 0x10,
		MinValue = 0x100,
		MaxValue = 0x1000,
		Enumeration = 0x10000,
		All = 0x11111
	}

	public class XmlSimpleTypeRestriction
	{
		public XmlSimpleTypeRestriction(XsdSimpleType Container)
		{
			this.Container = Container;
		}

		public readonly XsdSimpleType Container;

		public System.Xml.Schema.XmlSchemaSimpleTypeRestriction Restriction { get {return Container.SimpleType == null ? null : Container.SimpleType.Content as System.Xml.Schema.XmlSchemaSimpleTypeRestriction;} }
		public System.Xml.Schema.XmlSchemaObjectCollection	Facets {get {return Restriction == null ? new System.Xml.Schema.XmlSchemaObjectCollection() :  Restriction.Facets;}}

		public bool IsEnum { get {return GetFirstRestrictionValue(XsdSimpleTypeRestriction.Enumeration) == null ? false : true;} }
		public bool IsMinValue{ get {return GetFirstRestrictionValue(XsdSimpleTypeRestriction.MinValue) == null ? false : true;} }
		public bool IsMaxValue{ get {return GetFirstRestrictionValue(XsdSimpleTypeRestriction.MaxValue) == null ? false : true;} }
        public bool IsMinLength { get { return GetFirstRestrictionValue(XsdSimpleTypeRestriction.MinLength) == null ? false : true; } }
        public bool IsMaxLength { get { return GetFirstRestrictionValue(XsdSimpleTypeRestriction.MaxLength) == null ? false : true; } }

		public int MinLengthRestriction
		{
			get
			{
				object val = GetFirstRestrictionValue(XsdSimpleTypeRestriction.MinLength);
				return val == null ? -1 : (int)val;
			}
		}
		
		public int MaxLengthRestriction
		{
			get
			{
				object val = GetFirstRestrictionValue(XsdSimpleTypeRestriction.MaxLength);

				return val == null ? -1 : (int)val;
			}
		}

		public EnumerationRestriction EnumerationRestriction
		{
			get
			{
				return GetRestriction(XsdSimpleTypeRestriction.Enumeration) as EnumerationRestriction;
			}
		}

		public object GetRestriction(XsdSimpleTypeRestriction TypeRestriction)
		{
			object [] restrs = GetRestrictions((int)TypeRestriction);	

			return restrs == null ? null : restrs[0];	
		}

		public object GetFirstRestrictionValue(XsdSimpleTypeRestriction TypeRestriction)
		{
            object[] objs = GetRestrictions((int)TypeRestriction, true);	

			return objs == null ? null : objs[0];	
		}

		public object[] GetRestrictions()
		{
			return GetRestrictions((int)XsdSimpleTypeRestriction.All);
		}

		public object[] GetRestrictions(int TypeRestrictions)
		{
			return GetRestrictions(TypeRestrictions, false);
		}

		private object GetRestrictionValueTyped(string Value, System.Type RestrictionType)
		{
			System.Reflection.MethodInfo methodInfo = RestrictionType.GetMethod("Parse", new System.Type[] {typeof(string)}); 

			System.Diagnostics.Debug.Assert(methodInfo != null);

			return methodInfo.Invoke(new object(), new string[] {Value});
		}

		private object[] GetRestrictions(int TypeRestrictions, bool fFindAndExit)
		{
			EnumerationRestriction enumeration = new EnumerationRestriction();

			MinMaxLengthRestriction minMaxLength = new MinMaxLengthRestriction();

			MinMaxValueRestriction minMaxValue = new MinMaxValueRestriction();

			bool fFind = false;

            object obj = null;

			foreach(System.Xml.Schema.XmlSchemaFacet xmlSchemaFacet in Facets)
			{
				if (((TypeRestrictions & (int)XsdSimpleTypeRestriction.MinLength) != 0) && xmlSchemaFacet is System.Xml.Schema.XmlSchemaMinLengthFacet)
				{
                    minMaxLength.SetMin(obj = GetRestrictionValueTyped(xmlSchemaFacet.Value, typeof(int)), xmlSchemaFacet);
				}

				if (((TypeRestrictions & (int)XsdSimpleTypeRestriction.MaxLength) != 0) && xmlSchemaFacet is System.Xml.Schema.XmlSchemaMaxLengthFacet)
				{
                    minMaxLength.SetMax(obj = GetRestrictionValueTyped(xmlSchemaFacet.Value, typeof(int)), xmlSchemaFacet);
				}
				
				if (((TypeRestrictions & (int)XsdSimpleTypeRestriction.MinValue) != 0) && xmlSchemaFacet is System.Xml.Schema.XmlSchemaMinInclusiveFacet)
				{
                    minMaxValue.SetMax(obj = GetRestrictionValueTyped(xmlSchemaFacet.Value, Container.ValueType), xmlSchemaFacet);
				}

				if (((TypeRestrictions & (int)XsdSimpleTypeRestriction.MaxValue) != 0) && xmlSchemaFacet is System.Xml.Schema.XmlSchemaMaxInclusiveFacet)
				{
                    minMaxValue.SetMax(obj = GetRestrictionValueTyped(xmlSchemaFacet.Value, Container.ValueType), xmlSchemaFacet);
				}
																												
				if (((TypeRestrictions & (int)XsdSimpleTypeRestriction.Enumeration) != 0) && (xmlSchemaFacet is System.Xml.Schema.XmlSchemaEnumerationFacet))
				{
                    enumeration.AddRestriction(obj = xmlSchemaFacet.Value, xmlSchemaFacet);														   
				}

                if (obj != null && fFindAndExit)
                {
                    return new object[] { obj };
                }
			}

			System.Collections.ArrayList al = new System.Collections.ArrayList();

			if (enumeration.IsContainAny)al.Add(enumeration);

			if (minMaxLength.IsContainAny)al.Add(minMaxLength);

			if (minMaxValue.IsContainAny)al.Add(minMaxValue);

			return al.Count == 0 ? null : (object[])al.ToArray(typeof(object));
		}



	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions