RecipeViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.UI.View.Common;
  3. using Aitex.UI.RecipeEditor;
  4. using Aitex.UI.RecipeEditor.View;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Media;
  15. using System.Xml;
  16. using Venus_MainPages.PMs;
  17. using Venus_MainPages.Views;
  18. using WPF.Themes.UserControls;
  19. namespace Venus_MainPages.ViewModels
  20. {
  21. internal class RecipeViewModel : BindableBase
  22. {
  23. #region 私有字段
  24. private string m_Title;
  25. private string m_chamId = "PMA";
  26. private string m_CurrentRecipeName;
  27. private UiRecipeManager m_uiRecipeManager = new UiRecipeManager();
  28. private RecipeView recipeView;
  29. private TreeView treeViewRcpList;
  30. private RecipeEditorControl tableRecipeGrid;
  31. #endregion
  32. #region 属性
  33. public string Title
  34. {
  35. get { return m_Title; }
  36. set { SetProperty(ref m_Title, value); }
  37. }
  38. public string CurrentRecipeName
  39. {
  40. get { return m_CurrentRecipeName; }
  41. set { SetProperty(ref m_CurrentRecipeName, value); }
  42. }
  43. #endregion
  44. #region 命令
  45. private DelegateCommand _NewCommand;
  46. public DelegateCommand NewCommand =>
  47. _NewCommand ?? (_NewCommand = new DelegateCommand(OnNew));
  48. private DelegateCommand _ReNameCommand;
  49. public DelegateCommand ReNameCommand =>
  50. _ReNameCommand ?? (_ReNameCommand = new DelegateCommand(OnReName));
  51. private DelegateCommand _DeleteCommand;
  52. public DelegateCommand DeleteCommand =>
  53. _DeleteCommand ?? (_DeleteCommand = new DelegateCommand(OnDelete));
  54. private DelegateCommand<Object> _MouseRightButtonDownCommand;
  55. public DelegateCommand<Object> MouseRightButtonDownCommand =>
  56. _MouseRightButtonDownCommand ?? (_MouseRightButtonDownCommand = new DelegateCommand<Object>(OnMouseRightButtonDown));
  57. private DelegateCommand<Object> _LoadedCommand;
  58. public DelegateCommand<Object> LoadedCommand =>
  59. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand<Object>(OnLoaded));
  60. #endregion
  61. #region 构造函数
  62. public RecipeViewModel()
  63. {
  64. Title = "配方管理";
  65. }
  66. #endregion
  67. #region 方法
  68. private void OnNew()
  69. {
  70. }
  71. private void OnReName()
  72. {
  73. }
  74. private void OnDelete()
  75. {
  76. }
  77. private void OnLoaded(Object myrecipeView)
  78. {
  79. recipeView = myrecipeView as RecipeView;
  80. treeViewRcpList = recipeView.treeViewRcpList;
  81. tableRecipeGrid = recipeView.tableRecipeGrid;
  82. UpdateRecipeFileList();
  83. treeViewRcpList.SelectedItemChanged += TreeViewRcpList_SelectedItemChanged;
  84. }
  85. private void TreeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  86. {
  87. if (tableRecipeGrid.IsRecipeModified)
  88. {
  89. //if (permission == ViewPermission.FullyControl)
  90. {
  91. switch (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgSaveCurrentRecipe"].ToString(), CurrentRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question))
  92. {
  93. case MessageBoxResult.Yes:
  94. if (SaveRecipeV())
  95. {
  96. var item = e.NewValue as TreeViewFileItem;
  97. if (item != null)
  98. item.IsSelected = true;
  99. }
  100. else
  101. {
  102. var item = e.OldValue as TreeViewFileItem;
  103. if (item != null)
  104. item.IsSelected = true;
  105. return;
  106. }
  107. break;
  108. case MessageBoxResult.No:
  109. {
  110. var item = e.NewValue as TreeViewFileItem;
  111. if (item != null)
  112. item.IsSelected = true;
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. var selectedItem = e.NewValue as TreeViewFileItem;
  119. if (selectedItem == null)
  120. return;
  121. try
  122. {
  123. CurrentRecipeName = selectedItem.FileName;
  124. string xmlRecipeData = m_uiRecipeManager.LoadRecipe(m_chamId,selectedItem.FileName);
  125. this.tableRecipeGrid.ControlViewModel.LoadRecipe(RecipeFormat, xmlRecipeData);
  126. }
  127. catch (Exception ex)
  128. {
  129. //LOG.Write(ex);
  130. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  131. }
  132. }
  133. /// <summary>
  134. /// Recipe format
  135. /// </summary>
  136. public string RecipeFormat
  137. {
  138. get
  139. {
  140. return m_uiRecipeManager.GetRecipeFormatXml(m_chamId);
  141. }
  142. }
  143. private bool SaveRecipeV(string flag = "Single")
  144. {
  145. bool ret = true;
  146. try
  147. {
  148. var hasErr = this.tableRecipeGrid.ControlViewModel.Errors.Count > 0;
  149. if (!hasErr ||
  150. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveInfo"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
  151. {
  152. ret = SaveRecipe(CurrentRecipeName, this.tableRecipeGrid.ControlViewModel.GetRecipeContentString());
  153. tableRecipeGrid.Dispatcher.Invoke(new System.Action(() =>
  154. {
  155. tableRecipeGrid.ControlViewModel.LoadRecipe(RecipeFormat, LoadRecipe(CurrentRecipeName));
  156. }));
  157. }
  158. else
  159. {
  160. ret = false;
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. //LOG.Write(ex);
  166. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveException"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  167. ret = false;
  168. }
  169. return ret;
  170. }
  171. /// <summary>
  172. /// Load recipe data by recipe name
  173. /// </summary>
  174. /// <param name="receipeName"></param>
  175. /// <returns></returns>
  176. public string LoadRecipe(string receipeName)
  177. {
  178. return m_uiRecipeManager.LoadRecipe(m_chamId, receipeName);
  179. }
  180. /// <summary>
  181. /// Save recipe by recipe name and recipe data
  182. /// </summary>
  183. /// <param name="recipeName"></param>
  184. /// <param name="recipe"></param>
  185. /// <returns></returns>
  186. public bool SaveRecipe(string recipeName, string recipeContent)
  187. {
  188. return m_uiRecipeManager.SaveRecipe(m_chamId, recipeName, recipeContent);
  189. }
  190. /// <summary>
  191. /// 右击TreeView,上下文菜单选择功能
  192. /// </summary>
  193. /// <param name="treeView"></param>
  194. private void OnMouseRightButtonDown(Object treeView)
  195. {
  196. //treeViewRcpList = treeView as TreeView;
  197. treeViewRcpList.ContextMenu = new ContextMenu() { Background = new SolidColorBrush(Colors.AliceBlue)};
  198. MenuItem menuItem = new MenuItem();
  199. menuItem = new MenuItem();
  200. menuItem.Tag = "\\";
  201. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  202. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"];
  203. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  204. menuItem = new MenuItem();
  205. menuItem.Tag = "\\";
  206. //menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe);
  207. menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"];
  208. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  209. treeViewRcpList.ContextMenu.Items.Add(new Separator());
  210. menuItem = new MenuItem();
  211. menuItem.Tag = "\\";
  212. //menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder);
  213. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"];
  214. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  215. treeViewRcpList.ContextMenu.Visibility = Visibility.Visible;
  216. }
  217. /// <summary>
  218. /// 创建新的配方
  219. /// </summary>
  220. /// <param name="sender"></param>
  221. /// <param name="e"></param>
  222. private void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e)
  223. {
  224. MenuItem mit = sender as MenuItem;
  225. string folderName = mit.Tag as string;
  226. PerformCreateRecipe(folderName);
  227. }
  228. private void PerformCreateRecipe(string folderName)
  229. {
  230. try
  231. {
  232. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  233. {
  234. Owner = Application.Current.MainWindow
  235. };
  236. if (dlg.ShowDialog() == true)
  237. {
  238. var recipeName = folderName + "\\" + dlg.InputText;
  239. string recipeContent = m_uiRecipeManager.GetRecipeTemplate(m_chamId);
  240. //m_uiRecipeManager.SaveAsRecipe(m_chamId, recipeName, m_uiRecipeManager.LoadRecipe("system",folderName));
  241. if (SaveAsRecipe(recipeName, RecipeTemplate))
  242. {
  243. UpdateRecipeFileList();
  244. SelectRecipe(recipeName);
  245. }
  246. else
  247. {
  248. WPFMessageBox.ShowError("Error");
  249. }
  250. }
  251. }
  252. catch (Exception ex)
  253. {
  254. //LOG.Write(ex);
  255. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  256. }
  257. }
  258. /// <summary>
  259. /// SaveAs recipe by recipe name and recipe data
  260. /// </summary>
  261. /// <param name="recipeName"></param>
  262. /// <param name="recipe"></param>
  263. /// <returns></returns>
  264. public bool SaveAsRecipe(string recipeName, string recipeContent)
  265. {
  266. return m_uiRecipeManager.SaveAsRecipe(m_chamId, recipeName, recipeContent);
  267. }
  268. /// <summary>
  269. /// Recipe template
  270. /// </summary>
  271. public string RecipeTemplate
  272. {
  273. get
  274. {
  275. string template = m_uiRecipeManager.GetRecipeTemplate(m_chamId);
  276. return template;
  277. }
  278. }
  279. private void UpdateRecipeFileList()
  280. {
  281. XmlDocument doc = new XmlDocument();
  282. doc.LoadXml(GetXmlRecipeList());
  283. treeViewRcpList.Items.Clear();
  284. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList);
  285. }
  286. /// <summary>
  287. /// Get recipe list in xml format
  288. /// </summary>
  289. /// <returns></returns>
  290. public string GetXmlRecipeList()
  291. {
  292. return m_uiRecipeManager.GetXmlRecipeList(m_chamId, true) ?? "";
  293. }
  294. void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl)
  295. {
  296. foreach (XmlElement ele in curElementNode.ChildNodes)
  297. {
  298. if (ele.Name == "File")
  299. {
  300. string fileName = ele.Attributes["Name"].Value;
  301. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  302. TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value);
  303. item.Tag = ele.Attributes["Name"].Value;
  304. item.ToolTip = fileName;
  305. itemsControl.Items.Add(item);
  306. }
  307. else if (ele.Name == "Folder")
  308. {
  309. string folderName = ele.Attributes["Name"].Value;
  310. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  311. TreeViewFolderItem item = new TreeViewFolderItem(folderName);
  312. item.Tag = ele.Attributes["Name"].Value;
  313. CreateTreeViewItems(ele, item);
  314. item.IsExpanded = false;
  315. itemsControl.Items.Add(item);
  316. }
  317. }
  318. }
  319. void SelectRecipe(string recipeName)
  320. {
  321. try
  322. {
  323. string[] paths = recipeName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
  324. string fileName = "";
  325. for (int i = 0; i < paths.Length - 1; i++)
  326. fileName += paths[i] + "\\";
  327. fileName += paths[paths.Length - 1];
  328. selectRecipe(treeViewRcpList, paths, 0, fileName);
  329. }
  330. catch (Exception ex)
  331. {
  332. //LOG.Write(ex);
  333. }
  334. }
  335. ItemsControl selectRecipe(ItemsControl currentNode, string[] paths, int index, string fileName)
  336. {
  337. if (currentNode == null)
  338. return null;
  339. if (index == paths.Length - 1)
  340. {
  341. foreach (var item in currentNode.Items)
  342. {
  343. TreeViewFileItem tvf = item as TreeViewFileItem;
  344. if (tvf != null && tvf.FileName == fileName)
  345. {
  346. tvf.IsSelected = true;
  347. return null;
  348. }
  349. }
  350. }
  351. foreach (var item in currentNode.Items)
  352. {
  353. TreeViewFolderItem tvf = item as TreeViewFolderItem;
  354. if (tvf != null && tvf.FolderName == paths[index])
  355. {
  356. tvf.IsExpanded = true;
  357. selectRecipe(tvf, paths, index + 1, fileName);
  358. break;
  359. }
  360. }
  361. return null;
  362. }
  363. #endregion
  364. }
  365. }