Click here to Skip to main content
15,892,298 members
Articles / Web Development / ASP.NET

Developing Next Generation Smart Clients using .NET 2.0 working with Existing .NET 1.1 SOA-based XML Web Services

Rate me:
Please Sign up or sign in to vote.
4.96/5 (134 votes)
16 Aug 200540 min read 1.2M   3.9K   462  
Comprehensive guide to development of .NET 2.0 Smart Clients working with existing Service Oriented Architecture based XML web services, fully utilizing the Enterprise Library
using System;
namespace SmartInstitute.ObjectModel
{
	/// <summary>
	/// Event fired when selection changes. Whenever an item is added or removed from the 
	/// selection list, the event gets fired. The "item" contains the item which was added/removed.
	/// When "Clear" is invoked on the selected items collection, the event also fires, but
	/// the "item" is null.
	/// </summary>
	public delegate void SelectionChangeHandler( CollectionBase collection, ItemBase item );

	/// <summary>
	/// Summary description for SelectableCollectionBase.
	/// </summary>
	[Serializable]
	public class SelectableCollection<T> : GenericCollection<T> where
	T : ItemBase
	{
		public event SelectionChangeHandler OnSelectionChanged;

		
		private bool _MultiSelect = true;

		/// <summary>
		/// When multiselect is set, it allowes multiple child item to be selected. Otherwise
		/// only one child item is selected at a time
		/// </summary>
		public bool MultiSelect
		{
			get
			{
				return _MultiSelect;
			}
			set
			{
				_MultiSelect = value;
			}
		}

		public void NotifySelectionChanged( ItemBase selectedItem )
		{
			if( null != OnSelectionChanged )
			{
				OnSelectionChanged( this, selectedItem );
			}
		}

		private T _Selected;

		public T Selected
		{
			get
			{
				return _Selected;
			}
			set
			{
				if( value != _Selected )
				{
					if( null == value )
					{
						// Selection cleared
						this.SelectedItems.Clear( );
					}
					else
					{
						// add the item in the selected list if not added already
						if( this.SelectedItems.Contains( value ) == false )
						{
							// for single selection, clear previous selections
							this.SelectedItems.Clear();
							this.SelectedItems.Add( value );
						}

						if( !value.Selected )
							value.Selected = true;
					}

					this._Selected = value;
				}
				else
				{
					// currently selected item is selected again
				}
			}
		}

		/// <summary>
		/// Add new item to the collection
		/// </summary>
		/// <param name="item"></param>

		public override int Add( T item )
		{
			item.OnSelect += new ItemSelectHandler( item_OnSelect );
			return base.Add( item );
		}

		private void item_OnSelect( ItemBase item )
		{
			// Make sure the item being selected is contained within this collection,
			// not any nested item or collection
			if( item.Parent != this )
				return;

			if( this.MultiSelect )
			{
				if( item.Selected )
				{
					// Item is selected. Avoid duplicate adding in selected items
					if( this.SelectedItems.Contains( (T)item ) == false )
					{
						this.SelectedItems.Add((T)item);
					}
				}
				else
				{
					// Item is unselected, remove from the selected items collection
					SelectedItems.Remove((T)item);
				}
			}
			else
			{
				// In single selection mode, only one item can be select at a given time
				if( item.Selected )
				{
					this.Selected = (T)item;
				}
				else
				{
					// Item unselected, if this is the item being selected, remove selection
					// otherwise leave the currently selected item as is.
					if( SelectedItems.Contains( (T)item ) )
					{
						SelectedItems.Clear( );
					}
				}
			}
		}

		new public void Clear( )
		{
			this.Selected = null;
			base.Clear();
		}

		public readonly GenericCollection<T> SelectedItems;

		public SelectableCollection( bool multiSelect, object parent ) : base( parent )
		{
			this.MultiSelect = multiSelect;

			SelectedItems = new GenericCollection<T>( this );
			SelectedItems.OnItemCollectionAdd += new CollectionAddHandler( SelectedItems_OnAdd );
			SelectedItems.OnItemCollectionClear += new CollectionClearHandler( SelectedItems_OnClear );
			SelectedItems.OnItemCollectionRemove += new CollectionRemoveHandler( SelectedItems_OnRemove );
		}

		private void SelectedItems_OnAdd( CollectionBase collection, ItemBase item )
		{
			if( collection == this.SelectedItems )
			{
				this._Selected = (T)item;
				NotifySelectionChanged( item );
			}
		}

		private void SelectedItems_OnClear( CollectionBase collection )
		{
			if( collection == this.SelectedItems )
			{
				this._Selected = null;
				NotifySelectionChanged( null );
			}
		}

		private void SelectedItems_OnRemove( CollectionBase collection, ItemBase item )
		{
			if( collection == this.SelectedItems )
			{
				// if the item being removed from the selected items is not unselected, unselect it
				if( false != item.Selected )
				{
					item.Selected = false;
					NotifySelectionChanged( item );
				}

				// if this is the item currently selected
				if( this.Selected == item )
				{
					if( this.MultiSelect )
					{
						// select the last item in the selection list, if any
						if( this.SelectedItems.Count > 0 )
							this._Selected = this.SelectedItems[ this.SelectedItems.Count - 1 ];
						else
							this._Selected = null;

						NotifySelectionChanged( item );
					}
					else
					{
						// if single selection mode, then , then there is no current selection
						this._Selected = null;

						NotifySelectionChanged( item );
					}
				
				}
			}
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions