DataConfigViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using Aitex.Core.RT.Log;
  2. using Caliburn.Micro;
  3. using MECF.Framework.Common.CommonData;
  4. using MECF.Framework.Common.DataCenter;
  5. using MECF.Framework.UI.Client.CenterViews.Editors;
  6. using MECF.Framework.UI.Client.ClientBase;
  7. using OpenSEMI.ClientBase;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Xml;
  12. using SciChart.Core.Extensions;
  13. namespace MECF.Framework.UI.Client.CenterViews.Configs.DataConfig
  14. {
  15. public class DataGroupItem : NotifiableItem
  16. {
  17. private string _name;
  18. public string Name
  19. {
  20. get { return _name; }
  21. set { _name = value; InvokePropertyChanged(nameof(Name)); }
  22. }
  23. private bool _isFix;
  24. public bool IsFix
  25. {
  26. get { return _isFix; }
  27. set { _isFix = value; InvokePropertyChanged(nameof(IsFix)); }
  28. }
  29. private bool _isVisible;
  30. public bool IsVisible
  31. {
  32. get { return _isVisible; }
  33. set { _isVisible = value; InvokePropertyChanged(nameof(IsVisible)); }
  34. }
  35. }
  36. public class DataConfigViewModel : ModuleUiViewModelBase, ISupportMultipleSystem
  37. {
  38. public class DataItem : NotifiableItem
  39. {
  40. private string _name;
  41. public string Name
  42. {
  43. get { return _name; }
  44. set { _name = value; InvokePropertyChanged(nameof(Name)); }
  45. }
  46. private bool _isSelected;
  47. public bool IsSelected
  48. {
  49. get { return _isSelected; }
  50. set { _isSelected = value; InvokePropertyChanged(nameof(IsSelected)); }
  51. }
  52. private bool _isChecked;
  53. public bool IsChecked
  54. {
  55. get { return _isChecked; }
  56. set { _isChecked = value; InvokePropertyChanged(nameof(IsChecked)); }
  57. }
  58. }
  59. #region Properties
  60. public ObservableCollection<DataGroupItem> GroupData { get; set; }
  61. private DataGroupItem _currentSelection;
  62. public DataGroupItem CurrentSelection
  63. {
  64. get { return _currentSelection; }
  65. set
  66. {
  67. _currentSelection = value;
  68. ChangeGroupSelection(_currentSelection);
  69. NotifyOfPropertyChange(nameof(CurrentSelection));
  70. }
  71. }
  72. private string _content;
  73. private XmlDocument _xmlContent;
  74. public ObservableCollection<DataItem> Unselections { get; set; }
  75. public ObservableCollection<DataItem> Selections { get; set; }
  76. public string NewGroupName { get; set; }
  77. #endregion
  78. #region Functions
  79. public DataConfigViewModel()
  80. {
  81. this.DisplayName = "Data Config";
  82. GroupData = new ObservableCollection<DataGroupItem>();
  83. Unselections = new ObservableCollection<DataItem>();
  84. Selections = new ObservableCollection<DataItem>();
  85. }
  86. protected override void OnInitialize()
  87. {
  88. base.OnInitialize();
  89. var lstItems = (List<string>)QueryDataClient.Instance.Service.GetConfig("System.NumericDataList");
  90. lstItems.Sort();
  91. var selection = new ObservableCollection<DataItem>();
  92. var unselection = new ObservableCollection<DataItem>();
  93. foreach (var item in lstItems)
  94. {
  95. unselection.Add(new DataItem() { Name = item, IsChecked = false, IsSelected = false });
  96. selection.Add(new DataItem() { Name = item, IsChecked = false, IsSelected = false });
  97. }
  98. Unselections = unselection;
  99. Selections = selection;
  100. NotifyOfPropertyChange(nameof(Unselections));
  101. NotifyOfPropertyChange(nameof(Selections));
  102. }
  103. protected override void OnActivate()
  104. {
  105. base.OnActivate();
  106. UpdateData();
  107. }
  108. private void UpdateData()
  109. {
  110. string content = QueryDataClient.Instance.Service.GetTypedConfigContent("DataGroup", "");
  111. if (_content == content)
  112. return;
  113. GroupData.Clear();
  114. try
  115. {
  116. _xmlContent = new XmlDocument();
  117. _xmlContent.LoadXml(content);
  118. _content = content;
  119. var groups = _xmlContent.SelectNodes("DataGroupConfig/DataGroup");
  120. foreach (var item in groups)
  121. {
  122. XmlElement element = item as XmlElement;
  123. string strFix = element.GetAttribute("fix");
  124. bool bFix = false;
  125. if (!string.IsNullOrEmpty(strFix))
  126. {
  127. bool.TryParse(strFix, out bFix);
  128. }
  129. string strVisible = element.GetAttribute("visible");
  130. bool bVisible = true;
  131. if (!string.IsNullOrEmpty(strVisible))
  132. {
  133. bool.TryParse(strVisible, out bVisible) ;
  134. }
  135. if (!bVisible)
  136. continue;
  137. string name = element.GetAttribute("name");
  138. GroupData.Add(new DataGroupItem() { Name = name, IsFix = bFix});
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. LOG.Write(ex);
  144. }
  145. }
  146. public void NewGroup()
  147. {
  148. if (string.IsNullOrEmpty(NewGroupName))
  149. {
  150. DialogBox.ShowWarning($"Please input new data group name");
  151. return;
  152. }
  153. NewGroupName = NewGroupName.Trim();
  154. var nodeGroup = _xmlContent.SelectSingleNode($"DataGroupConfig/DataGroup[@name='{NewGroupName}']");
  155. if (nodeGroup != null)
  156. {
  157. DialogBox.ShowWarning($"{NewGroupName} Already exist");
  158. return;
  159. }
  160. var nodeRoot = _xmlContent.SelectSingleNode($"DataGroupConfig");
  161. var node = _xmlContent.CreateElement("DataGroup");
  162. node.SetAttribute("name", NewGroupName);
  163. nodeRoot.AppendChild(node);
  164. var item = new DataGroupItem()
  165. {
  166. IsFix = false,
  167. Name = NewGroupName
  168. };
  169. GroupData.Add(item);
  170. CurrentSelection = item;
  171. QueryDataClient.Instance.Service.SetTypedConfigContent("DataGroup", "", _xmlContent.InnerXml);
  172. }
  173. public void RenameGroup(DataGroupItem group)
  174. {
  175. InputFileNameDialogViewModel dialog = new InputFileNameDialogViewModel("Input New Config Name");
  176. dialog.FileName = group.Name;
  177. WindowManager wm = new WindowManager();
  178. bool? dialogReturn = wm.ShowDialog(dialog);
  179. if (!dialogReturn.HasValue || !dialogReturn.Value)
  180. return;
  181. string name = dialog.FileName.Trim();
  182. if (string.IsNullOrEmpty(name))
  183. {
  184. DialogBox.ShowWarning("Folder name should not be empty");
  185. return;
  186. }
  187. var nodeGroup = _xmlContent.SelectSingleNode($"DataGroupConfig/DataGroup[@name='{name}']");
  188. if (nodeGroup != null)
  189. {
  190. DialogBox.ShowWarning($"{name} Already exist");
  191. return;
  192. }
  193. nodeGroup = _xmlContent.SelectSingleNode($"DataGroupConfig/DataGroup[@name='{group.Name}']");
  194. (nodeGroup as XmlElement).SetAttribute("name", name);
  195. group.Name = name;
  196. QueryDataClient.Instance.Service.SetTypedConfigContent("DataGroup", "", _xmlContent.InnerXml);
  197. }
  198. public void DeleteGroup(DataGroupItem group)
  199. {
  200. if (!DialogBox.Confirm($"Are you sure you want to delete {group.Name}?"))
  201. return;
  202. var nodeGroup = _xmlContent.SelectSingleNode($"DataGroupConfig/DataGroup[@name='{group.Name}']");
  203. nodeGroup.ParentNode.RemoveChild(nodeGroup);
  204. QueryDataClient.Instance.Service.SetTypedConfigContent("DataGroup", "", _xmlContent.InnerXml);
  205. GroupData.RemoveWhere(x=>x.Name == group.Name);
  206. //CurrentSelection = null;
  207. }
  208. public void Select()
  209. {
  210. foreach (var unselection in Unselections)
  211. {
  212. if (unselection.IsChecked)
  213. {
  214. unselection.IsSelected = false;
  215. unselection.IsChecked = false;
  216. foreach (var selection in Selections)
  217. {
  218. if (selection.Name == unselection.Name)
  219. {
  220. selection.IsChecked = false;
  221. selection.IsSelected = true;
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. public void Unselect()
  229. {
  230. foreach (var selection in Selections)
  231. {
  232. if (selection.IsChecked)
  233. {
  234. selection.IsSelected = false;
  235. selection.IsChecked = false;
  236. foreach (var unselection in Unselections)
  237. {
  238. if (unselection.Name == selection.Name)
  239. {
  240. unselection.IsChecked = false;
  241. unselection.IsSelected = true;
  242. break;
  243. }
  244. }
  245. }
  246. }
  247. }
  248. public void SaveSelection()
  249. {
  250. var nodeGroup = _xmlContent.SelectSingleNode($"DataGroupConfig/DataGroup[@name='{CurrentSelection.Name}']");
  251. var nodeItem = _xmlContent.SelectNodes($"DataGroupConfig/DataGroup[@name='{CurrentSelection.Name}']/DataItem");
  252. foreach (var nodeGroupChildNode in nodeItem)
  253. {
  254. var node = nodeGroupChildNode as XmlElement;
  255. nodeGroup.RemoveChild(node);
  256. }
  257. foreach (var item in Selections)
  258. {
  259. if (item.IsSelected)
  260. {
  261. var node = _xmlContent.CreateElement("DataItem");
  262. node.SetAttribute("name", item.Name);
  263. nodeGroup.AppendChild(node);
  264. }
  265. }
  266. QueryDataClient.Instance.Service.SetTypedConfigContent("DataGroup", "", _xmlContent.InnerXml);
  267. }
  268. public void CancelSelection()
  269. {
  270. ChangeGroupSelection(CurrentSelection);
  271. }
  272. protected void ChangeGroupSelection(DataGroupItem group)
  273. {
  274. if (group == null)
  275. {
  276. foreach (var unselection in Unselections)
  277. {
  278. unselection.IsSelected = false;
  279. }
  280. foreach (var selection in Selections)
  281. {
  282. selection.IsSelected = false;
  283. }
  284. return;
  285. }
  286. var items = _xmlContent.SelectNodes($"DataGroupConfig/DataGroup[@name='{group.Name}']/DataItem");
  287. List<string> names = new List<string>();
  288. foreach (var item in items)
  289. {
  290. var node = item as XmlElement;
  291. names.Add(node.GetAttribute("name"));
  292. }
  293. foreach (var unselection in Unselections)
  294. {
  295. unselection.IsSelected = !names.Contains(unselection.Name);
  296. }
  297. foreach (var selection in Selections)
  298. {
  299. selection.IsSelected = names.Contains(selection.Name);
  300. }
  301. }
  302. #endregion
  303. }
  304. }