⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectbrowser.cs

📁 是用c#实现的一个有关于报表设计的程序代码
💻 CS
字号:
#region License
/*
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
#endregion

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using daReport;
using System.Xml;


namespace daReportDesigner
{
	#region Custom Delegate Declaration
	public delegate void AfterCustomSelectHandler(TreeViewEventArgs e);
	#endregion

	/// <summary>
	/// Summary description for ObjectBrowser.
	/// </summary>
	public class ObjectBrowser : System.Windows.Forms.TreeView
	{
		#region Declarations
		private ArrayList mNodesCollection = new ArrayList();
		private TreeNode mLastNode;
		private TreeNode mFirstNode;

		private TreeNode mRootNode;
		private TreeNode mParametersNode;
		private TreeNode mContentNode;

		public TreeNode staticContentsNode;
		public TreeNode dynamicContentsNode;

		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public event AfterCustomSelectHandler AfterCustomSelect;
		#endregion

		#region Public Functions

		public void ClearSelectedNodes()
		{
			this.SelectedNodes.Clear();
			this.removePaintFromNodes(this.mRootNode);
		}


		public void SelectDynamicNode(int t)
		{
			TreeNode CurrentNode = dynamicContentsNode.Nodes[t];
			TreeViewEventArgs e = new TreeViewEventArgs(CurrentNode, TreeViewAction.ByMouse);
			this.OnAfterSelect(e);

			//this.SelectedNode = dynamicContentsNode.Nodes[t];
		}


		public void SelectStaticNode(int t)
		{
			TreeNode CurrentNode = staticContentsNode.Nodes[t];
			TreeViewEventArgs e = new TreeViewEventArgs(CurrentNode, TreeViewAction.ByMouse);
			this.OnAfterSelect(e);

			//this.SelectedNode = staticContentsNode.Nodes[t];
		}


		public void SelectNodes(ArrayList StaticNodeIndexes, ArrayList DynamicNodeIndexes)
		{
			this.mNodesCollection.Clear();

			foreach (Int32 CurrentIndex in StaticNodeIndexes)
			{
				this.mNodesCollection.Add(this.staticContentsNode.Nodes[CurrentIndex]);
			}

			foreach (Int32 CurrentIndex in DynamicNodeIndexes)
			{
				this.mNodesCollection.Add(this.dynamicContentsNode.Nodes[CurrentIndex]);
			}

			this.removePaintFromNodes(this.mRootNode);
			this.paintSelectedNodes();
			this.AfterCustomSelect(new TreeViewEventArgs(Nodes[0], TreeViewAction.Unknown));
		}


		public void SetData(Parameters parameters, ICustomPaint[] staticElements, ICustomPaint[] dynamicElements)
		{
			InitNodes();
			for (int i=0;i<parameters.Count;i++)
			{
				mParametersNode.Nodes.Add( new TreeNode(parameters[i].ToString(),2,2) );	
			}
			mParametersNode.Expand();
			
			for (int i=0;i<staticElements.Length;i++)
			{
				if ( staticElements[i] is TextField)
				{
					TextField txtField = (TextField)staticElements[i];
					string theText = txtField.Text.Length>25 ? txtField.Text.Substring(0,25)+"..." : txtField.Text ;
					staticContentsNode.Nodes.Add( new TreeNode( "Text field [" + theText + "]",0,0) );	
				}
				else if ( staticElements[i] is daReport.PictureBox)
				{
					staticContentsNode.Nodes.Add( new TreeNode("Picture",1,1) );	
				}
				else if ( staticElements[i] is daReport.ChartBox)
				{
					ChartBox chartBox = (ChartBox)staticElements[i];
					staticContentsNode.Nodes.Add( new TreeNode("Chart [" + chartBox.Name + "]",5,5) );	
				}
				else if ( staticElements[i] is daReport.StyledTable)
				{
					StyledTable styledTable = (StyledTable)staticElements[i];

					if (styledTable.DataSource != null)
						staticContentsNode.Nodes.Add( new TreeNode( "Styled table [" + styledTable.DataSource + "]",3,3) );
					else
						staticContentsNode.Nodes.Add( new TreeNode("Styled table",3,3) );
				}
				else if ( staticElements[i] is daReport.Line)
				{
					staticContentsNode.Nodes.Add( new TreeNode("Line", 6, 6) );	
				}
				

			}
			staticContentsNode.Expand();

			for (int i=0;i<dynamicElements.Length;i++)
			{
				if ( dynamicElements[i] is TextField)
				{
					TextField txtField = (TextField)dynamicElements[i];
					string theText = txtField.Text.Length>25 ? txtField.Text.Substring(0,25)+"..." : txtField.Text ;
					dynamicContentsNode.Nodes.Add( new TreeNode("Text field [" + theText + "]",0,0) );	
				}
				else if ( dynamicElements[i] is daReport.PictureBox)
				{
					dynamicContentsNode.Nodes.Add( new TreeNode("Picture",1,1) );	
				}
				else if ( dynamicElements[i] is daReport.StyledTable)
				{
					StyledTable styledTable = (StyledTable)dynamicElements[i];

					if (styledTable.DataSource != null)
						dynamicContentsNode.Nodes.Add( new TreeNode("Styled table [" + styledTable.DataSource + "]",3,3) );
					else
						dynamicContentsNode.Nodes.Add( new TreeNode("Styled table",3,3) );
				}

			}
			dynamicContentsNode.Expand();
			
		}


		public void SetNodeText(ICustomPaint theObject,TreeNode theNode )
		{
			if ( theObject is TextField)
			{
				TextField txtField = (TextField)theObject;
				string theText = txtField.Text.Length>25 ? txtField.Text.Substring(0,25)+"..." : txtField.Text ;
				theNode.Text =  "Text field [" + theText + "]" ;	
			}
			else if ( theObject is daReport.PictureBox)
			{
				
				theNode.Text = "Picture" ;	
			}
			else if ( theObject is daReport.ChartBox)
			{
				ChartBox chartBox = (ChartBox)theObject;
				theNode.Text = "Chart [" + chartBox.Name + "]" ;	
			}
			else if ( theObject is daReport.StyledTable)
			{
				StyledTable styledTable = (StyledTable)theObject;

				if (styledTable.DataSource != null)
					theNode.Text = "Styled table [" + styledTable.DataSource + "]" ;
				else
					theNode.Text = "Styled table" ;
			}
			
		}
		

		#endregion
		
		#region Public Properties

		public bool IsDynamicNode(TreeNode theNode)
		{
			if (theNode==null) return false;

			return theNode.Parent == dynamicContentsNode;
		}


		public bool IsRootNode(TreeNode theNode)
		{
			if (theNode==null) return false;
			return theNode == this.Nodes[0];
		}


		public bool IsStaticNode(TreeNode theNode)
		{
			if (theNode==null) return false;

			return theNode.Parent == staticContentsNode;
		}


		public ArrayList SelectedNodes
		{
			get
			{
				return mNodesCollection;
			}
			set
			{
				//removePaintFromNodes();
				mNodesCollection.Clear();
				mNodesCollection = value;
				paintSelectedNodes();
			}
		}


		#endregion

		#region Overrides

		protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
		{
			base.OnBeforeSelect(e);
				
			bool bControl = (ModifierKeys==Keys.Control);
			bool bShift = (ModifierKeys==Keys.Shift);

			// selecting twice the node while pressing CTRL ?
			if (bControl && mNodesCollection.Contains( e.Node ) )
			{
				// unselect it (let framework know we don't want selection this time)
				e.Cancel = true;
	
				// update nodes
				removePaintFromNodes(this.mRootNode);
				mNodesCollection.Remove( e.Node );
				paintSelectedNodes();

				if(this.AfterCustomSelect!=null)
					AfterCustomSelect(new System.Windows.Forms.TreeViewEventArgs(new TreeNode(), TreeViewAction.Unknown));

				return;
			}

			mLastNode = e.Node;
			if (!bShift) mFirstNode = e.Node; // store begin of shift sequence
		}


		protected override void OnAfterSelect(TreeViewEventArgs e)
		{
			base.OnAfterSelect(e);

			bool bControl = (ModifierKeys==Keys.Control);
			bool bShift = (ModifierKeys==Keys.Shift);

			if (bControl)
			{
				if ( !mNodesCollection.Contains( e.Node ) ) // new node ?
				{
					mNodesCollection.Add( e.Node );
				}
				else  // not new, remove it from the collection
				{
					removePaintFromNodes(e.Node);
					mNodesCollection.Remove( e.Node );
				}
				paintSelectedNodes();
			}
			else 
			{
				// SHIFT is pressed
				if (bShift)
				{
					Queue myQueue = new Queue();
					
					TreeNode uppernode = mFirstNode;
					TreeNode bottomnode = e.Node;
					// case 1 : begin and end nodes are parent
					bool bParent = isParent(mFirstNode, e.Node); // is m_firstNode parent (direct or not) of e.Node
					if (!bParent)
					{
						bParent = isParent(bottomnode, uppernode);
						if (bParent) // swap nodes
						{
							TreeNode t = uppernode;
							uppernode = bottomnode;
							bottomnode = t;
						}
					}
					if (bParent)
					{
						TreeNode n = bottomnode;
						while ( n != uppernode.Parent)
						{
							if ( !mNodesCollection.Contains( n ) ) // new node ?
								myQueue.Enqueue( n );

							n = n.Parent;
						}
					}
						// case 2 : nor the begin nor the end node are descendant one another
					else
					{
						if ( (uppernode.Parent==null && bottomnode.Parent==null) || (uppernode.Parent!=null && uppernode.Parent.Nodes.Contains( bottomnode )) ) // are they siblings ?
						{
							int nIndexUpper = uppernode.Index;
							int nIndexBottom = bottomnode.Index;
							if (nIndexBottom < nIndexUpper) // reversed?
							{
								TreeNode t = uppernode;
								uppernode = bottomnode;
								bottomnode = t;
								nIndexUpper = uppernode.Index;
								nIndexBottom = bottomnode.Index;
							}

							TreeNode n = uppernode;
							while (nIndexUpper <= nIndexBottom)
							{
								if ( !mNodesCollection.Contains( n ) ) // new node ?
									myQueue.Enqueue( n );
								
								n = n.NextNode;

								nIndexUpper++;
							} // end while
							
						}
						else
						{
							if ( !mNodesCollection.Contains( uppernode ) ) myQueue.Enqueue( uppernode );
							if ( !mNodesCollection.Contains( bottomnode ) ) myQueue.Enqueue( bottomnode );
						}
					}

					mNodesCollection.AddRange( myQueue );

					removePaintFromNodes(this.mRootNode);
					paintSelectedNodes();
					mFirstNode = e.Node; // let us chain several SHIFTs if we like it
				} // end if m_bShift
				else
				{
					// in the case of a simple click, just add this item
					if (mNodesCollection!=null && mNodesCollection.Count>0)
					{
						removePaintFromNodes(this.mRootNode);
						mNodesCollection.Clear();
					}
					mNodesCollection.Add( e.Node );
					this.SelectedNode = e.Node;
				}
			}

			if(this.AfterCustomSelect!=null)
				AfterCustomSelect(e);
		}


		#endregion

		#region Private Functions

		private void InitNodes()
		{
			this.Nodes.Clear();

			mParametersNode = new TreeNode("Parameters");

			mContentNode = new TreeNode("Contents");
			staticContentsNode = new TreeNode("Static contents");
			dynamicContentsNode = new TreeNode("Dynamic contents");
			mContentNode.Nodes.Add(staticContentsNode);
			mContentNode.Nodes.Add(dynamicContentsNode);
			mContentNode.Expand();

			mRootNode = new TreeNode("Print Document");

			mRootNode.Nodes.Add(mParametersNode);
			mRootNode.Nodes.Add(mContentNode);

			mRootNode.Expand();
			this.Nodes.Add(mRootNode);
		}


		protected void paintSelectedNodes()
		{
			foreach ( TreeNode n in mNodesCollection )
			{
				n.BackColor = SystemColors.Highlight;
				n.ForeColor = SystemColors.HighlightText;
			}
		}


		protected void removePaintFromNodes(TreeNode CurrentNode)
		{
			if (CurrentNode.Nodes.Count != 0)
			{
				foreach (TreeNode ChildNode in CurrentNode.Nodes)
				{
					removePaintFromNodes(ChildNode);
				}
			}

			CurrentNode.BackColor = this.BackColor;
			CurrentNode.ForeColor = this.ForeColor;
		}


		#endregion

		#region Private Properties

		protected bool isParent(TreeNode parentNode, TreeNode childNode)
		{
			if (parentNode==childNode)
				return true;

			TreeNode n = childNode;
			bool bFound = false;
			while (!bFound && n!=null)
			{
				n = n.Parent;
				bFound = (n == parentNode);
			}
			return bFound;
		}


		#endregion

		#region Creator and Destructor

		public ObjectBrowser()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

		}


		/// <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()
		{

		}
		#endregion

		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -