Click here to Skip to main content
15,895,709 members
Articles / Web Development / HTML

Windows and Web Generic Components

Rate me:
Please Sign up or sign in to vote.
4.08/5 (7 votes)
23 Jul 2008CPOL4 min read 27.4K   742   15  
A method to create Windows and Web components with the same interface
using System;
using System.Resources;
using System.Reflection;
using Framework.baInterfaces;
using Framework.UI.baResources;
using Framework.Lib;

namespace Framework.UI.baController
{
    #region AddEditDelEventArgs
	public class AddEditDelEventArgs: System.EventArgs
	{
		private TransactionStateKind transactionState;
		public AddEditDelEventArgs()
		{
		}
		public AddEditDelEventArgs(TransactionStateKind transactionState)
		{
			this.transactionState = transactionState;
		}

		public TransactionStateKind TransactionState
		{
			get
			{
				return transactionState;
			}
            set
            {
                transactionState = value;
            }
		}
	}
	#endregion

	#region IAddEditDelete and EventHandlers
	/// <summary>
	/// EventHandlers
	/// </summary>
	public delegate void AddEditDelEventHandler(object sender, AddEditDelEventArgs e);
	/// <summary>
	/// Summary description for IAddEditDelete.
	/// </summary>
	public interface IAddEditDelete
	{
        IBindingManager BindingManager
        {
            get;
            set;
        }

		object Controller
		{
			get;
		}

        System.Drawing.Point Location
        {
            get;
            set;
        }

        TransactionStateKind TransactionState
        {
            get;
            set;
        }
        TransactionStateKind OldTransactionState
        {
            get;
            set;
        }

		IButton AddButton
		{
			get;
		}
		IButton EditButton
		{
			get;
		}
		IButton DeleteButton
		{
			get;
		}
		IButton ConfirmButton
		{
			get;
		}

		event AddEditDelEventHandler AddClick;
		event AddEditDelEventHandler EditClick;
		event AddEditDelEventHandler DeleteClick;
		event AddEditDelEventHandler ConfirmClick;
        event AddEditDelEventHandler TransactionStateChanged;
	}
	#endregion

    /// <summary>
    /// Finite State machine
    /// </summary>
    public partial class AddEditDelCtrl
    {
        private Lib.StateMachine stateMachine;
        #region ConstructStateMachine & SetButtonsState
        /// <summary>
		/// Set the state machine values:
		/// 
		///				|	AddButton	|	EditButton		|	DeleteButton	|	ConfirmButton
		///		------------------------------------------------------------------------------------
		///		Initial	|	Enable		|	Enable			|	Enable			|	Disable
		///					������			�����			
		///		------------------------------------------------------------------------------------
		///		Add		|
		///		------------------------------------------------------------------------------------
		///		Edit	|
		///		------------------------------------------------------------------------------------
		///		Delete	|
		///		
		/// </summary>
		private void ConstructStateMachine()
		{
            try
            {
                stateMachine = new Lib.StateMachine();

                // The <AddButton> control in <Add> state is 
                stateMachine[TransactionStateKind.Add, theComponent.AddButton] = new State(true, rm.GetString("AddCancel"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Add, theComponent.EditButton] = new State(false, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Add, theComponent.DeleteButton] = new State(false, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Add, theComponent.ConfirmButton] = new State(true, rm.GetString("Confirm"), System.Drawing.Color.Green);

                stateMachine[TransactionStateKind.Edit, theComponent.AddButton] = new State(false, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Edit, theComponent.EditButton] = new State(true, rm.GetString("EditCancel"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Edit, theComponent.DeleteButton] = new State(false, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Edit, theComponent.ConfirmButton] = new State(true, rm.GetString("Confirm"), System.Drawing.Color.Orange);

                stateMachine[TransactionStateKind.Delete, theComponent.AddButton] = new State(false, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Delete, theComponent.EditButton] = new State(false, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Delete, theComponent.DeleteButton] = new State(true, rm.GetString("DeleteCancel"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Delete, theComponent.ConfirmButton] = new State(true, rm.GetString("Confirm"), System.Drawing.Color.Red);

                // The <AddButton> control Text in <Initial> state is 
                stateMachine[TransactionStateKind.Initial, theComponent.AddButton] = new State(true, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Initial, theComponent.EditButton] = new State(true, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Initial, theComponent.DeleteButton] = new State(true, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Initial, theComponent.ConfirmButton] = new State(false, rm.GetString("Confirm"), System.Drawing.Color.Gray);

                stateMachine[TransactionStateKind.SearchOnly, theComponent.AddButton] = new State(false, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.SearchOnly, theComponent.EditButton] = new State(false, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.SearchOnly, theComponent.DeleteButton] = new State(false, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.SearchOnly, theComponent.ConfirmButton] = new State(false, rm.GetString("Confirm"), System.Drawing.Color.Gray);

                stateMachine[TransactionStateKind.EditOnly, theComponent.AddButton] = new State(false, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.EditOnly, theComponent.EditButton] = new State(true, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.EditOnly, theComponent.DeleteButton] = new State(false, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.EditOnly, theComponent.ConfirmButton] = new State(false, rm.GetString("Confirm"), System.Drawing.Color.Gray);

                stateMachine[TransactionStateKind.AddEditOnly, theComponent.AddButton] = new State(true, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.AddEditOnly, theComponent.EditButton] = new State(true, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.AddEditOnly, theComponent.DeleteButton] = new State(false, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.AddEditOnly, theComponent.ConfirmButton] = new State(false, rm.GetString("Confirm"), System.Drawing.Color.Gray);

                stateMachine[TransactionStateKind.Confirm, theComponent.AddButton] = new State(true, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Confirm, theComponent.EditButton] = new State(true, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Confirm, theComponent.DeleteButton] = new State(true, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Confirm, theComponent.ConfirmButton] = new State(false, rm.GetString("Confirm"), System.Drawing.Color.Gray);

                stateMachine[TransactionStateKind.Null, theComponent.AddButton] = new State(false, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Null, theComponent.EditButton] = new State(false, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Null, theComponent.DeleteButton] = new State(false, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.Null, theComponent.ConfirmButton] = new State(false, rm.GetString("Confirm"), System.Drawing.Color.Gray);

                stateMachine[TransactionStateKind.EmptyDataSource, theComponent.AddButton] = new State(true, rm.GetString("Add"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.EmptyDataSource, theComponent.EditButton] = new State(false, rm.GetString("Edit"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.EmptyDataSource, theComponent.DeleteButton] = new State(false, rm.GetString("Delete"), System.Drawing.Color.Black);
                stateMachine[TransactionStateKind.EmptyDataSource, theComponent.ConfirmButton] = new State(false, rm.GetString("Confirm"), System.Drawing.Color.Gray);
            }
            catch (Exception baExp)
            {
                throw new baException(baExp, MethodBase.GetCurrentMethod());
            }
        }

		private void SetButtonsState(TransactionStateKind tstate)
		{
            try
            {
                theComponent.AddButton.Text = ((IState)stateMachine[tstate, theComponent.AddButton]).Text;
                theComponent.ConfirmButton.Text = ((IState)stateMachine[tstate, theComponent.ConfirmButton]).Text;
                theComponent.DeleteButton.Text = ((IState)stateMachine[tstate, theComponent.DeleteButton]).Text;
                theComponent.EditButton.Text = ((IState)stateMachine[tstate, theComponent.EditButton]).Text;

                theComponent.AddButton.Enabled = ((IState)stateMachine[tstate, theComponent.AddButton]).Enabled;
                theComponent.ConfirmButton.Enabled = ((IState)stateMachine[tstate, theComponent.ConfirmButton]).Enabled;
                theComponent.DeleteButton.Enabled = ((IState)stateMachine[tstate, theComponent.DeleteButton]).Enabled;
                theComponent.EditButton.Enabled = ((IState)stateMachine[tstate, theComponent.EditButton]).Enabled;

                theComponent.AddButton.ForeColor = ((IState)stateMachine[tstate, theComponent.AddButton]).ForeColor;
                theComponent.ConfirmButton.ForeColor = ((IState)stateMachine[tstate, theComponent.ConfirmButton]).ForeColor;
                theComponent.DeleteButton.ForeColor = ((IState)stateMachine[tstate, theComponent.DeleteButton]).ForeColor;
                theComponent.EditButton.ForeColor = ((IState)stateMachine[tstate, theComponent.EditButton]).ForeColor;
            }
            catch (Exception baExp)
            {
                throw new baException(baExp, MethodBase.GetCurrentMethod());
            }
		}
		#endregion
    }
	/// <summary>
	/// Summary description for AddEditDelCtrl
	/// </summary>
    public partial class AddEditDelCtrl : ITransactionState
	{
		private IAddEditDelete theComponent;
		private ResourceManager rm;

        #region Constructor
		public AddEditDelCtrl(IAddEditDelete theComponent, TransactionStateKind CtrlState)
		{
            try
            {
                Assembly asm = System.Reflection.Assembly.Load("Framework.UI.baResources");
                rm = new ResourceManager(Defaults.AddEditDelete(), asm);

                this.theComponent = theComponent;
                ConstructStateMachine();
                theComponent.AddClick += new AddEditDelEventHandler(theComponent_AddClick);
                theComponent.DeleteClick += new AddEditDelEventHandler(theComponent_DeleteClick);
                theComponent.EditClick += new AddEditDelEventHandler(theComponent_EditClick);
                theComponent.ConfirmClick += new AddEditDelEventHandler(theComponent_ConfirmClick);
                theComponent.TransactionStateChanged += new AddEditDelEventHandler(theComponent_TransactionStateChanged);
                TransactionState = CtrlState;
            }
            catch(Exception baExp)
            {
                throw new baException(baExp, MethodBase.GetCurrentMethod());
            }
        }

        void theComponent_TransactionStateChanged(object sender, AddEditDelEventArgs e)
        {
            TransactionState = e.TransactionState;
            SetButtonsState(e.TransactionState);
        }
        #endregion

        #region TransactionState
        public TransactionStateKind TransactionState
        {
            set
            {
                theComponent.TransactionState = value;
            }
            get
            {
                return theComponent.TransactionState;
            }
        }
        #endregion

        #region OldTransactionState
        public TransactionStateKind OldTransactionState
        {
            set
            {
                theComponent.OldTransactionState = value;
            }
            get
            {
                return theComponent.OldTransactionState;
            }
        }
        #endregion

        /// When Add/Edit/Delete buttons clicked, 
		/// their state being changed to Cancel 
		/// and they will fire Cancel action event
        /// Such as CancelAdd/CancelEdit or CancelDelete
        #region theComponent_AddClick(object sender, AddEditDelEventArgs e)
        private void theComponent_AddClick(object sender, AddEditDelEventArgs e)
		{
            e.TransactionState = ChangeTransactionState(TransactionStateKind.Add);
        }
        #endregion 

        #region theComponent_DeleteClick(object sender, AddEditDelEventArgs e)
        private void theComponent_DeleteClick(object sender, AddEditDelEventArgs e)
		{
            e.TransactionState = ChangeTransactionState(TransactionStateKind.Delete);
        }
        #endregion 

        #region theComponent_EditClick(object sender, AddEditDelEventArgs e)
        private void theComponent_EditClick(object sender, AddEditDelEventArgs e)
		{
            e.TransactionState = ChangeTransactionState(TransactionStateKind.Edit);
        }
        #endregion 

        #region theComponent_ConfirmClick(object sender, AddEditDelEventArgs e)
        private void theComponent_ConfirmClick(object sender, AddEditDelEventArgs e)
		{
            TransactionState = TransactionStateKind.Confirm;
            /// Control BindingManager state behaviour:
            /// Set its state to current state
            this.theComponent.BindingManager.TransactionState = TransactionState;
        }
        #endregion
 
        #region TransactionStateKind ChangeTransactionState(TransactionStateKind newState)
        private TransactionStateKind ChangeTransactionState(TransactionStateKind newState)
        {
            try
            {
                /// Control internal state behaviour of the controls in this component
                if ((TransactionState == TransactionStateKind.Initial)
                    || (TransactionState == TransactionStateKind.EditOnly)
                    || (TransactionState == TransactionStateKind.AddEditOnly)
                    || (TransactionState == TransactionStateKind.SearchOnly)
                    || (TransactionState == TransactionStateKind.Confirm)
                    || (TransactionState == TransactionStateKind.Null)
                    || (TransactionState == TransactionStateKind.EmptyDataSource))
                {
                    if (TransactionState != TransactionStateKind.Confirm)
                        OldTransactionState = TransactionState;
                    TransactionState = newState;
                }
                else
                {
                    //TransactionState = TransactionStateKind.Initial;
                    if (OldTransactionState == TransactionStateKind.EmptyDataSource && 
                        !this.theComponent.BindingManager.IsEmpty)
                    {
                        TransactionState = this.theComponent.BindingManager.DefaultTransactionState;
                    }
                    else
                        TransactionState = this.OldTransactionState;
                }

                /// Control BindingManager state behaviour:
                /// Set its state to current state
                this.theComponent.BindingManager.TransactionState = TransactionState;
                return TransactionState;
            }
            catch (Exception baExp)
            {
                throw new baException(baExp, MethodBase.GetCurrentMethod());
            }
        }
        #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
Software Developer (Senior)
Canada Canada
Software development experience since 1993

Skills
- Programming Languages: C# .NET, Delphi, C++, Java and VB
- Development Methodologies (SDLC): RUP, Scrum and XP
- Database: SQL server, Oracle, Apollo database and MS Access.

Educations & Certificates
- Microsoft® Certified Professional (MCP)
- Sun® Certified Java2 Programmer
- B.S. in computer science

Comments and Discussions