using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using System.Windows; using DTS.Common; using DTS.Common.Base; using DTS.Common.Events; using DTS.Common.Interface; using DTS.Common.Utils; using Microsoft.Practices.Prism.Events; using Microsoft.Practices.Prism.Interactivity.InteractionRequest; using Microsoft.Practices.Prism.Regions; using Microsoft.Practices.Unity; namespace DTS.Viewer { public class MainViewModel : BaseViewModel, IMainViewModel { public IBaseView View { get; private set; } private IBaseWindowModel Parent { get; set; } private IEventAggregator _eventAggregator { get; set; } private new IRegionManager _regionManager; private IUnityContainer _unityContainer { get; set; } public InteractionRequest NotificationRequest { get; private set; } public new InteractionRequest ConfirmationRequest { get; private set; } /// /// Creates a new instance of the MainViewModel. /// /// The MainView View. /// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed. /// The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other. /// The Unity container. public MainViewModel(IMainView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) : base(regionManager, eventAggregator, unityContainer) { View = view; View.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; _unityContainer = unityContainer; _regionManager = regionManager; _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); } #region Methods /// /// Initialize without parameter /// public override void Initialize() { } /// /// Initialize with parameter - Parent /// public override void Initialize(object parameter) { Parent = (IBaseWindowModel)parameter; Parent.IsMenuIncluded = _isMenuIncluded; Parent.IsNavigationIncluded = _isNavigationIncluded; _eventAggregator.GetEvent().Subscribe(OnAssemblyListChange, ThreadOption.PublisherThread, true); _eventAggregator.GetEvent().Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true); View.DataContext = this; } public override void Activated() { } /// /// Display loaded assemblies /// /// List of loaded assemblies private void OnAssemblyListChange(AssemblyListInfo e) { var assemblyList = new List(); foreach (var assembly in e.AssemblyList) { assemblyList.AddRange(from attribute in assembly.GetCustomAttributes() where attribute.GetType().Name.EndsWith("ImageAttribute") select new AssemblyNameImage { AssemblyGroup = ((ImageAttribute)attribute).GetAssemblyGroup(), AssemblyName = Regex.Replace(((ImageAttribute)attribute).GetAssemblyName(), "(\\B[A-Z])", " $1"), AssemblyImage = ((ImageAttribute)attribute).GetAssemblyImage(), AssemblyRegion = ((ImageAttribute)attribute).GetAssemblyRegion() }); } foreach (var assembly in assemblyList) { switch (assembly.AssemblyRegion) { case eAssemblyRegion.NavigationRegion: break; case eAssemblyRegion.GraphRegion: ContextGraphRegion = GetGraphView(Parent); break; case eAssemblyRegion.StatsRegion: break; case eAssemblyRegion.CursorRegion: break; case eAssemblyRegion.DiagRegion: break; case eAssemblyRegion.TestsRegion: break; case eAssemblyRegion.GraphsRegion: break; case eAssemblyRegion.LegendRegion: break; case eAssemblyRegion.PropertyRegion: ContextPropertyRegion = GetPropertyView(Parent); break; } } } /// /// Private Event handler for RaiseNotification event. /// private void OnRaiseNotification(NotificationContentEventArgs eventArgsWithTitle) { // Notification object expects a NotificationContentEventArgsWithoutTitle object and a Title string. var eventArgsWithoutTitle = new NotificationContentEventArgs(eventArgsWithTitle.Message, eventArgsWithTitle.MessageDetails, eventArgsWithTitle.Image); NotificationRequest.Raise(new Notification { Content = eventArgsWithoutTitle, Title = eventArgsWithTitle.Title }); } /// /// Private Event handler for RaiseNotification event. /// private void OnBusyIndicatorNotification(bool eventArg) { IsBusy = eventArg; } #endregion #region Views private INavigationView GetNavigationView(IBaseViewModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } private IPropertyView GetPropertyView(IBaseWindowModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } private IGraphView GetGraphView(IBaseWindowModel parent) { var view = _unityContainer.Resolve(); var viewModel = _unityContainer.Resolve(); view.DataContext = viewModel; viewModel.Initialize(parent); return view; } #endregion Views #region ContextRegion public object ContextNavigationRegion { get { return ((MainView)View).NavigationRegion.Content; } set { ((MainView)View).NavigationRegion.Content = value; OnPropertyChanged("ContextNavigationRegion"); } } #region Graph public object ContextGraphRegion { get { return ((MainView)View).GraphRegion.Content; } set { ((MainView)View).GraphRegion.Content = value; OnPropertyChanged("ContextGraphsRegion"); } } #endregion Graph public object ContextTestsRegion { get { return ((MainView)View).TestsRegion.Content; } set { ((MainView)View).TestsRegion.Content = value; OnPropertyChanged("ContextTestsRegion"); } } public object ContextGraphsRegion { get { return ((MainView)View).GraphsRegion.Content; } set { ((MainView)View).GraphsRegion.Content = value; OnPropertyChanged("ContextGraphsRegion"); } } public object ContextLegendRegion { get { return ((MainView)View).LegendRegion.Content; } set { ((MainView)View).LegendRegion.Content = value; OnPropertyChanged("ContextLegendRegion"); } } public object ContextDiagRegion { get { return ((MainView)View).DiagRegion.Content; } set { ((MainView)View).DiagRegion.Content = value; OnPropertyChanged("ContextDiagRegion"); } } public object ContextStatsRegion { get { return ((MainView)View).StatsRegion.Content; } set { ((MainView)View).StatsRegion.Content = value; OnPropertyChanged("ContextStatsRegion"); } } public object ContextCursorRegion { get { return ((MainView)View).CursorRegion.Content; } set { ((MainView)View).CursorRegion.Content = value; OnPropertyChanged("ContextCursorRegion"); } } public object ContextPropertyRegion { get { return ((MainView)View).PropertyRegion.Content; } set { ((MainView)View).PropertyRegion.Content = value; OnPropertyChanged("ContextPropertyRegion"); } } public List GetRegions() { var items = new List(); Utils.GetChildrenByName(((MainView)View).MainShell, "Region", ref items); return items; } #endregion #region Properties //List _assemblyGroupList = new List(); //public List AssemblyGroupList //{ // get { return _assemblyGroupList; } // set { _assemblyGroupList = value; OnPropertyChanged("AssemblyGroupList"); } //} /// ///Occurs when a property value changes. /// public new event PropertyChangedEventHandler PropertyChanged; private new void OnPropertyChanged(string propertyName) { var eventHandler = PropertyChanged; if (eventHandler != null) { eventHandler(this, new PropertyChangedEventArgs(propertyName)); } } private bool _isBusy = false; public new bool IsBusy { get { return _isBusy; } set { _isBusy = value; OnPropertyChanged("IsBusy"); } } private string _isBusyMessage = string.Empty; public new string IsBusyMessage { get { return _isBusyMessage; } set { _isBusyMessage = value; OnPropertyChanged("IsBusyMessage"); } } private bool _isMenuIncluded = false; public new bool IsMenuIncluded { get { return _isMenuIncluded; } set { _isMenuIncluded = value; OnPropertyChanged("IsMenuIncluded"); } } private bool _isNavigationIncluded = false; public new bool IsNavigationIncluded { get { return _isNavigationIncluded; } set { _isNavigationIncluded = value; OnPropertyChanged("IsNavigationIncluded"); } } /// /// Gets the HeaderInfo. /// public string HeaderInfo { get { return "MainRegion"; } } #endregion } }