Click here to Skip to main content
15,896,348 members
Articles / Programming Languages / C#

Romeo and Juliet

Rate me:
Please Sign up or sign in to vote.
4.87/5 (34 votes)
3 Dec 2011CPOL11 min read 58.9K   413   67  
Making relationships first class citizens.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Resources;
using System.Windows.Forms;

namespace Clifton.Windows.Forms.XmlTree
{
	public class NodeDef : ISupportInitialize
	{
		protected string name;
		protected string refName;
		protected bool isReadOnly;
		protected string text;
		protected bool isRequired;
		protected string iconFilename;
		private string typeName;
		private Type type;
		protected ImageList imgList;
		protected int imgOffset;
		protected bool separator;
		
		protected List<Popup> parentPopupItems;
		protected List<Popup> popupItems;
		protected List<NodeDef> nodes;
		protected NodeDef parent;
		protected bool recurse;
		protected string typeData;
		protected string iconResourceName;

		public bool Separator
		{
			get { return separator; }
			set { separator = value; }
		}
		
		/// <summary>
		/// Gets/sets iconResourceName
		/// </summary>
		public string IconResourceName
		{
			get { return iconResourceName; }
			set { iconResourceName = value; }
		}

		/// <summary>
		/// Gets/sets typeData
		/// </summary>
		public string TypeData
		{
			get { return typeData; }
			set { typeData = value; }
		}
		
		/// <summary>
		/// Gets/sets recurse flag.
		/// </summary>
		public bool Recurse
		{
			get { return recurse; }
			set { recurse = value; }
		}
		
		/// <summary>
		/// Gets/sets parent
		/// </summary>
		public NodeDef Parent
		{
			get { return parent; }
			set { parent = value; }
		}

		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		public string RefName
		{
			get { return refName; }
			set { refName = value; }
		}

		public bool IsReadOnly
		{
			get { return isReadOnly; }
			set { isReadOnly = value; }
		}

		public string Text
		{
			get { return text; }
			set { text = value; }
		}

		public bool IsRequired
		{
			get { return isRequired; }
			set { isRequired = value; }
		}

		public List<Popup> ParentPopupItems
		{
			get { return parentPopupItems; }
		}

		public List<Popup> PopupItems
		{
			get { return popupItems; }
		}

		public List<NodeDef> Nodes
		{
			get
			{
				if ((parent != null) && (recurse))
				{
					return parent.Nodes;
				}
				else
				{
					return nodes;
				}
			}
		}

		public bool IsRef
		{
			get { return refName != null; }
		}

		public string IconFilename
		{
			get { return iconFilename; }
			set { iconFilename = value; }
		}

		/// <summary>
		/// Gets/sets typeName
		/// </summary>
		public string TypeName
		{
			get { return typeName; }
			set { typeName = value; }
		}

		/// <summary>
		/// Gets/sets type
		/// </summary>
		public Type ImplementingType
		{
			get { return type; }
			set { type = value; }
		}

		public ImageList ImageList
		{
			get { return imgList; }
		}

		public int ImageOffset
		{
			get { return imgOffset; }
			set { imgOffset = value; }
		}

		public NodeDef()
		{
			parentPopupItems = new List<Popup>();
			popupItems = new List<Popup>();
			nodes = new List<NodeDef>();
			imgOffset = -1;
			separator = true;
		}

		public virtual void BeginInit()
		{
		}

		public virtual void EndInit()
		{
			imgList = new ImageList();
			type = typeof(PlaceholderInstance);

			if (iconFilename != null)
			{
				string[] icons = iconFilename.Split(',');

				foreach (string ifn in icons)
				{
					string fn=ifn.Trim();
					Icon icon = new Icon(fn);
					imgList.Images.Add(fn, icon);
				}
			}

			if (iconResourceName != null)
			{
				string[] icons = iconResourceName.Split(',');

				foreach (string irn in icons)
				{
					string assyName;
					string className;
					string resName;
					Helper.GetResourceInfo(irn, out assyName, out className, out resName);
					Assembly resAssy = Assembly.Load(assyName);
					ResourceManager rm = new ResourceManager(assyName + "." + className, resAssy);
					object obj = rm.GetObject(resName.Trim());
					imgList.Images.Add(irn + ".ico", (Icon)obj);
				}
			}

			if (typeName != null)
			{
				try
				{
					type = Type.GetType(typeName);
					System.Diagnostics.Trace.WriteLine("Typename " + TypeName + (type == null ? " null" : " found"));
				}
				catch (Exception e)
				{
					System.Diagnostics.Debug.WriteLine("Loading typename " + typeName + " failed.");
					throw e;
				}
			}

			foreach (NodeDef child in nodes)
			{
				child.Parent = this;
			}
		}

		public IXtreeNode CreateImplementingType(IXtreeNode parent)
		{
			IXtreeNode inst = null;
			inst = (IXtreeNode)Activator.CreateInstance(ImplementingType);
			inst.Parent = parent;
			return inst;
		}
	}
}

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 Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions