Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

A .NET State Machine Toolkit - Part III

Rate me:
Please Sign up or sign in to vote.
4.91/5 (43 votes)
26 Oct 2006CPOL11 min read 223.4K   1.2K   135  
Using code generation with the .NET state machine toolkit.
/*
 * Created by: Leslie Sanford
 * 
 * Contact: jabberdabber@hotmail.com
 * 
 * Last modified: 10/05/2005
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Xml.Serialization;

namespace StateMachineToolkit
{
	/// <summary>
	/// Represents a collection of StateRows.
	/// </summary>
	public class StateRowCollection : CollectionBase, IBindingList
	{ 
        #region StateRowCollection Members

        #region Construction

        /// <summary>
        /// Initializes a new instance of the StateRowCollection class.
        /// </summary>
		public StateRowCollection()
		{
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds a StateRow to the StateRowCollection.
        /// </summary>
        /// <param name="row">
        /// The StateRow to add to the StateRowCollection.
        /// </param>
        /// <returns>
        /// The position into which the StateRow was inserted into the 
        /// StareRowCollection.
        /// </returns>
        public int Add(StateRow row)
        {
            return List.Add(row);
        }

        /// <summary>
        /// Creates a StateRow based on the specified name and adds it to the
        /// StateRowCollection.
        /// </summary>
        /// <param name="name">
        /// The state name.
        /// </param>
        /// <returns>
        /// The position into which the StateRow was inserted into the 
        /// StareRowCollection.
        /// </returns>
        public int Add(string name)
        {
            StateRow newRow = new StateRow();

            newRow.Name = name;

            return Add(newRow);
        }

        /// <summary>
        /// Creates a StateRow based on the specified name and initial state 
        /// and adds it to the StateRowCollection.
        /// </summary>
        /// <param name="name">
        /// The state name.
        /// </param>
        /// <param name="initialState">
        /// The state's initial state.
        /// </param>
        /// <returns>
        /// The position into which the StateRow was inserted into the 
        /// StareRowCollection.
        /// </returns>
        public int Add(string name, string initialState)
        {
            StateRow newRow = new StateRow();

            newRow.Name = name;
            newRow.InitialState = initialState;

            return Add(newRow);
        }

        /// <summary>
        /// Creates a StateRow based on the specified name, initial state, and
        /// history type and adds it to the StateRowCollection.
        /// </summary>
        /// <param name="name">
        /// The state name.
        /// </param>
        /// <param name="initialState">
        /// The state's initial state.
        /// </param>
        /// <param name="historyType">
        /// The state's history type.
        /// </param>
        /// <returns>
        /// The position into which the StateRow was inserted into the 
        /// StareRowCollection.
        /// </returns>
        public int Add(string name, string initialState, HistoryType historyType)
        {
            StateRow newRow = new StateRow();

            newRow.Name = name;
            newRow.InitialState = initialState;
            newRow.HistoryType = historyType;

            return Add(newRow);
        }

        protected override void OnClear()
        {
            foreach(StateRow row in List)            
            {
                row.EditCancelled -= new EventHandler(EditCancelledHandler);
            }

            base.OnClear ();
        }

        protected override void OnClearComplete()
        {
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

            base.OnClearComplete();
        }

        protected override void OnInsertComplete(int index, object value)
        {
            StateRow newRow = (StateRow)value;

            newRow.EditCancelled += new EventHandler(EditCancelledHandler);

            OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));

            base.OnInsertComplete (index, value);
        }

        protected override void OnRemoveComplete(int index, object value)
        {
            StateRow oldRow = (StateRow)value;

            oldRow.EditCancelled -= new EventHandler(EditCancelledHandler);

            OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));

            base.OnRemoveComplete (index, value);
        }

        protected override void OnSetComplete(int index, object oldValue, object newValue)
        {
            if(oldValue != newValue)
            {
                StateRow oldRow = (StateRow)oldValue;
                StateRow newRow = (StateRow)newValue;

                oldRow.EditCancelled -= new EventHandler(EditCancelledHandler);
                newRow.EditCancelled += new EventHandler(EditCancelledHandler);

                OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
            }

            base.OnSetComplete (index, oldValue, newValue);
        }

        private void OnListChanged(ListChangedEventArgs e)
        {
            ListChangedEventHandler handler = ListChanged;

            if(handler != null)
            {
                handler(this, e);
            }
        }

        private void EditCancelledHandler(object sender, EventArgs e)
        {
            List.Remove(sender);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the StateRow at the specified index.
        /// </summary>
        public StateRow this[int index]
        {
            get
            {
                return (StateRow)List[index];
            }
            set
            {
                List[index] = value;
            }
        }

        #endregion

        #endregion

        #region IBindingList Members

        public event System.ComponentModel.ListChangedEventHandler ListChanged;

        public object AddNew()
        {
            StateRow newRow = new StateRow();

            List.Add(newRow);

            return newRow;
        }

        public void AddIndex(PropertyDescriptor property)
        {
            throw new NotSupportedException();
        }        

        public void ApplySort(PropertyDescriptor property, System.ComponentModel.ListSortDirection direction)
        {
            throw new NotSupportedException();
        }        

        public int Find(PropertyDescriptor property, object key)
        {
            throw new NotSupportedException();
        }

        public void RemoveSort()
        {
            throw new NotSupportedException();
        }

        public void RemoveIndex(PropertyDescriptor property)
        {
            throw new NotSupportedException();
        }

        public bool AllowNew
        {
            get
            {
                return true;
            }
        }        

        public bool AllowEdit
        {
            get
            {
                return true;
            }
        }

        public bool AllowRemove
        {
            get
            {
                return true;
            }
        }

        public PropertyDescriptor SortProperty
        {
            get
            {
                throw new NotSupportedException();
            }
        }

        public bool SupportsChangeNotification
        {
            get
            {
                return true;
            }
        }

        public bool SupportsSorting
        {
            get
            {
                return false;
            }
        }

        public bool SupportsSearching
        {
            get
            {
                return false;
            }
        }

        public bool IsSorted
        {
            get
            {
                throw new NotSupportedException();
            }
        }        

        public System.ComponentModel.ListSortDirection SortDirection
        {
            get
            {
                throw new NotSupportedException();
            }
        }
        
        #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
United States United States
Aside from dabbling in BASIC on his old Atari 1040ST years ago, Leslie's programming experience didn't really begin until he discovered the Internet in the late 90s. There he found a treasure trove of information about two of his favorite interests: MIDI and sound synthesis.

After spending a good deal of time calculating formulas he found on the Internet for creating new sounds by hand, he decided that an easier way would be to program the computer to do the work for him. This led him to learn C. He discovered that beyond using programming as a tool for synthesizing sound, he loved programming in and of itself.

Eventually he taught himself C++ and C#, and along the way he immersed himself in the ideas of object oriented programming. Like many of us, he gotten bitten by the design patterns bug and a copy of GOF is never far from his hands.

Now his primary interest is in creating a complete MIDI toolkit using the C# language. He hopes to create something that will become an indispensable tool for those wanting to write MIDI applications for the .NET framework.

Besides programming, his other interests are photography and playing his Les Paul guitars.

Comments and Discussions