context: #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 System.Collections.Generic;
  using ClearCanvas.Common;
  using ClearCanvas.Common.Utilities;
  using ClearCanvas.Desktop;
  using ClearCanvas.Desktop.Actions;
  using ClearCanvas.Desktop.Tools;
  using ClearCanvas.Desktop.Tables;
  using ClearCanvas.Enterprise.Common;
  using ClearCanvas.Ris.Application.Common;
  using ClearCanvas.Ris.Application.Common.BrowsePatientData;
  using System.Diagnostics;
  
  namespace ClearCanvas.Ris.Client
  {
  	/// <summary>
  	/// Extension point for views onto <see cref="AttachedDocumentPreviewComponent"/>
  	/// </summary>
  	[ExtensionPoint]
  	public class AttachedDocumentPreviewComponentViewExtensionPoint : ExtensionPoint<IApplicationComponentView>
  	{
  	}
  
  	[ExtensionPoint]
  	public class AttachedDocumentToolExtensionPoint : ExtensionPoint<ITool>
  	{
  	}
  
  	public interface IAttachedDocumentToolContext : IToolContext
  	{
  		AttachmentSite Site { get; }
  		event EventHandler SelectedAttachmentChanged;
  		AttachmentSummary SelectedAttachment { get; }
  		AttachedDocumentSummary SelectedDocument { get; }
  
  		void AddAttachment(AttachedDocumentSummary document, EnumValueInfo category);
  		void RemoveSelectedAttachment();
  		void OpenSelectedAttachment();
  		bool IsReadonly { get; }
  
  		IDesktopWindow DesktopWindow { get; }
  	}
  
  	public enum AttachmentSite
  	{
  		Patient,
  		Order
  	}
  
  	/// <summary>
  	/// AttachedDocumentPreviewComponent class
  	/// </summary>
  	[AssociateView(typeof(AttachedDocumentPreviewComponentViewExtensionPoint))]
  	public class AttachedDocumentPreviewComponent : ApplicationComponent
  	{
  
  		class AttachedDocumentToolContext : ToolContext, IAttachedDocumentToolContext
  		{
  			private readonly AttachedDocumentPreviewComponent _component;
  
  			internal AttachedDocumentToolContext(AttachedDocumentPreviewComponent component)
  			{
  				_component = component;
  			}
  
  			#region IAttachedDocumentToolContext Members
  
  			public AttachmentSite Site
  			{
  				get { return _component._site; }
  			}
  
  			public event EventHandler SelectedAttachmentChanged
  			{
  				add { _component.SelectedDocumentChanged += value; }
  				remove { _component.SelectedDocumentChanged -= value; }
  			}
  
  			public AttachmentSummary SelectedAttachment
  			{
  				get { return _component.SelectedAttachment; }
  			}
  
  			public AttachedDocumentSummary SelectedDocument
  			{
  				get { return _component.SelectedDocument; }
  			}
  
  			public void AddAttachment(AttachedDocumentSummary document, EnumValueInfo category)
  			{
  				_component.AddAttachment(document, category);
  			}
  
  			public void RemoveSelectedAttachment()
  			{
  				_component.RemoveSelectedAttachment();
  			}
  
  			public void OpenSelectedAttachment()
  			{
  				_component.OpenAttachment();
  			}
  
  			public IDesktopWindow DesktopWindow
  			{
  				get { return _component.Host.DesktopWindow; }
  			}
  
  			public bool IsReadonly
  			{
  				get { return _component.Readonly; }
  			}
  
  			#endregion
  		}
  
  		// Summary component members
  		private AttachmentSite _site;
  		private AttachmentSummary _selectedAttachment;
  		private AttachmentSummary _initialSelection;
  		private event EventHandler _selectedDocumentChanged;
  
  		private readonly AttachmentSummaryTable _patientAttachmentTable;
  
  		private ToolSet _toolSet;
  		private readonly bool _readonly;
  
  		private EntityRef _patientProfileRef;
  		private EntityRef _orderRef;
  
  		/// <summary>
  		/// Constructor to show/hide the summary section
  		/// </summary>
  		/// <param name="readonly">True to show the summary toolbar, false to hide it</param>
  		/// <param name="site">Set the component attachment mode</param>
  		public AttachedDocumentPreviewComponent(bool @readonly, AttachmentSite site)
  		{
  			_readonly = @readonly;
  			_site = site;
  
  			_patientAttachmentTable = new AttachmentSummaryTable();
  		}
  
  		public override void Start()
  		{
  			_toolSet = new ToolSet(new AttachedDocumentToolExtensionPoint(), new AttachedDocumentToolContext(this));
  
  			if (_site == AttachmentSite.Patient)
  				LoadPatientAttachments();
  			else
  				LoadOrderAttachments();
  
  			base.Start();
  		}
  
  		public override void Stop()
  		{
  			_toolSet.Dispose();
  
  			base.Stop();
  		}
  
  		#region Events
  
  		public event EventHandler SelectedDocumentChanged
  		{
  			add { _selectedDocumentChanged += value; }
  			remove { _selectedDocumentChanged -= value; }
  		}
  
  		#endregion
  
  		#region Presentation Models
  
  		public bool Readonly
  		{
  
gt: 			get { return _readonly; }