Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

A .NET State Machine Toolkit - Part III

, 26 Oct 2006
Using code generation with the .NET state machine toolkit.
statemachinetoolkitpartiii_demo.zip
StateMachineToolkit
StateMachineBuilderDemo
App.ico
StateMachineBuilderDemo.csproj.user
StateMachineDemo
App.ico
StateMachineDemo.csproj.user
StateMachineMaker
App.ico
StateMachineMaker.csproj.user
StateMachineToolkit
bin
Debug
LSCollections.dll
CodeGeneration
StateMachineBuilder
StateMachineToolkit.csproj.user
statemachinetoolkitpartiii_src.zip
StateMachineToolkitPartIII_src
CodeGeneration
/*
 * Created by: Leslie Sanford
 * 
 * Contact: jabberdabber@hotmail.com
 * 
 * Last modified: 10/07/2005
 */

using System;
using System.Collections;
using System.ComponentModel;

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

        #region Construction

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

        #endregion

        #region Methods

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

        /// <summary>
        /// Creates a TransitionRow with the specified event, guard, action 
        /// and target and adds it to the TransitionRowCollection.
        /// </summary>
        /// <param name="event">
        /// The event that raised the transition.
        /// </param>
        /// <param name="guard">
        /// The guard to evaluate whether or not the transition should fire.
        /// </param>
        /// <param name="action">
        /// The action to perform if the transition fires.
        /// </param>
        /// <param name="target">
        /// The target state of the transition.
        /// </param>
        /// <returns>
        /// The position into which the TransitionRow was inserted into the 
        /// TransitionRowCollection.
        /// </returns>
        public int Add(string @event, string guard, string action, string target)
        {
            TransitionRow newRow = new TransitionRow();

            newRow.Event = @event;
            newRow.Guard = guard;
            newRow.Action = action;
            newRow.Target = target;

            return Add(newRow);
        }

        protected override void OnClear()
        {
            foreach(TransitionRow 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)
        {
            TransitionRow newRow = (TransitionRow)value;

            newRow.EditCancelled += new EventHandler(EditCancelledHandler);

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

            base.OnInsertComplete (index, value);
        }

        protected override void OnRemoveComplete(int index, object value)
        {
            TransitionRow oldRow = (TransitionRow)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)
            {
                TransitionRow oldRow = (TransitionRow)oldValue;
                TransitionRow newRow = (TransitionRow)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 TransitionRow at the specified index.
        /// </summary>
        public TransitionRow this[int index]
        {
            get
            {
                return (TransitionRow)List[index];
            }
            set
            {
                List[index] = value;
            }
        }

        #endregion

        #endregion

        #region IBindingList Members

        public event System.ComponentModel.ListChangedEventHandler ListChanged;

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

            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)

Share

About the Author

Leslie Sanford

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 26 Oct 2006
Article Copyright 2005 by Leslie Sanford
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid