Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF

The Time Machine

Rate me:
Please Sign up or sign in to vote.
4.91/5 (13 votes)
9 May 2012CPOL6 min read 34.1K   1.2K   32  
Long time strategy of software design and development
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;

using CategoryTheory;

using Diagram.UI.Interfaces;
using Diagram.UI.Interfaces.Labels;

namespace DiagramUI.Labels
{
	/// <summary>
	/// 
	/// </summary>
	internal class ChildObjectLabel : Panel, IChildObjectLabel
	{
		static private readonly int width = 10;
		static private readonly int height = 10;
		static private readonly Color back = Color.Black;
		private INamedComponent nc;
		private ContainerObjectLabel parent;
		private int x;
		private int y;
		private Form form;


		internal ChildObjectLabel(ContainerObjectLabel parent, string name, INamedComponent nc, object[] o)
		{
			this.nc = nc;
			Height = height;
			Width = width;
			BackColor = back;
			ToolTip t = new ToolTip();
			t.ShowAlways = true;
			t.SetToolTip(this, name + " :: " + o[2]);
			x = (int) o[0];
			y = (int) o[1];
			this.parent = parent;
			parent.Parent.Controls.Add(this);
			move();
			MouseUp += new MouseEventHandler(onMouseUpShow) + new MouseEventHandler(onMouseUpArrow);
			MouseDown += new MouseEventHandler(onMouseDownMoveEventHandler);
			MouseMove += new MouseEventHandler(onMouseMoveArrow);

		}


		#region Specific Members

		internal void move()
		{
			Left = parent.Left + x;
			Top = parent.Top + y;
		}

		internal void remove()
		{
			RemoveForm();
			PanelDesktop desktop = Parent as PanelDesktop;
            if (desktop != null)
            {
                desktop.remove(this);
            }
            if (nc != null)
            {
                nc.Remove();
            }
		}

		public void RemoveForm()
		{
			if (form != null)
			{
                if (!form.IsDisposed)
				{
					form.Dispose();
				}
				form = null;
				GC.Collect();
			}
		}

		/// <summary>
		/// The on mouse up event handler
		/// </summary>
		/// <param name="sender">The sender</param>
		/// <param name="e">The event arguments</param>
		private void onMouseUpShow(object sender, MouseEventArgs e)
		{

			if (e.Button != MouseButtons.Right)
			{
				return;
			}
			ShowForm();
		}

		public void ShowForm()
		{
			PanelDesktop desktop = Parent as PanelDesktop;
            try
            {
                if (form == null)
                {
                    form = desktop.Tools.Factory.CreateForm(nc) as Form;
                }
                if (form != null)
                {
                    if (form.IsDisposed)
                    {
                        form = desktop.Tools.Factory.CreateForm(nc) as Form;
                    }
                    form.Show();
                    form.BringToFront();
                    form.Activate();
                    form.Focus();
                    form.Show();
                }

            }
            catch (Exception ex)
            {
                ex.ShowError(10);
            }

		}

		/// <summary>
		/// The on mouse up event handler
		/// </summary>
		/// <param name="sender">The sender</param>
		/// <param name="e">The event arguments</param>
		protected void onMouseUpArrow(object sender, MouseEventArgs e)
		{
			//isMoved = false;
			PanelDesktop desktop = Parent as PanelDesktop;
			ICategoryArrow arrow = desktop.ActiveArrow;
            if (!StaticExtensionDiagramUIForms.IsArrowClick(e))
			{
				return;
			}
			if (!(nc is IObjectLabel))
			{
				return;
			}
			IObjectLabel ol = nc as IObjectLabel;
			ICategoryObject obj = ol.Object;
			try
			{
				if (arrow == null)
				{
					return;
				}
				int x = Left + e.X;
				int y = Top + e.Y;
				for (int i = 0; i < desktop.Controls.Count; i++)
				{
					if (!(desktop.Controls[i] is ChildObjectLabel) & !(desktop.Controls[i] is ObjectLabel))
					{
						continue;
					}
					Control c = desktop.Controls[i];
					bool hor = x < c.Left | x > c.Left + c.Width;
					bool vert = y < c.Top | y > c.Top + c.Height;
					if (hor | vert)
					{
						continue;
					}
					IObjectLabel label = null;
					if (desktop.Controls[i] is IObjectLabel)
					{
						label = desktop.Controls[i] as IObjectLabel;
					}
					else
					{
						ChildObjectLabel child = desktop.Controls[i] as ChildObjectLabel;
						label = child.Label;
					}
					arrow.Target = label.Object;
					ArrowLabel lab = 
                        desktop.Tools.Factory.CreateArrowLabel(desktop.Tools.Active, 
                        arrow, ol, label) as ArrowLabel;
					lab.Arrow.Object = lab;
					desktop.AddArrowLabel(lab);
					break;
				}
			}
			catch (Exception ex)
			{
                ex.ShowError(10);
                if (arrow != null)
				{
					if (arrow is IRemovableObject)
					{
						IRemovableObject rem = arrow as IRemovableObject;
						rem.RemoveObject();
					}
				}
                ex.ShowError(1);
			}
			desktop.ActiveArrow = null;
			desktop.Redraw();
		}

		public IObjectLabel Label
		{
			get
			{
				if (nc is IObjectLabel)
				{
					return nc as IObjectLabel;
				}
				return null;
			}
		}

		/// <summary>
		/// The on mouse down event handler
		/// </summary>
		/// <param name="sender">The sender</param>
		/// <param name="e">The event arguments</param>
		private void onMouseDownMoveEventHandler(object sender, MouseEventArgs e)
		{
            if (!e.IsArrowClick())
			{
				return;
			}
			IObjectLabel lab = Label;
			if (lab == null)
			{
				return;
			}
			PanelDesktop desktop = Parent as PanelDesktop;
			PaletteButton active = desktop.Tools.Active;
			if (active != null)
			{
				if (active.IsArrow & !(active.ReflectionType == null))
				{
					try
					{
						ICategoryArrow arrow = desktop.Tools.Factory.CreateArrow(active);
						arrow.Source = lab.Object;
						desktop.ActiveArrow = arrow;
						desktop.ActiveObjectLabel = lab;
						return;
					}
					catch (Exception ex)
					{
                        ex.ShowError(10);
 					}
				}
			}
		}

		/// <summary>
		/// The on mouse move event handler
		/// Draws correspond arrows
		/// </summary>
		/// <param name="sender">The sender</param>
		/// <param name="e">The event arguments</param>
		protected void onMouseMoveArrow(object sender, MouseEventArgs e)
		{
			PanelDesktop desktop = Parent as PanelDesktop;
            if (!e.IsArrowClick())
			{
				return;
			}
			if (desktop.ActiveArrow == null)
			{
				return;
			}
			desktop.DrawArrow(this, e);
		}






		#endregion

 
        #region IStartStop Members

        void IStartStop.Action(object type, ActionType actionType)
        {
            StaticExtensionDiagramUIForms.Action(form, type, actionType);
        }

        #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