Click here to Skip to main content
15,886,795 members
Articles / Programming Languages / C#

Integration: Mechanics + Hydraulics + Navigation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (46 votes)
3 Feb 2011CPOL21 min read 61.6K   6.1K   88  
Sample of integration of branches of engineering.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
using System.IO;

namespace DiagramUI.Components
{
    /// <summary>
    /// Holder of desktop
    /// </summary>
    public partial class DesktopHolder : Component, ICustomTypeDescriptor
    {
        #region Fields

        /// <summary>
        /// Editor
        /// </summary>
        protected UITypeEditor editor;

        /// <summary>
        /// Content
        /// </summary>
        protected ByteHolder content = new ByteHolder();

        #endregion


        #region Ctor

        /// <summary>
        /// Default constructor
        /// </summary>
        public DesktopHolder()
        {
            InitializeComponent();
            editor = new StandardEditor(this);
        }

        /// <summary>
        /// Constructor with editor
        /// </summary>
        /// <param name="editor">The editor</param>
        protected DesktopHolder(UITypeEditor editor)
            : this()
        {
            this.editor = editor;
        }

        #endregion


        #region Members

        /// <summary>
        /// Saves its content to stream
        /// </summary>
        /// <param name="stream">The stream</param>
        public void Save(Stream stream)
        {
            byte[] b = content.Bytes;
            stream.Write(b, 0, b.Length);
        }

        /// <summary>
        /// Saves its content to file
        /// </summary>
        /// <param name="filename">File name</param>
        public void Save(string filename)
        {
            Stream stream = File.OpenWrite(filename);
            Save(stream);
            stream.Flush();
            stream.Close();
        }


        /// <summary>
        /// Content
        /// </summary>
        [Browsable(false)]
        [ReadOnly(false)]
        [Description("Control")]
        [TypeConverter(typeof(DesignerConverter))]
        public ByteHolder Content
        {
            get
            {
                return content;
            }
            set
            {
                if (value == null)
                {
                    throw new Exception("Empty content");
                }
                content = value;
            }
        }

        /// <summary>
        /// Desktop
        /// </summary>
        [Browsable(false)]
        public virtual PureDesktopPeer Desktop
        {
            get
            {
                return content.Desktop;
            }
        }

        object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Stream stream = File.OpenRead(openFileDialog.FileName);
                byte[] b = new byte[stream.Length];
                stream.Read(b, 0, b.Length);
                content.Bytes = b;
            }
            return content;
        }

        #endregion


        #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 c = TypeDescriptor.GetProperties(this, true);
            List<PropertyDescriptor> l = new List<PropertyDescriptor>();
            foreach (PropertyDescriptor pd in c)
            {
                if (pd.Name.Equals("Content") | pd.Equals("Content"))
                {
                    continue;
                }
                l.Add(pd);
            }
            l.Add(new ContentProprertyDescriptor(this));
            return new PropertyDescriptorCollection(l.ToArray());
        }

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

        #endregion


        class ContentProprertyDescriptor : PropertyDescriptor
        {
            DesktopHolder holder;

            internal ContentProprertyDescriptor(DesktopHolder holder) :
                base("Content", null)
            {
                this.holder = holder;
            }


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

            public override Type ComponentType
            {
                get { return typeof(Component); }
            }

            public override object GetValue(object component)
            {
                return holder.content;
            }

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

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

            public override void ResetValue(object component)
            {
            }

            public override void SetValue(object component, object value)
            {
                holder.content = value as ByteHolder;
            }

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

            public override object GetEditor(Type editorBaseType)
            {
                return holder.editor;
            }
        }

        class StandardEditor : UITypeEditor
        {
            private DesktopHolder holder;

            internal StandardEditor(DesktopHolder holder)
            {
                this.holder = holder;
            }

            public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                return holder.EditValue(context, provider, value);
            }

            public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }
        }

        class DesignerConverter : TypeConverter
        {
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }

        }
    }
}

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