// -- FILE ------------------------------------------------------------------ // name : CommandCollection.cs // project : Swisscom SCS // created : Jani Giannoudis - 2012.05.05 // language : c# // environment: .NET 4.0 // copyright : (c) 2004-2012 by Itenso GmbH, Switzerland // -------------------------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; namespace Itenso.Community.XamlPatterns.Windows.Input { // ------------------------------------------------------------------------ public class CommandCollection : ICommandCollection { // ---------------------------------------------------------------------- public event EventHandler CommandExecutingChanged; // ---------------------------------------------------------------------- public bool IsCommandExecuting { get { return startedCommandCount > 0; } } // IsCommandExecuting // ---------------------------------------------------------------------- public T Register<T>( T command ) where T : ICommand { if ( commands.Contains( command ) ) { throw new InvalidOperationException(); } command.Started += CommandStartedHandler; command.Finished += CommandFinishedHandler; commands.Add( command ); return command; } // Register // ---------------------------------------------------------------------- public void Unregister<T>( T command ) where T : ICommand { if ( !commands.Contains( command ) ) { throw new InvalidOperationException(); } command.Started -= CommandStartedHandler; command.Finished -= CommandFinishedHandler; commands.Remove( command ); } // Unregister // ---------------------------------------------------------------------- public void UnregisterAll() { while ( commands.Count > 0 ) { Unregister( commands[ 0 ] ); } } // UnregisterAll // ---------------------------------------------------------------------- public void Dispose() { Dispose( true ); GC.SuppressFinalize( this ); } // Dispose // ---------------------------------------------------------------------- public IEnumerator<ICommand> GetEnumerator() { return commands.GetEnumerator(); } // GetEnumerator // ---------------------------------------------------------------------- IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } // IEnumerable.GetEnumerator // ---------------------------------------------------------------------- protected virtual void Dispose( bool disposing ) { if ( disposing ) { foreach ( ICommand command in commands ) { command.Dispose(); } UnregisterAll(); } } // Dispose // ---------------------------------------------------------------------- protected virtual void OnCommandStarted() { startedCommandCount++; if ( startedCommandCount == 1 ) { OnCommandExecutingChanged(); } } // OnCommandStarted // ---------------------------------------------------------------------- private void OnCommandFinished() { if ( startedCommandCount == 0 ) { throw new InvalidOperationException(); } startedCommandCount--; if ( startedCommandCount == 0 ) { OnCommandExecutingChanged(); } } // CommandStarted // ---------------------------------------------------------------------- protected virtual void OnCommandExecutingChanged() { EventHandler commandExecutingChanged = CommandExecutingChanged; if ( commandExecutingChanged != null ) { commandExecutingChanged( this, EventArgs.Empty ); } } // OnCommandExecutingChanged // ---------------------------------------------------------------------- private void CommandStartedHandler( object sender, EventArgs e ) { OnCommandStarted(); } // CommandStartedHandler // ---------------------------------------------------------------------- private void CommandFinishedHandler( object sender, EventArgs e ) { OnCommandFinished(); } // CommandFinishedHandler // ---------------------------------------------------------------------- // members private readonly List<ICommand> commands = new List<ICommand>(); private int startedCommandCount; } // class CommandCollection } // namespace Itenso.Community.XamlPatterns.Windows.Input // -- EOF -------------------------------------------------------------------
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.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)