www.pudn.com > CustomFormsDesigner.rar > ToolboxServiceImpl.cs, change:2004-07-09,size:16503b


using System; 
using System.CodeDom; 
using System.Collections; 
using System.ComponentModel; 
using System.ComponentModel.Design; 
using System.ComponentModel.Design.Serialization; 
using System.Drawing; 
using System.Drawing.Design; 
using System.Windows.Forms; 
using System.Windows.Forms.Design; 
using System.Reflection; 
 
namespace FormDesigner.impls 
{ 
	/// <summary> 
	/// Summary description for ToolboxServiceImpl. 
	/// </summary> 
	public class ToolboxServiceImpl : System.Windows.Forms.UserControl,IToolboxService 
	{ 
		private System.Windows.Forms.ImageList treeViewImgList; 
		private System.Windows.Forms.TreeView toolboxView; 
		private System.Windows.Forms.ImageList requiredImgList; 
		private System.ComponentModel.IContainer components; 
		public ToolboxServiceImpl() 
		{ 
			// This call is required by the Windows.Forms Form Designer. 
			InitializeComponent(); 
					 
			// 
			toolboxView.ItemDrag+=new ItemDragEventHandler(toolboxView_ItemDrag); 
			toolboxView.MouseDown+=new MouseEventHandler(toolboxView_MouseDown); 
		} 
		/// <summary> 
		/// used to load the toolbox 
		/// </summary> 
		/// <param name="tools"></param> 
		public void LoadToolbox(FDToolbox tools) 
		{ 
			// clear out existing nodes and the imagelist associated with the tree 
			toolboxView.Nodes.Clear(); 
			treeViewImgList.Dispose(); 
			treeViewImgList=new ImageList(this.components); 
			// we have two images that we always use for category nodes 
			// and the select tool node (pointer node). 
			// add these in now 
			treeViewImgList.Images.Add(requiredImgList.Images[0]); 
			treeViewImgList.Images.Add(requiredImgList.Images[1]); 
			// assign imagelist to the treeview 
			toolboxView.ImageList=treeViewImgList; 
			// if we have categories... 
			if(tools!=null && tools.FDToolboxCategories!=null && tools.FDToolboxCategories.Length>0) 
			{ 
				foreach(Category cat in tools.FDToolboxCategories) 
				{ 
					LoadCategory(cat); 
				} 
			} 
		} 
		/// <summary>  
		/// Clean up any resources being used. 
		/// </summary> 
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if(components != null) 
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Component Designer generated code 
		/// <summary>  
		/// Required method for Designer support - do not modify  
		/// the contents of this method with the code editor. 
		/// </summary> 
		private void InitializeComponent() 
		{ 
			this.components = new System.ComponentModel.Container(); 
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ToolboxServiceImpl)); 
			this.treeViewImgList = new System.Windows.Forms.ImageList(this.components); 
			this.toolboxView = new System.Windows.Forms.TreeView(); 
			this.requiredImgList = new System.Windows.Forms.ImageList(this.components); 
			this.SuspendLayout(); 
			//  
			// treeViewImgList 
			//  
			this.treeViewImgList.ImageSize = new System.Drawing.Size(16, 16); 
			this.treeViewImgList.TransparentColor = System.Drawing.Color.Transparent; 
			//  
			// toolboxView 
			//  
			this.toolboxView.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192))); 
			this.toolboxView.BorderStyle = System.Windows.Forms.BorderStyle.None; 
			this.toolboxView.Dock = System.Windows.Forms.DockStyle.Fill; 
			this.toolboxView.HideSelection = false; 
			this.toolboxView.ImageIndex = -1; 
			this.toolboxView.Location = new System.Drawing.Point(0, 0); 
			this.toolboxView.Name = "toolboxView"; 
			this.toolboxView.SelectedImageIndex = -1; 
			this.toolboxView.Size = new System.Drawing.Size(304, 328); 
			this.toolboxView.TabIndex = 0; 
			//  
			// requiredImgList 
			//  
			this.requiredImgList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit; 
			this.requiredImgList.ImageSize = new System.Drawing.Size(16, 16); 
			this.requiredImgList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("requiredImgList.ImageStream"))); 
			this.requiredImgList.TransparentColor = System.Drawing.Color.Transparent; 
			//  
			// ToolboxServiceImpl 
			//  
			this.Controls.Add(this.toolboxView); 
			this.Name = "ToolboxServiceImpl"; 
			this.Size = new System.Drawing.Size(304, 328); 
			this.ResumeLayout(false); 
 
		} 
		#endregion 
 
		#region IToolboxService Members 
 
		public void AddLinkedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category, IDesignerHost host) 
		{ 
			// TODO:  Add ToolboxServiceImpl.AddLinkedToolboxItem implementation 
		} 
 
		void System.Drawing.Design.IToolboxService.AddLinkedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, IDesignerHost host) 
		{ 
			// TODO:  Add ToolboxServiceImpl.System.Drawing.Design.IToolboxService.AddLinkedToolboxItem implementation 
		} 
 
		public System.Drawing.Design.ToolboxItem DeserializeToolboxItem(object serializedObject, IDesignerHost host) 
		{ 
			// we know that we serialized the item in a dataobject, so  
			// get it out  
			return (ToolboxItem)((DataObject)serializedObject).GetData(typeof(ToolboxItem)); 
		} 
 
		System.Drawing.Design.ToolboxItem System.Drawing.Design.IToolboxService.DeserializeToolboxItem(object serializedObject) 
		{ 
			return (ToolboxItem)((DataObject)serializedObject).GetData(typeof(ToolboxItem)); 
		} 
 
		public object SerializeToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem) 
		{ 
			DataObject dataObj = null; 
			if(toolboxItem!=null) 
			{ 
				// package the item in a dataobject 
				dataObj = new DataObject(toolboxItem); 
			} 
			return dataObj; 
		} 
 
		public void RemoveToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category) 
		{ 
			// we handle adding and removing of toolbox items ourself 
		} 
 
		void System.Drawing.Design.IToolboxService.RemoveToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem) 
		{ 
			// we handle adding and removing of toolbox items ourself 
		} 
 
		public bool SetCursor() 
		{ 
			// not critical for sample app 
			return false; 
		} 
 
		public void AddToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category) 
		{ 
			// we handle adding and removing of toolbox items ourself 
		} 
 
		void System.Drawing.Design.IToolboxService.AddToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem) 
		{ 
			// we handle adding and removing of toolbox items ourself 
		} 
 
		public void AddCreator(System.Drawing.Design.ToolboxItemCreatorCallback creator, string format, IDesignerHost host) 
		{ 
			// we only deal with the standard win forms tools that we know of 
		} 
 
		void System.Drawing.Design.IToolboxService.AddCreator(System.Drawing.Design.ToolboxItemCreatorCallback creator, string format) 
		{ 
			// we only deal with the standard win forms tools that we know of 
		} 
 
		public void RemoveCreator(string format, IDesignerHost host) 
		{ 
			// we only deal with the standard win forms tools that we know of 
		} 
 
		void System.Drawing.Design.IToolboxService.RemoveCreator(string format) 
		{ 
			// we only deal with the standard win forms tools that we know of 
		} 
 
		public ToolboxItemCollection GetToolboxItems(string category, IDesignerHost host) 
		{ 
			return (this as IToolboxService).GetToolboxItems(category); 
		} 
 
		ToolboxItemCollection System.Drawing.Design.IToolboxService.GetToolboxItems(string category) 
		{ 
			IList items = new ArrayList(); 
			// return all toolbox items 
			if(toolboxView!=null && toolboxView.Nodes!=null && toolboxView.Nodes.Count>0) 
			{ 
				// iterate over categories 
				foreach(TreeNode nd in toolboxView.Nodes) 
				{ 
					if(string.Compare(nd.Text,category,true)==0) 
					{ 
						foreach(TreeNode itemNode in nd.Nodes) 
						{ 
							items.Add(itemNode.Tag); 
						} 
					} 
				} 
			} 
			ToolboxItem[] obj = new ToolboxItem[items.Count]; 
			items.CopyTo(obj,0); 
			return new ToolboxItemCollection(obj); 
		} 
 
		ToolboxItemCollection System.Drawing.Design.IToolboxService.GetToolboxItems(IDesignerHost host) 
		{ 
			IList items = new ArrayList(); 
			// return all toolbox items 
			if(toolboxView!=null && toolboxView.Nodes!=null && toolboxView.Nodes.Count>0) 
			{ 
				// iterate over categories 
				foreach(TreeNode nd in toolboxView.Nodes) 
				{ 
					foreach(TreeNode itemNode in nd.Nodes) 
					{ 
						items.Add(itemNode.Tag); 
					} 
				} 
			} 
			ToolboxItem[] obj = new ToolboxItem[items.Count]; 
			items.CopyTo(obj,0); 
			return new ToolboxItemCollection(obj); 
		} 
 
		ToolboxItemCollection System.Drawing.Design.IToolboxService.GetToolboxItems() 
		{ 
			return (this as IToolboxService).GetToolboxItems((IDesignerHost)null); 
		} 
 
		public CategoryNameCollection CategoryNames 
		{ 
			get 
			{ 
				string[] names = null; 
				if(toolboxView!=null && toolboxView.Nodes!=null && toolboxView.Nodes.Count>0) 
				{ 
					names = new string[toolboxView.Nodes.Count]; 
					int categoryCount = 0; 
					foreach(TreeNode nd in toolboxView.Nodes) 
					{ 
						names[categoryCount++]=nd.Text; 
					} 
				} 
 
				return new CategoryNameCollection(names); 
			} 
		} 
 
		public System.Drawing.Design.ToolboxItem GetSelectedToolboxItem(IDesignerHost host) 
		{ 
			ToolboxItem item = null; 
//			if(toolboxView.SelectedNode!=null && toolboxView.SelectedNode.Tag!=null) 
//			{ 
//				item = toolboxView.SelectedNode.Tag as ToolboxItem; 
//			} 
			return item; 
		} 
 
		System.Drawing.Design.ToolboxItem System.Drawing.Design.IToolboxService.GetSelectedToolboxItem() 
		{ 
			return (this as IToolboxService).GetSelectedToolboxItem((IDesignerHost)null); 
		} 
 
		public void SetSelectedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem) 
		{ 
			// we handle this manually 
		} 
 
		public bool IsSupported(object serializedObject, ICollection filterAttributes) 
		{ 
			return true; 
		} 
 
		bool System.Drawing.Design.IToolboxService.IsSupported(object serializedObject, IDesignerHost host) 
		{ 
			return true; 
		} 
 
		public void SelectedToolboxItemUsed() 
		{ 
			// after the user has selected a tool, then the framework calls 
			// this method to select the pointer tool 
			// get the category and select the first node (the pointer node) 
//			if(toolboxView!=null && toolboxView.Nodes!=null &&  
//				toolboxView.Nodes.Count>0 && toolboxView.SelectedNode!=null &&  
//				toolboxView.SelectedNode.Parent!=null && 
//				string.Compare("Pointer",toolboxView.SelectedNode.Text,true)!=0) 
//			{ 
//				// get the parent node and select its pointer node 
//				TreeNode parentNode = toolboxView.SelectedNode.Parent; 
//				foreach(TreeNode nd in parentNode.Nodes) 
//				{ 
//					if(string.Compare(nd.Text,"Pointer",true)==0) 
//					{ 
//						toolboxView.SelectedNode=nd; 
//						break; 
//					} 
//				} 
//			} 
		} 
 
		public bool IsToolboxItem(object serializedObject, IDesignerHost host) 
		{ 
			// TODO:  Add ToolboxServiceImpl.IsToolboxItem implementation 
			return false; 
		} 
 
		bool System.Drawing.Design.IToolboxService.IsToolboxItem(object serializedObject) 
		{ 
			// TODO:  Add ToolboxServiceImpl.System.Drawing.Design.IToolboxService.IsToolboxItem implementation 
			return false; 
		} 
 
		public string SelectedCategory 
		{ 
			get 
			{ 
				string category = null; 
				// get the selected node and find the category 
				if(toolboxView!=null && toolboxView.Nodes!=null &&  
					toolboxView.Nodes.Count>0 && toolboxView.SelectedNode!=null && 
					toolboxView.SelectedNode.Tag is ToolboxItem) 
				{ 
					category = toolboxView.SelectedNode.Parent.Text; 
				} 
				return category; 
			} 
			set 
			{ 
				// TODO:  Add ToolboxServiceImpl.SelectedCategory setter implementation 
			} 
		} 
 
		#endregion 
 
		#region Helper Methods  
		/// <summary> 
		/// loads a group of toolbox items into the under the given category 
		/// </summary> 
		/// <param name="cat"></param> 
		private void LoadCategory(Category cat) 
		{ 
			// if we have items in the category... 
			if(cat!=null && cat.FDToolboxItem!=null && cat.FDToolboxItem.Length>0) 
			{ 
				// create a node for the category 
				TreeNode catNode = new TreeNode(cat.DisplayName); 
				catNode.ImageIndex=0; 
				catNode.SelectedImageIndex=0; 
				// add this category to the tree 
				toolboxView.Nodes.Add(catNode); 
				// every category gets the selection tool node 
				AddSelectionNode(catNode); 
				foreach(FDToolboxItem item in cat.FDToolboxItem) 
				{ 
					LoadItem(item,catNode); 
				} 
			} 
		} 
		private void AddSelectionNode(TreeNode cat) 
		{ 
			if(cat!=null) 
			{ 
				ToolboxItem item = new ToolboxItem(); 
				//				item.Bitmap=new Bitmap(this.treeViewImgList.Images[1]); 
				item.DisplayName="Pointer"; 
				TreeNode nd = new TreeNode(item.DisplayName); 
				nd.ImageIndex=1; 
				nd.SelectedImageIndex=1; 
				nd.Tag=item; 
				cat.Nodes.Add(nd); 
			} 
		} 
		/// <summary> 
		/// loads an item into the tree 
		/// </summary> 
		/// <param name="item"></param> 
		private void LoadItem(FDToolboxItem item,TreeNode cat) 
		{ 
			if(item!=null && item.Type!=null && cat!=null) 
			{ 
				// load the type 
				string[] assemblyClass = item.Type.Split(new char[]{','}); 
				Type toolboxItemType = GetTypeFromLoadedAssembly(assemblyClass[0],assemblyClass[1]); 
				// 
				ToolboxItem toolItem = new ToolboxItem(toolboxItemType); 
				// get the image for the item 
				Image img = GetItemImage(toolboxItemType); 
				// create the node for it 
				TreeNode nd = new TreeNode(toolItem.DisplayName); 
				// add the item's bitmap to the image list 
				if(img!=null) 
				{ 
					// add it to the image list 
					treeViewImgList.Images.Add(img); 
					// set the nodes image index 
					nd.ImageIndex=treeViewImgList.Images.Count-1; 
					// we have to set both the node's ImageIndex and 
					// SelectedImageIndex or we get some wierd behavior 
					// when we select nodes (the selected nodes image changes)... 
					nd.SelectedImageIndex=treeViewImgList.Images.Count-1; 
				} 
				nd.Tag=toolItem; 
				// add this node to the category node 
				cat.Nodes.Add(nd); 
			} 
		} 
 
		/// <summary> 
		/// finds the image associated with the type 
		/// </summary> 
		/// <param name="type"></param> 
		/// <returns></returns> 
		private Image GetItemImage(Type type) 
		{ 
			Image img = null; 
			// get the AttributeCollection for the given type and 
			// find the ToolboxBitmap attribute  
			AttributeCollection attrCol=TypeDescriptor.GetAttributes(type,true); 
			if(attrCol!=null) 
			{ 
				ToolboxBitmapAttribute toolboxBitmapAttr=(ToolboxBitmapAttribute)attrCol[typeof(ToolboxBitmapAttribute)]; 
				if(toolboxBitmapAttr!=null) 
				{ 
					img = toolboxBitmapAttr.GetImage(type); 
				} 
			} 
			return img; 
		} 
 
		#endregion 
 
		private void toolboxView_ItemDrag(object sender, ItemDragEventArgs e) 
		{ 
			DoDragDrop(e.Item,DragDropEffects.Move); 
		} 
 
		private void toolboxView_MouseDown(object sender, MouseEventArgs e) 
		{ 
			// single click 
			if(sender is TreeView && e.Clicks<2) 
			{ 
				TreeNode clickedNode = toolboxView.GetNodeAt(e.X,e.Y); 
				if(clickedNode!=null) 
				{ 
					// select it 
					toolboxView.SelectedNode=clickedNode; 
					// if the node is a top level category 
					// we don't do anything other than select it 
					if(clickedNode.Parent!=null) 
					{ 
						ToolboxItem item = clickedNode.Tag as ToolboxItem; 
						// if its not the pointer tool 
						if(string.Compare(item.DisplayName,"Pointer",true)!=0) 
						{ 
							// serialize the item and send it over 
							DataObject dataObj = SerializeToolboxItem(item) as DataObject; 
							DoDragDrop(dataObj,DragDropEffects.Copy); 
						} 
					} 
				} 
			} 
			else if(sender is TreeView && e.Clicks==2) 
			{ 
				// double click 
			} 
		} 
		private Type GetTypeFromLoadedAssembly(string classname,string assembly) 
		{ 
			Assembly[] loadedAsms = AppDomain.CurrentDomain.GetAssemblies(); 
			Type loadedType = null; 
					 
			Assembly foundAssembly = null; 
			foreach(Assembly asm in loadedAsms) 
			{ 
				if(string.Compare(assembly,asm.GetName().Name,true)==0) 
				{ 
					foundAssembly=asm; 
					break; 
				} 
			} 
			if(foundAssembly!=null) 
			{ 
				loadedType = foundAssembly.GetType(classname); 
			} 
			else 
			{ 
				// try to load it with a partial name 
				Assembly asm = Assembly.LoadWithPartialName(assembly); 
				if(asm!=null) 
				{ 
					loadedType=asm.GetType(classname); 
				} 
			} 
			return loadedType; 
		} 
	} 
}