Click here to Skip to main content
15,895,011 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.8K   6.1K   88  
Sample of integration of branches of engineering.
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;

using CategoryTheory;
using MathGraph;
using DiagramUI.Labels;
using DiagramUI.Interfaces;

namespace DiagramUI
{
    /// <summary>
    /// Base conttainer of objects
    /// </summary>
    public abstract class ObjectContainerBase : CategoryObject, ISerializable, IObjectContainer, IPostSetArrow
    {
		#region Fields

		
		/// <summary>
		/// Byte information
		/// </summary>
		protected byte[] bytes;
		
		/// <summary>
		/// Interface
		/// </summary>
		protected Hashtable inter = new Hashtable();
		
		/// <summary>
		/// Type
		/// </summary>
		private string type;

		/// <summary>
		/// Serialization binders
		/// </summary>
		static private SerializationBinder[] binders;

		/// <summary>
		/// Child desktop
		/// </summary>
		protected PureDesktopPeer desktop = new PureDesktopPeer();

        /// <summary>
        /// The "is loaded" sign
        /// </summary>
        protected bool isLoaded = false;

        /// <summary>
        /// The "is post loaded" sign
        /// </summary>
        protected bool isPostLoaded = false;

		#endregion

		#region Constructors

		/// <summary>
		/// Main constructor
		/// </summary>
		/// <param name="desktop">The parent desktop</param>
		protected ObjectContainerBase(PureDesktopPeer desktop)
		{
			this.desktop = desktop;
		}

		/// <summary>
        /// Deserialization constructor
		/// </summary>
		/// <param name="info"></param>
		/// <param name="context"></param>
		public ObjectContainerBase(SerializationInfo info, StreamingContext context)
		{
			bytes = info.GetValue("Bytes", typeof(byte[])) as byte[];
			inter = info.GetValue("Interface", typeof(Hashtable)) as Hashtable;
			type = info.GetValue("Type", typeof(string)) as string;
		}

		#endregion

		#region ISerializable Members

        /// <summary>
        /// ISerializable interface implementation
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			MemoryStream stream = new MemoryStream();
            SaveDesktop(stream);
			bytes = stream.GetBuffer();
			info.AddValue("Bytes", bytes);
			info.AddValue("Interface", inter);
			info.AddValue("Type", type);
		}

		#endregion

        #region Abstract Members

        /// <summary>
        /// Saves desktop
        /// </summary>
        /// <param name="stream">Stream to save</param>
        protected abstract void SaveDesktop(Stream stream);

        /// <summary>
        /// Loads desktop
        /// </summary>
        /// <param name="bytes">Soure bytes</param>
        /// <returns>Thrue in success and false otherwise</returns>
        protected abstract bool LoadDesktop(byte[] bytes);


        #endregion

        #region IPostSetArrow Members

        /// <summary>
        /// The post set arrow operation
        /// </summary>
		public void PostSetArrow()
		{
		}

		#endregion

        #region IObjectCollection Members

        string[] IObjectCollection.Names
        {
            get { return desktop.GetNames(); }
        }

        object IObjectCollection.this[string name]
        {
            get 
            {
                return desktop.GetObject(name);
            }
        }

        #endregion


        #region IComponentCollection Members

        ICollection<object> IComponentCollection.AllComponents
        {
            get { return desktop.AllComponents; }
        }

        #endregion
 
 

		#region Specific Members



		/// <summary>
		/// Sets parents of objects
		/// </summary>
		/// <param name="desktop">The desktop</param>
		public void SetParents(IDesktop desktop)
		{
			ICollection<IObjectLabel> objs = desktop.Objects;
			INamedComponent comp = null;
			foreach (IObjectLabel ol in objs)
			{
				if (ol.Object == this)
				{
					comp = ol;
					break;
				}
			}
			ICollection<object> objects = this.desktop.Components;
			foreach (INamedComponent nc in objects)
			{
				if (nc is IObjectLabel)
				{
					IObjectLabel l = nc as IObjectLabel;
					if (l.Object is IObjectContainer)
					{
						IObjectContainer oc = l.Object as IObjectContainer;
						PureDesktopPeer.SetParents(oc.Desktop);
					}
				}
				nc.Parent = comp;
			}
		}


        /// <summary>
        /// Loads itself
        /// </summary>
        /// <returns>True in success</returns>
        public bool Load()
        {
            if (isLoaded)
            {
                return false;
            }
            isLoaded = true;
            desktop.HasParent = true;
            bool b = LoadDesktop(bytes);
            foreach (INamedComponent nc in desktop.Components)
            {
                if (nc.Parent == null)
                {
                    nc.Parent = Object as INamedComponent;
                }
                nc.Desktop = desktop;
            }
            desktop.setParent(this);
            return b;
        }

		/// <summary>
		/// The post load operation
		/// </summary>
		public virtual bool PostLoad()
		{
            if (!isLoaded)
            {
                return false;
            }
            if (isPostLoaded)
            {
                return true;
            }
            isPostLoaded = true;
			bytes = null;
			GC.Collect();
            return desktop.PostLoad();
		}

		/// <summary>
		/// Gets relative name of component
		/// </summary>
		/// <param name="comp">The component</param>
		/// <returns>The relative name</returns>
		public string GetName(INamedComponent comp)
		{
			return comp.GetName(desktop);
		}

		/// <summary>
		/// All child objects
		/// </summary>
		public ICollection<object> AllObjects
		{
			get
			{
				return PureDesktopPeer.GetAllObjects(desktop);
			}
		}

		/// <summary>
		/// Adds a component
		/// </summary>
		/// <param name="c">The component to add</param>
		/// <param name="x">The x - coordinate</param>
		/// <param name="y">The y - coordinate</param>
		/// <param name="comment">The comment</param>
		public void Add(INamedComponent c, int x, int y, string comment)
		{
			string name = c.GetName(desktop);
			inter[name] = new object[]{x, y, comment};
		}

		/// <summary>
		/// Serialization binders
		/// </summary>
		public static SerializationBinder[] Binders
		{
			set
			{
				binders = value;
			}
		}

		/// <summary>
		/// The type
		/// </summary>
		public string Type
		{
			get
			{
				return type;
			}
			set
			{
				type = value;
			}
		}

		/// <summary>
		/// Access to child by name
		/// </summary>
		/// <param name="name">The name</param>
		/// <returns>The child</returns>
		public INamedComponent this[string name]
		{
			get
			{
				return desktop[name];
			}
		}

		/// <summary>
		/// The interface
		/// </summary>
		public Hashtable Interface
		{
			get
			{
				return inter;
			}
		}

        /// <summary>
        /// Loads desktop
        /// </summary>
        /// <returns>Desktop</returns>
        public IDesktop LoadDesktop()
        {
            desktop.Load(bytes);
            return desktop;
        }


		/// <summary>
		/// The desktop
		/// </summary>
		public IDesktop Desktop
		{
			get
			{
				return desktop;
			}
		}

        /// <summary>
        /// Post Load desktop
        /// </summary>
        /// <returns>True in success</returns>
        protected bool DesktopPostLoad()
        {
            return desktop.PostLoad();
        }


		#endregion
   }
}

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