PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/VMClusterManager/View Models/VMModels/VMListViewModel.cs

#
C# | 370 lines | 307 code | 52 blank | 11 comment | 19 complexity | ba9ba09aa9839118ad0cae5ea20b400b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using VMClusterManager.Controls;
  4. using VMClusterManager.ViewModels.VMModels;
  5. using System.Threading;
  6. using System.Collections.ObjectModel;
  7. using Microsoft.Practices.Composite.Wpf.Commands;
  8. using System.Windows;
  9. using VMClusterManager.Controls.JobViews;
  10. using VMClusterManager.ViewModels.HpcModels;
  11. namespace VMClusterManager.ViewModels
  12. {
  13. class VMListViewModel : ViewModelBase
  14. {
  15. VMModel vmModel;
  16. private VMListView view;
  17. public VMListView View
  18. {
  19. get { return view; }
  20. set { view = value; }
  21. }
  22. private string mainNodeName;
  23. public string MainNodeName
  24. {
  25. get { return mainNodeName; }
  26. set { mainNodeName = value; ChangeMainNodeNameCommand.RaiseCanExecuteChanged(); OnPropertyChanged("MainNodeName"); }
  27. }
  28. private JobCommandsView jobActionsView;
  29. public JobCommandsView JobActionsView
  30. {
  31. get { return jobActionsView; }
  32. set { jobActionsView = value; OnJobActionsViewChanged(); }
  33. }
  34. public event EventHandler<EventArgs> JobActionsViewChanged;
  35. private void OnJobActionsViewChanged()
  36. {
  37. if (JobActionsViewChanged != null)
  38. JobActionsViewChanged(this, new EventArgs());
  39. }
  40. private DelegateCommand<ObservableCollection<VM>> refreshVMListCommand;
  41. private DelegateCommand<IEnumerable<VMViewModel>> selectAllCommand;
  42. private DelegateCommand<IEnumerable<VMViewModel>> unselectAllCommand;
  43. private DelegateCommand<string> changeMainNodeNameCommand;
  44. public DelegateCommand<string> ChangeMainNodeNameCommand
  45. {
  46. get { return changeMainNodeNameCommand; }
  47. set { changeMainNodeNameCommand = value; OnPropertyChanged("ChangeMainNodeNameCommand"); }
  48. }
  49. public DelegateCommand<IEnumerable<VMViewModel>> UnselectAllCommand
  50. {
  51. get { return unselectAllCommand; }
  52. set { unselectAllCommand = value; OnPropertyChanged("UnselectAllCommand"); }
  53. }
  54. public DelegateCommand<IEnumerable<VMViewModel>> SelectAllCommand
  55. {
  56. get { return selectAllCommand; }
  57. set { selectAllCommand = value; OnPropertyChanged("SelectAllCommand"); }
  58. }
  59. public DelegateCommand<ObservableCollection<VM>> RefreshVMListCommand
  60. {
  61. get { return refreshVMListCommand; }
  62. set { refreshVMListCommand = value; OnPropertyChanged("RefreshVMListCommand"); }
  63. }
  64. private List<Action> CanExecuteRaisers;
  65. public ObservableCollection<LogMessage> Log
  66. {
  67. get
  68. {
  69. return VMLog.GetInstance();
  70. }
  71. }
  72. private ObservableCollection<VM> vmList;
  73. public ObservableCollection<VM> VMList
  74. {
  75. get { return vmList; }
  76. set
  77. {
  78. vmList = value;
  79. OnPropertyChanged("VMList");
  80. }
  81. }
  82. private ObservableCollection<VM> activeVMList;
  83. public ObservableCollection<VM> ActiveVMList
  84. {
  85. get { return activeVMList; }
  86. private set { activeVMList = value; }
  87. }
  88. private object vmDetails;
  89. private object vmProperties;
  90. private object hpcJobsView;
  91. public object HpcJobsView
  92. {
  93. get { return hpcJobsView; }
  94. set { hpcJobsView = value; OnPropertyChanged("HpcJobsView"); }
  95. }
  96. public object VMProperties
  97. {
  98. get { return vmProperties; }
  99. set { vmProperties = value; OnPropertyChanged("VMProperties"); }
  100. }
  101. public object VMDetails
  102. {
  103. get { return vmDetails; }
  104. set { vmDetails = value; OnPropertyChanged("VMDetails"); }
  105. }
  106. private bool detailsExpanded;
  107. private bool propertiesExpanded;
  108. private bool jobsExpanded;
  109. private bool logExpanded;
  110. public bool LogExpanded
  111. {
  112. get { return logExpanded; }
  113. set { logExpanded = value; OnPropertyChanged("LogExpanded"); }
  114. }
  115. public bool JobsExpanded
  116. {
  117. get { return jobsExpanded; }
  118. set
  119. {
  120. jobsExpanded = value;
  121. if (value)
  122. {
  123. if (vmModel.ActiveVMList.Count > 0)
  124. {
  125. JobListViewModel jobs = new JobListViewModel(MainNodeName, vmModel.ActiveVMList);
  126. HpcJobsView = jobs.View;
  127. JobActionsView = jobs.ActionsView;
  128. }
  129. else
  130. {
  131. HpcJobsView = "Select one or more Virtual Machines to explore HPC jobs running on.";
  132. }
  133. }
  134. else
  135. {
  136. JobActionsView = null;
  137. }
  138. OnPropertyChanged("JobsExpanded");
  139. }
  140. }
  141. public bool PropertiesExpanded
  142. {
  143. get { return propertiesExpanded; }
  144. set
  145. {
  146. propertiesExpanded = value;
  147. if (value)
  148. {
  149. if (vmModel.ActiveVMList.Count > 0)
  150. {
  151. VMPropertiesViewModel vmProp = new VMPropertiesViewModel(vmModel.ActiveVMList);
  152. vmProp.ViewCloseRequested +=
  153. (o, e) =>
  154. {
  155. PropertiesExpanded = false;
  156. };
  157. VMProperties = vmProp.View;
  158. }
  159. else
  160. {
  161. VMProperties = "No Virtual Machine selected.";
  162. }
  163. }
  164. OnPropertyChanged("PropertiesExpanded");
  165. }
  166. }
  167. public bool DetailsExpanded
  168. {
  169. get { return detailsExpanded; }
  170. set
  171. {
  172. detailsExpanded = value;
  173. if (value)
  174. {
  175. if (vmModel.ActiveVMList.Count == 1)
  176. {
  177. VMDetailsViewModel vmDet = new VMDetailsViewModel(vmModel.ActiveVMList[0]);
  178. VMDetails = vmDet.View;
  179. }
  180. else
  181. {
  182. VMDetails = "Details are available only for single VM checked";
  183. }
  184. }
  185. OnPropertyChanged("DetailsExpanded");
  186. }
  187. }
  188. public VMListViewModel(VMModel vmModel, ObservableCollection<VM> _VMList)
  189. {
  190. this.vmModel = vmModel;
  191. View = new VMListView();
  192. this.VMList = _VMList;
  193. vmModel.ActiveVMList.Clear();
  194. //FillVMList();
  195. RefreshVMListCommand = new DelegateCommand<ObservableCollection<VM>>(RefreshVMList, CanRefreshVMList);
  196. SelectAllCommand = new DelegateCommand<IEnumerable<VMViewModel>>(SelectAll, CanSelectAll);
  197. UnselectAllCommand = new DelegateCommand<IEnumerable<VMViewModel>>(UnselectAll, CanUnselectAll);
  198. ChangeMainNodeNameCommand = new DelegateCommand<string>(ChangeMainNode, CanChangeMainNode);
  199. CanExecuteRaisers = new List<Action>{
  200. RefreshVMListCommand.RaiseCanExecuteChanged,
  201. SelectAllCommand.RaiseCanExecuteChanged,
  202. UnselectAllCommand.RaiseCanExecuteChanged
  203. };
  204. this.MainNodeName = vmModel.Settings.MainNodeName;
  205. this.VMList.CollectionChanged +=
  206. (obj, exp) =>
  207. {
  208. RefreshCommands();
  209. OnPropertyChanged("VMList");
  210. };
  211. vmModel.ActiveVMList.CollectionChanged +=
  212. (obj, exp) =>
  213. {
  214. DetailsExpanded = false;
  215. PropertiesExpanded = false;
  216. JobsExpanded = false;
  217. GC.Collect();
  218. UnselectAllCommand.RaiseCanExecuteChanged();
  219. };
  220. VMLog.GetInstance().CollectionChanged +=
  221. (o, e) =>
  222. {
  223. LogExpanded = true;
  224. };
  225. View.SetViewModel(this);
  226. }
  227. private Thread CommandRefreshThread = null;
  228. private void RefreshCommands()
  229. {
  230. if (CommandRefreshThread != null)
  231. {
  232. if (CommandRefreshThread.ThreadState == ThreadState.Running)
  233. {
  234. return;
  235. }
  236. }
  237. CommandRefreshThread = new Thread(new ThreadStart(delegate()
  238. {
  239. foreach (Action action in CanExecuteRaisers)
  240. {
  241. action();
  242. }
  243. }));
  244. CommandRefreshThread.IsBackground = true;
  245. CommandRefreshThread.Start();
  246. }
  247. private void RefreshVMList(ObservableCollection<VM> vmList)
  248. {
  249. vmModel.ActiveVMList.Clear();
  250. //FillVMList();
  251. }
  252. private bool CanRefreshVMList(ObservableCollection<VM> vmList)
  253. {
  254. return true;
  255. }
  256. private void SelectAll(IEnumerable<VMViewModel> vmToSelect)
  257. {
  258. foreach (VMViewModel vm in vmToSelect)
  259. {
  260. if (!vm.IsActiveVM)
  261. {
  262. vm.IsActiveVM = true;
  263. }
  264. }
  265. }
  266. private bool CanSelectAll(IEnumerable<VMViewModel> vmToSelect)
  267. {
  268. //bool canSelectAll = false;
  269. //if (vmToSelect != null)
  270. //{
  271. // canSelectAll = (vmToSelect. > 0);
  272. //}
  273. return true;
  274. }
  275. private void UnselectAll(IEnumerable<VMViewModel> vmToUnSelect)
  276. {
  277. foreach (VMViewModel vm in vmToUnSelect)
  278. {
  279. if (vm.IsActiveVM)
  280. {
  281. vm.IsActiveVM = false;
  282. }
  283. }
  284. }
  285. private bool CanUnselectAll(IEnumerable<VMViewModel> vmToUnSelect)
  286. {
  287. bool canUnselect =false;
  288. if (vmToUnSelect != null)
  289. {
  290. canUnselect = vmModel.ActiveVMList.Count > 0;
  291. }
  292. return canUnselect;
  293. }
  294. public void Dispose()
  295. {
  296. //foreach (VMViewModel vmView in VMList)
  297. //{
  298. // vmView.Dispose();
  299. //}
  300. }
  301. private void ChangeMainNode(string newName)
  302. {
  303. vmModel.Settings.MainNodeName = this.MainNodeName;
  304. vmModel.Settings.SaveToFile();
  305. ChangeMainNodeNameCommand.RaiseCanExecuteChanged();
  306. if (vmModel.ActiveVMList.Count > 0)
  307. {
  308. JobListViewModel jobs = new JobListViewModel(MainNodeName, vmModel.ActiveVMList);
  309. HpcJobsView = jobs.View;
  310. JobActionsView = jobs.ActionsView;
  311. }
  312. }
  313. private bool CanChangeMainNode(string newName)
  314. {
  315. return vmModel.Settings.MainNodeName != this.MainNodeName;
  316. }
  317. }
  318. }