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;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Globalization;
  using System.Linq;
  using System.Text;
  using ClearCanvas.Common;
  using ClearCanvas.Common.Utilities;
  using ClearCanvas.Desktop;
  using ClearCanvas.Desktop.Actions;
  using ClearCanvas.Desktop.Tables;
  using ClearCanvas.Desktop.Tools;
  using ClearCanvas.Dicom.ServiceModel.Query;
  using ClearCanvas.Dicom.Utilities;
  using ClearCanvas.ImageViewer.Common;
  using ClearCanvas.ImageViewer.Common.StudyManagement;
  using ClearCanvas.ImageViewer.Configuration.ServerTree;
  
  namespace ClearCanvas.ImageViewer.Explorer.Dicom
  {
  	[ExtensionPoint]
  	public sealed class StudyBrowserToolExtensionPoint : ExtensionPoint<ITool>
  	{
  	}
  
  	[ExtensionPoint]
  	public sealed class StudyBrowserComponentViewExtensionPoint : ExtensionPoint<IApplicationComponentView>
  	{
  	}
  
  	public interface IStudyBrowserToolContext : IToolContext
  	{
          StudyTableItem SelectedStudy { get; }
  
          ReadOnlyCollection<StudyTableItem> SelectedStudies { get; }
  
  		DicomServiceNodeList SelectedServers { get; }
  
  		event EventHandler SelectedStudyChanged;
  
  		event EventHandler SelectedServerChanged;
  
  		ClickHandlerDelegate DefaultActionHandler { get; set; }
  
  	    void RefreshStudyTable();
  
  		IDesktopWindow DesktopWindow { get; }
      }
  
  	[AssociateView(typeof(StudyBrowserComponentViewExtensionPoint))]
  	public class StudyBrowserComponent : ApplicationComponent, IStudyBrowserComponent
  	{
  #if DEBUG
          #region Fake Study Query Tool
  
          [MenuAction("populate", "dicomstudybrowser-contextmenu/Populate with 10000 studies", "Populate")]
          [ExtensionOf(typeof(StudyBrowserToolExtensionPoint), Enabled = false)]
  	    internal class FakeStudyQueryTool : StudyBrowserTool
          {
              private int _nextPatientId = 1;
              private int _dateCount = 0;
  
              public void Populate()
              {
                  var context = (StudyBrowserToolContext) base.Context;
                  var items = new List<StudyTableItem>();
                  var now = DateTime.Now;
  
                  for (int i = 0; i < 10000; ++i)
                  {
                      var idString = _nextPatientId.ToString(CultureInfo.InvariantCulture);
                      var entry = new StudyEntry();
                      var study = new StudyRootStudyIdentifier
                                      {
                                          PatientsName = String.Format("Test{0}^Patient{0}", idString),
                                          PatientId = idString,
                                          AccessionNumber = "A" + idString,
                                          InstanceAvailability = "ONLINE",
                                          ModalitiesInStudy = new[] {"MR"},
                                          NumberOfStudyRelatedInstances = 10,
                                          NumberOfStudyRelatedSeries = 5,
                                          StudyDescription = "Study" + idString,
                                          StudyInstanceUid = idString,
                                          StudyDate = now.ToString(DateParser.DicomDateFormat)
                                      };
  
                      if (_dateCount >= 5)
                          _dateCount = 0;
  
                      var deleteOn = now.AddDays(_dateCount);
                      ++_dateCount;
  
                      entry.Study = study;
                      entry.Data = new StudyEntryData {DeleteTime = deleteOn};
                      var item = new StudyTableItem(entry);
  
                      items.Add(item);
                      ++_nextPatientId;
                  }
  
                  var searchResult = context.Component.CurrentSearchResult;
                  searchResult.SearchEnded(items, false);
              }
  
              protected override void OnSelectedServerChanged(object sender, EventArgs e)
              {
                  Enabled = Visible = true;
              }
  
              protected override void OnSelectedStudyChanged(object sender, EventArgs e)
              {
                  Enabled = Visible = true;
              }
          }
          #endregion
  #endif
  
          #region Tool Context
  
          private class StudyBrowserToolContext : ToolContext, IStudyBrowserToolContext
  		{
  			public StudyBrowserToolContext(StudyBrowserComponent component)
  			{
  				Platform.CheckForNullReference(component, "component");
                  Component = component;
  			}
  
              internal StudyBrowserComponent Component { get; private set; }
  
  			#region IStudyBrowserToolContext Members
  
              public StudyTableItem SelectedStudy
  			{
  				get
  				{
  					return Component.SelectedStudy;
  				}
  			}
  
              public ReadOnlyCollection<StudyTableItem> SelectedStudies
  			{
  				get
  				{
                      return Component.SelectedStudies;
  				}
  			}
  
  			public DicomServiceNodeList SelectedServers
  			{
                  get { return Component._selectedServers; }
  			}
  
  			public event EventHandler SelectedStudyChanged
  			{
                  add { Component.SelectedStudyChanged += value; }
                  remove { Component.SelectedStudyChanged -= value; }
  			}
  
  			public event EventHandler SelectedServerChanged
  			{
                  add { Component.SelectedServerChanged += value; }
                  remove { Component.SelectedServerChanged -= value; }
  			}
  
  			public ClickHandlerDelegate DefaultActionHandler
  			{
                  get { return Component._defaultActionHandler; }
                  set { Component._defaultActionHandler = value; }
  			}
  
  			public IDesktopWindow DesktopWindow
  			{
                  get { return Component.Host.DesktopWindow; }
  			}
  
  			public void RefreshStudyTable()
  			{
  				try
  				{
                      Component.Search(Component._lastQueryCriteria);
  				}
  				catch (Exception e)
  				{
                      ExceptionHandler.Report(e, Component.Host.DesktopWindow);
  				}
  			}
  
  			#endregion
  		}
  
  		#endregion
  
  		#region Fields
  
  		private StudyRootStudyIdentifier _lastQueryCriteria;
  		private readonly Dictionary<string, SearchResult> _searchResults;
  	    private SearchResult _currentSearchResult;
  
  		private readonly Table<StudyTableItem> _dummyStudyTable;
  		private event EventHandler _studyTableChanged;
  		private bool _filterDuplicateStudies = true;
  
  		private ISelection _currentSelection;
  		private event EventHandler _selectedStudyChangedEvent;
  
  		private DicomServiceNodeList _selectedServers = new DicomServiceNodeList();
  		private event EventHandler _selectedServerChangedEvent;
  
  		private ToolSet _toolSet;
  		private ClickHandlerDelegate _defaultActionHandler;
  
  		private ActionModelRoot _toolbarModel;
  		private ActionModelRoot _contextMenuModel;
  
  		private SearchResultColumnOptionCollection _searchResultColumnOptions;
  
  		private bool _isEnabled = true;
  
  	    #endregion
  
  		public StudyBrowserComponent()
  		{
  			_dummyStudyTable = new Table<StudyTableItem>();
  			_searchResults = new Dictionary<string, SearchResult>();
              _lastQueryCriteria = new StudyRootStudyIdentifier();
  		}
  
  		#region Properties/Events
  
  		public DicomServiceNodeList SelectedServers
  		{
  			get { return _selectedServers; }
  			set
  			{
  
gt:                 if (ReferenceEquals(value, _selectedServers))