Files
2026-04-17 14:55:32 -04:00

312 lines
12 KiB
C#

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>, 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<Notification> NotificationRequest { get; private set; }
public new InteractionRequest<Confirmation> ConfirmationRequest { get; private set; }
/// <summary>
/// Creates a new instance of the MainViewModel.
/// </summary>
/// <param name="view">The MainView View.</param>
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
/// <param name="eventAggregator">The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other.</param>
/// <param name="unityContainer">The Unity container.</param>
public MainViewModel(IMainView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer)
: base(regionManager, eventAggregator, unityContainer)
{
View = view;
View.DataContext = this;
NotificationRequest = new InteractionRequest<Notification>();
ConfirmationRequest = new InteractionRequest<Confirmation>();
_eventAggregator = eventAggregator;
_unityContainer = unityContainer;
_regionManager = regionManager;
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
}
#region Methods
/// <summary>
/// Initialize without parameter
/// </summary>
public override void Initialize()
{
}
/// <summary>
/// Initialize with parameter - Parent
/// </summary>
public override void Initialize(object parameter)
{
Parent = (IBaseWindowModel)parameter;
Parent.IsMenuIncluded = _isMenuIncluded;
Parent.IsNavigationIncluded = _isNavigationIncluded;
_eventAggregator.GetEvent<AssemblyListNotification>().Subscribe(OnAssemblyListChange, ThreadOption.PublisherThread, true);
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>().Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true);
View.DataContext = this;
}
public override void Activated()
{
}
/// <summary>
/// Display loaded assemblies
/// </summary>
/// <param name="e">List of loaded assemblies</param>
private void OnAssemblyListChange(AssemblyListInfo e)
{
var assemblyList = new List<AssemblyNameImage>();
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;
}
}
}
/// <summary>
/// Private Event handler for RaiseNotification event.
/// </summary>
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
});
}
/// <summary>
/// Private Event handler for RaiseNotification event.
/// </summary>
private void OnBusyIndicatorNotification(bool eventArg)
{
IsBusy = eventArg;
}
#endregion
#region Views
private INavigationView GetNavigationView(IBaseViewModel parent)
{
var view = _unityContainer.Resolve<INavigationView>();
var viewModel = _unityContainer.Resolve<INavigationViewModel>();
view.DataContext = viewModel;
viewModel.Initialize(parent);
return view;
}
private IPropertyView GetPropertyView(IBaseWindowModel parent)
{
var view = _unityContainer.Resolve<IPropertyView>();
var viewModel = _unityContainer.Resolve<IPropertyViewModel>();
view.DataContext = viewModel;
viewModel.Initialize(parent);
return view;
}
private IGraphView GetGraphView(IBaseWindowModel parent)
{
var view = _unityContainer.Resolve<IGraphView>();
var viewModel = _unityContainer.Resolve<IGraphViewModel>();
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<FrameworkElement> GetRegions()
{
var items = new List<FrameworkElement>();
Utils.GetChildrenByName(((MainView)View).MainShell, "Region", ref items);
return items;
}
#endregion
#region Properties
//List<IGroupView> _assemblyGroupList = new List<IGroupView>();
//public List<IGroupView> AssemblyGroupList
//{
// get { return _assemblyGroupList; }
// set { _assemblyGroupList = value; OnPropertyChanged("AssemblyGroupList"); }
//}
///<summary>
///Occurs when a property value changes.
///</summary>
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");
}
}
/// <summary>
/// Gets the HeaderInfo.
/// </summary>
public string HeaderInfo { get { return "MainRegion"; } }
#endregion
}
}