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

Grandiose Projects 3. Compatibility with Simulink

Rate me:
Please Sign up or sign in to vote.
4.27/5 (11 votes)
8 Feb 2010CPOL23 min read 47.8K   5.9K   38  
Import of Simulink files
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;

namespace DiagramUI.PropretyGridHelpers
{
    /// <summary>
    /// Table of double dictionary
    /// </summary>
    public class DictionaryTable<T> : List<DictionaryItem<T>>, ICustomTypeDescriptor
    {
        /// <summary>
        /// Sets dictionary to property grid
        /// </summary>
        /// <param name="dictionary">Dictionary</param>
        /// <param name="grid">Grid</param>
        static public void Set(IDictionary<string, T> dictionary, PropertyGrid grid)
        {
            grid.SelectedObject = new DictionaryTable<T>(dictionary);
        }

        private DictionaryTable(IDictionary<string, T> dictionary)
        {
            List<string> l = new List<string>();
            l.AddRange(dictionary.Keys);
            l.Sort();
            foreach (string key in dictionary.Keys)
            {
                Add(new DictionaryItem<T>(key, dictionary));
            }
        }



        #region ICustomTypeDescriptor Members

        AttributeCollection ICustomTypeDescriptor.GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        string ICustomTypeDescriptor.GetClassName()
        {
            string cn = TypeDescriptor.GetClassName(this, true);
            return cn;
        }

        string ICustomTypeDescriptor.GetComponentName()
        {
            string cn = TypeDescriptor.GetComponentName(this, true);
            return cn;
        }

        TypeConverter ICustomTypeDescriptor.GetConverter()
        {
            TypeConverter tc = TypeDescriptor.GetConverter(this, true);
            return tc;
        }

        EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
        {
            EventDescriptor ed = TypeDescriptor.GetDefaultEvent(this, true);
            return ed;
        }

        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
        {
            object o = TypeDescriptor.GetEditor(this, editorBaseType, true);
            return o;
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
        {
            EventDescriptorCollection edc = TypeDescriptor.GetEvents(this, attributes, true);
            return edc;
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
        {
            EventDescriptorCollection edc = TypeDescriptor.GetEvents(this, true);
            return edc;
        }

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            ICustomTypeDescriptor d = this;
            return d.GetProperties();
        }

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
        {
            PropertyDescriptorCollection coll = new PropertyDescriptorCollection(null);
            for (int i = 0; i < Count; i++)
            {
                coll.Add(new DictionaryItemDescriptor<T>(this[i]));
            }
            return coll;
        }

        object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion
    }

    /// <summary>
    /// Item of double dictionary table
    /// </summary>
    //[TypeConverter(typeof(DictionaryConverter))]
    public class DictionaryItem<T>
    {
        #region Fields

        string name;

        IDictionary<string, T> dictionary;

        #endregion


        #region Ctor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name">Item name</param>
        /// <param name="dictionary">Dictionary of items</param>
        internal DictionaryItem(string name, IDictionary<string, T> dictionary)
        {
            this.name = name;
            this.dictionary = dictionary;
        }

        #endregion


        #region Members

        /// <summary>
        /// Name
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
        }

        /// <summary>
        /// Value
        /// </summary>
        public T Value
        {
            get
            {
                return dictionary[name];
            }
            set
            {
                dictionary[name] = value;
            }
        }

        internal Type Type
        {
            get
            {
                return typeof(T);
            }
        }

        #endregion

    }


    class DictionaryItemDescriptor<T> : PropertyDescriptor
    {
        DictionaryItem<T> item;

        internal DictionaryItemDescriptor(DictionaryItem<T> item)
            : base(item.Name, null)
        {
            this.item = item;

        }

        public override bool CanResetValue(object component)
        {
            return false;
        }

        public override Type ComponentType
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override object GetValue(object component)
        {
            return item.Value;
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override Type PropertyType
        {
            get
            {
                return typeof(T);
            }
        }

        public override void ResetValue(object component)
        {
        }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }


        public override void SetValue(object component, object value)
        {
            item.Value = (T)value;
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions