#region License
// Copyright (c) 2013, ClearCanvas Inc.
// All rights reserved.
// http://www.clearcanvas.ca
//
// This file is part of the ClearCanvas RIS/PACS open source project.
//
// The ClearCanvas RIS/PACS open source project is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// The ClearCanvas RIS/PACS open source project is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// the ClearCanvas RIS/PACS open source project.  If not, see
// <http://www.gnu.org/licenses/>.
#endregion
using System;
using ClearCanvas.Common;
using ClearCanvas.Common.Utilities;
using ClearCanvas.Desktop;
using ClearCanvas.Desktop.Trees;
using ClearCanvas.Desktop.Tables;
using ClearCanvas.Desktop.Actions;
namespace ClearCanvas.Ris.Client
{
    /// <summary>
    /// Extension point for views onto <see cref="FolderExplorerComponent"/>
    /// </summary>
    [ExtensionPoint]
    public class FolderExplorerComponentViewExtensionPoint : ExtensionPoint<IApplicationComponentView>
    {
    }
    /// <summary>
    /// WorklistExplorerComponent class
    /// </summary>
    [AssociateView(typeof(FolderExplorerComponentViewExtensionPoint))]
    public class FolderExplorerComponent : ApplicationComponent, IFolderExplorerComponent
    {
		enum InitializationState
		{
			NotInitialized,
			Initializing,
			Initialized
		}
		private readonly FolderTreeRoot _folderTreeRoot;
		private FolderTreeNode _selectedTreeNode;
        private event EventHandler _selectedFolderChanged;
    	private event EventHandler _intialized;
		private InitializationState _initializationState;
        private readonly IFolderSystem _folderSystem;
    	private Timer _folderInvalidateTimer;
    	private readonly FolderExplorerGroupComponent _owner;
        /// <summary>
        /// Constructor
        /// </summary>
        public FolderExplorerComponent(IFolderSystem folderSystem, FolderExplorerGroupComponent owner)
        {
			_folderTreeRoot = new FolderTreeRoot(this);
            _folderSystem = folderSystem;
        	_owner = owner;
        }
		#region IFolderExplorerComponent implementation
    	/// <summary>
    	/// Gets a value indicating whether this folder explorer has already been initialized.
    	/// </summary>
    	bool IFolderExplorerComponent.IsInitialized
    	{
			get { return IsInitialized; }
    	}
    	/// <summary>
    	/// Instructs the folder explorer to initialize (build the folder system).
    	/// </summary>
    	void IFolderExplorerComponent.Initialize()
		{
			Initialize();
		}
		/// <summary>
		/// Occurs when asynchronous initialization of this folder system has completed.
		/// </summary>
		event EventHandler IFolderExplorerComponent.Initialized
		{
			add { _intialized += value; }
			remove { _intialized -= value; }
		}
		/// <summary>
		/// Gets or sets the currently selected folder.
		/// </summary>
		IFolder IFolderExplorerComponent.SelectedFolder
		{
			get { return this.SelectedFolder; }
			set
			{
				this.SelectedFolder = value;
			}
		}
		/// <summary>
		/// Invalidates all folders.
		/// </summary>
    	void IFolderExplorerComponent.InvalidateFolders()
		{
			// check initialized
			if (!IsInitialized)
				return;
			// invalidate all folders, and update starting at the root
			_folderSystem.InvalidateFolders();
		}
    	/// <summary>
    	/// Gets the underlying folder system associated with this folder explorer.
    	/// </summary>
    	IFolderSystem IFolderExplorerComponent.FolderSystem
		{
			get { return _folderSystem; }
		}
    	/// <summary>
    	/// Occurs when the selected folder changes.
    	/// </summary>
    	event EventHandler IFolderExplorerComponent.SelectedFolderChanged
		{
			add { _selectedFolderChanged += value; }
			remove { _selectedFolderChanged -= value; }
		}
		/// <summary>
		/// Executes a search on this folder system.
		/// </summary>
		/// <param name="searchParams"></param>
		void IFolderExplorerComponent.ExecuteSearch(SearchParams searchParams)
		{
			// check initialized
			if (!IsInitialized)
				return;
			if (_folderSystem.SearchEnabled)
				_folderSystem.ExecuteSearch(searchParams);
		}
		void IFolderExplorerComponent.LaunchAdvancedSearchComponent()
		{
			_folderSystem.LaunchSearchComponent();
		}
    	/// <summary>
    	/// Gets the application component that displays the content of a folder for this folder system.
    	/// </summary>
    	/// <returns></returns>
    	IApplicationComponent IFolderExplorerComponent.GetContentComponent()
    	{
    		return _folderSystem.GetContentComponent();
    	}
    	#endregion
		#region Application Component overrides
		public override void Start()
        {
			// if the folder system needs immediate initialization, do that now
			if(!_folderSystem.LazyInitialize)
			{
				Initialize();
			}
        	base.Start();
        }
    	public override void Stop()
		{
			if (_folderInvalidateTimer != null)
			{
				_folderInvalidateTimer.Stop();
				_folderInvalidateTimer.Dispose();
			}
			// un-subscribe to events (important because the folderSystem object may be re-used by another explorer)
			_folderSystem.Folders.ItemAdded -= FolderAddedEventHandler;
			_folderSystem.Folders.ItemRemoved -= FolderRemovedEventHandler;
			_folderSystem.FoldersChanged -= FoldersChangedEventHandler;
			_folderSystem.FoldersInvalidated -= FoldersInvalidatedEventHandler;
			_folderSystem.FolderPropertiesChanged -= FolderPropertiesChangedEventHandler;
			_folderSystem.Dispose();
			base.Stop();
		}
        public override IActionSet ExportedActions
        {
            get 
            { 
                return _folderSystem.FolderTools == null
                    ? new ActionSet()
                    : _folderSystem.FolderTools.Actions; 
            }
        }
        #endregion
        #region Presentation Model
    	public ITree FolderTree
        {
			get { return _folderTreeRoot.GetSubTree(); }
        }
        public ISelection SelectedFolderTreeNode
        {
            get { return new Selection(_selectedTreeNode); }
            set
            {
				var nodeToSelect = (FolderTreeNode)value.Item;
                SelectFolder(nodeToSelect);
            }
        }
        public ITable FolderContentsTable
        {
            get { return _selectedTreeNode == null ? null : _selectedTreeNode.Folder.ItemsTable; }
        }
        public event EventHandler SelectedFolderChanged
        {


Answer:             add { _selectedFolderChanged += value; }