Click here to Skip to main content
15,893,487 members
Articles / Programming Languages / C#

EasiReports

Rate me:
Please Sign up or sign in to vote.
4.87/5 (64 votes)
13 Feb 2006CPOL6 min read 481.7K   9.7K   219  
A library to add reports to your application.
using System;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Diagnostics;

using EasiReports.Report;

using Common;

namespace EasiReports.Algorithms
{

//-----------------------------------------------------------------------------
// AlgorithmsDirector

	/// <summary>Controls algorithm assemblies.</summary>
	/// <remarks>
	/// <p>
	/// This class controls the algorithms associated with a report.
	/// </p>
	/// <p>
	/// Accessed through
	/// <see cref="EasiReport.AlgorithmsDirector">EasiReport.AlgorithmsDirector</see>.
	/// </p>
	/// </remarks>
	[ Serializable ]
	public class AlgorithmsDirector : PlugInDirector, ISerializable, IDeserializationCallback, IDisposable
	{

//-----------------------------------------------------------------------------
// Static consts

		/// <summary>The name of the CSharp assembly</summary>
		public const string CSharpAssembly = "CSharp";

		/// <summary>The name of the VBasic assembly</summary>
		public const string VBasicAssembly = "VBasic";

		/// <summary>The name of the CSharp class</summary>
		public const string CSharpClass = "CSharp.Algorithms";

		/// <summary>The name of the VBasic class</summary>
		public const string VBasicClass = "VBasic.Algorithms";

		/// <summary>The name of the GetDataSource group</summary>
		public const string OnSetDataSourceGroup = "OnSetDataSource";

		/// <summary>The name of the OnBeginPrint group</summary>
		public const string OnBeginPrintGroup = "OnBeginPrint";

		/// <summary>The name of the OnEndPrint group</summary>
		public const string OnEndPrintGroup = "OnEndPrint";

		/// <summary>The name of the OnQueryPageSettings group</summary>
		public const string OnQueryPageSettingsGroup = "OnQueryPageSettings";

		/// <summary>The name of the DataAlgorithm group</summary>
		public const string DataAlgorithmGroup = "DataAlgorithm";

		/// <summary>The name of the FormatControl group</summary>
		public const string FormatControlGroup = "FormatControl";

		/// <summary>The name of the FormatSection group</summary>
		public const string FormatSectionGroup = "FormatSection";

//-----------------------------------------------------------------------------
// Fields

		private int _DeserializationVersion = 0;

		private AlgorithmsMemoryAssemblyCSharp _AlgorithmsMemoryAssemblyCSharp = null;
		private AlgorithmsMemoryAssemblyVBasic _AlgorithmsMemoryAssemblyVBasic = null;

//-----------------------------------------------------------------------------
// Properties

		internal AlgorithmsMemoryAssemblyCSharp AlgorithmsMemoryAssemblyCSharp { get { return _AlgorithmsMemoryAssemblyCSharp; } }
		internal AlgorithmsMemoryAssemblyVBasic AlgorithmsMemoryAssemblyVBasic { get { return _AlgorithmsMemoryAssemblyVBasic; } }

//-----------------------------------------------------------------------------
// Constructors

		/// <overloads>Initializes a new instance of the class.</overloads>
		/// <summary>The default constructor.</summary>
		/// <remarks>
		/// <p>Creates an empty object.</p>
		/// </remarks>
		public AlgorithmsDirector()
		{
			AddMethodGroup( OnSetDataSourceGroup     , typeof( void   ) , "OnSetDataSource"     , new Type[] {                                                         typeof( OnSetDataSourceArgs     ) } );
			AddMethodGroup( OnBeginPrintGroup        , typeof( void   ) , "OnBeginPrint"        , new Type[] {                             typeof( ReportProperties ), typeof( OnBeginPrintArgs        ) } );
			AddMethodGroup( OnEndPrintGroup          , typeof( void   ) , "OnEndPrint"          , new Type[] {                             typeof( ReportProperties ), typeof( OnEndPrintArgs          ) } );
			AddMethodGroup( OnQueryPageSettingsGroup , typeof( void   ) , "OnQueryPageSettings" , new Type[] { typeof( ResultData       ), typeof( ReportProperties ), typeof( OnQueryPageSettingsArgs ) } );
			AddMethodGroup( DataAlgorithmGroup       , null             , String.Empty          , new Type[] { typeof( ResultData       ), typeof( ReportProperties ), typeof( DataAlgorithmArgs       ) } );
			AddMethodGroup( FormatSectionGroup       , typeof( void   ) , String.Empty          , new Type[] { typeof( ResultData       ), typeof( ReportProperties ), typeof( FormatSectionArgs       ) } );
			AddMethodGroup( FormatControlGroup       , typeof( void   ) , String.Empty          , new Type[] { typeof( ResultData       ), typeof( ReportProperties ), typeof( FormatControlArgs       ) } );

			_AlgorithmsMemoryAssemblyCSharp = new AlgorithmsMemoryAssemblyCSharp();
			_AlgorithmsMemoryAssemblyVBasic = new AlgorithmsMemoryAssemblyVBasic();

			AddAssembly( _AlgorithmsMemoryAssemblyCSharp );
			AddAssembly( _AlgorithmsMemoryAssemblyVBasic );

			AddIgnoreAssemblies();
		}

		private void AddIgnoreAssemblies()
		{
			IgnoreAssemblies.Add( "EasiReports" );
			IgnoreAssemblies.Add( "Interop.MSDASC" );
			IgnoreAssemblies.Add( "CSharp" );
			IgnoreAssemblies.Add( "VBasic" );
		}

		/// <summary>The copy constructor.</summary>
		/// <param name="o">The object to copy.</param>
		/// <remarks>
		/// <p>
		/// Creates a copy of the specified object.
		/// </p>
		/// </remarks>
		public AlgorithmsDirector( AlgorithmsDirector o ) : base ( o )
		{
			CopyThis( o );
		}

		/// <summary>Creates a copy of the object.</summary>
		/// <remarks>
		/// <p>
		/// This method is declared virtual so it can be overriden in derived classes.
		/// Derived classes return a new instance of themselves, so the type of the new object
		/// is the same as the current object.
		/// </p>
		/// </remarks>
		/// <returns>The new object.</returns>
		public override PlugInDirector CreateCopy()
		{
			return new AlgorithmsDirector( this );
		}

		/// <summary>Copies the specified object.</summary>
		/// <param name="o">The object to copy.</param>
		/// <remarks>
		/// <p>
		/// This method makes a deep copy of the specified object.
		/// </p>
		/// </remarks>
		public override void Copy( PlugInDirector o )
		{
			base.Copy( o );

			if ( ! ( o is AlgorithmsDirector ) ) { Debug.Assert( false ); return; }

			CopyThis( ( AlgorithmsDirector ) o );
		}

		private void CopyThis( AlgorithmsDirector o )
		{
			SetAssemblies();
		}

		private void SetAssemblies()
		{
			PlugInDirectorAssembly cs = GetAssembly( AlgorithmsDirector.CSharpAssembly );
			_AlgorithmsMemoryAssemblyCSharp = ( AlgorithmsMemoryAssemblyCSharp ) cs.MemoryAssembly;

			PlugInDirectorAssembly vb = GetAssembly( AlgorithmsDirector.VBasicAssembly );
			_AlgorithmsMemoryAssemblyVBasic = ( AlgorithmsMemoryAssemblyVBasic ) vb.MemoryAssembly;
		}

		/// <summary>Clean up any resources being used.</summary>
		/// <param name="disposing"><b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only unmanaged resources.</param>
		/// <remarks>Releases the unmanaged resources and optionally releases the managed resources.</remarks>
		protected override void Dispose( bool disposing )
		{
			if ( disposing )
			{
			}

			base.Dispose( disposing );
		}

//-----------------------------------------------------------------------------
// ValueEquals

		/// <summary>Whether this object is equal in value to another object.</summary>
		/// <param name="o">The other object.</param>
		/// <returns>
		/// <p>Returns <b>true</b> if the objects are equal in value.</p>
		/// <p>Returns <b>false</b> if any of the properties are different.</p>
		/// </returns>
		/// <remarks>
		/// <p>This method compares the specified object to the current instance.</p>
		/// <p>This method always returns <b>true</b> if the objects reference the same instance.</p>
		/// <p>If the objects reference different instances, then the values of all properties are compared.</p>
		/// <p>If any of the properties have different values, then this method returns <b>false</b>.</p>
		/// </remarks>
		public override bool ValueEquals( PlugInDirector o )
		{
			if ( ! base.ValueEquals( o ) ) return false;

			if ( o == null ) return false;

			if ( GetType() != o.GetType() ) return false;

			AlgorithmsDirector p = ( AlgorithmsDirector ) o;

			if ( ! _AlgorithmsMemoryAssemblyCSharp.ValueEquals( p._AlgorithmsMemoryAssemblyCSharp ) ) return false;
			if ( ! _AlgorithmsMemoryAssemblyVBasic.ValueEquals( p._AlgorithmsMemoryAssemblyVBasic ) ) return false;

			return true;
		}

//-----------------------------------------------------------------------------
// Serialization

		// Save
		/// <summary>Populates a SerializationInfo with the data needed to serialize the target object.</summary>
		/// <param name="info">The SerializationInfo to populate with data.</param>
		/// <param name="context">The destination for this serialization.</param>
		/// <remarks>
		/// <p>This method is called during serialization.</p>
		/// <p>Do not call this method directly.</p>
		/// </remarks>
		[ SecurityPermission( SecurityAction.Demand, SerializationFormatter = true ) ]
		public override void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			SerializationHelper.WriteLine( "AlgorithmsDirectorVersion" );

			base.GetObjectData( info, context );

			try
			{
				info.AddValue( "AlgorithmsDirectorVersion", 3 );
			}
			catch ( SerializationException e )
			{
				MessageBox.Show( e.ToString() );
			}
		}

		// Load
		/// <summary>Initializes a new instance of the class during deserialization.</summary>
		/// <param name="info">The SerializationInfo populated with data.</param>
		/// <param name="context">The source for this serialization.</param>
		/// <remarks>
		/// <p>This method is called during deserialization.</p>
		/// <p>Do not call this method directly.</p>
		/// </remarks>
		protected AlgorithmsDirector( SerializationInfo info, StreamingContext context ) : base( info, context )
		{
			try
			{
				_DeserializationVersion = info.GetInt32( "AlgorithmsDirectorVersion" );

				if ( _DeserializationVersion < 2 ) AddIgnoreAssemblies();
			}
			catch ( SerializationException e )
			{
				MessageBox.Show( e.ToString() );
			}
		}

		
		/// <summary>Called during deserialization.</summary>
		/// <param name="sender">The object that initiated the callback.</param>
		/// <remarks>
		/// <p>This method is called during deserialization.</p>
		/// <p>Do not call this method directly.</p>
		/// </remarks>
		public override void OnDeserialization( object sender )
		{
			base.OnDeserialization( sender );
		}

		/// <summary>Called during deserialization.</summary>
		/// <remarks>
		/// <p>This method is called during deserialization.</p>
		/// <p>Do not call this method directly.</p>
		/// </remarks>
		public override void Fixup()
		{
			base.Fixup();

			if ( _DeserializationVersion > 0 && _DeserializationVersion < 3 )
				AddMethodGroup( OnSetDataSourceGroup     , typeof( void   ) , "OnSetDataSource"     , new Type[] {                                                         typeof( OnSetDataSourceArgs     ) } );

			SetAssemblies();
		}

//-----------------------------------------------------------------------------
// Enumerators

		private class MemoryAssemblyEnumerator : IEnumerator, IEnumerable
		{
			private AlgorithmsDirector _Director = null;
			private int _i = -1;

			public MemoryAssemblyEnumerator( AlgorithmsDirector director )
			{
				_Director = director;
			}

			public IEnumerator GetEnumerator() { return this; }

			public object Current
			{
				get
				{
					if ( _i == 0 ) return _Director._AlgorithmsMemoryAssemblyCSharp;
					if ( _i == 1 ) return _Director._AlgorithmsMemoryAssemblyVBasic;

					throw new InvalidOperationException();
				}
			}

			public bool MoveNext() { return ( _i++ < 1 ); }

			public void Reset() { _i = -1; }
		}

		/// <summary>The memory assemblies controlled by this instance.</summary>
		/// <remarks>
		/// <p>
		/// Returns an enumerator of <see cref="MemoryAssembly"/>.
		/// </p>
		/// </remarks>
		public IEnumerable MemoryAssemblies { get { return new MemoryAssemblyEnumerator( this ); } }

//-----------------------------------------------------------------------------

	}
}

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
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions