RecipeViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 recipeType = dlg.SelectedType;
  239. var recipeName = folderName + "\\" + dlg.InputText;
  240. //string recipeContent = m_uiRecipeManager.GetRecipeTemplate(m_chamId);
  241. //m_uiRecipeManager.SaveAsRecipe(m_chamId, recipeName, m_uiRecipeManager.LoadRecipe("system",folderName));
  242. //if (SaveAsRecipe(recipeName, RecipeTemplate))
  243. //{
  244. // UpdateRecipeFileList();
  245. // SelectRecipe(recipeName);
  246. //}
  247. //else
  248. //{
  249. // WPFMessageBox.ShowError("Error");
  250. //}
  251. UpdateRecipeFileList();
  252. SelectRecipe(recipeName);
  253. }
  254. }
  255. catch (Exception ex)
  256. {
  257. //LOG.Write(ex);
  258. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  259. }
  260. }
  261. /// <summary>
  262. /// SaveAs recipe by recipe name and recipe data
  263. /// </summary>
  264. /// <param name="recipeName"></param>
  265. /// <param name="recipe"></param>
  266. /// <returns></returns>
  267. public bool SaveAsRecipe(string recipeName, string recipeContent)
  268. {
  269. return m_uiRecipeManager.SaveAsRecipe(m_chamId, recipeName, recipeContent);
  270. }
  271. /// <summary>
  272. /// Recipe template
  273. /// </summary>
  274. public string RecipeTemplate
  275. {
  276. get
  277. {
  278. string template = m_uiRecipeManager.GetRecipeTemplate(m_chamId);
  279. return template;
  280. }
  281. }
  282. private void UpdateRecipeFileList()
  283. {
  284. XmlDocument doc = new XmlDocument();
  285. doc.LoadXml(GetXmlRecipeList());
  286. treeViewRcpList.Items.Clear();
  287. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList);
  288. }
  289. /// <summary>
  290. /// Get recipe list in xml format
  291. /// </summary>
  292. /// <returns></returns>
  293. public string GetXmlRecipeList()
  294. {
  295. return m_uiRecipeManager.GetXmlRecipeList(m_chamId, true) ?? "";
  296. }
  297. void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl)
  298. {
  299. foreach (XmlElement ele in curElementNode.ChildNodes)
  300. {
  301. if (ele.Name == "File")
  302. {
  303. string fileName = ele.Attributes["Name"].Value;
  304. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  305. TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value);
  306. item.Tag = ele.Attributes["Name"].Value;
  307. item.ToolTip = fileName;
  308. itemsControl.Items.Add(item);
  309. }
  310. else if (ele.Name == "Folder")
  311. {
  312. string folderName = ele.Attributes["Name"].Value;
  313. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  314. TreeViewFolderItem item = new TreeViewFolderItem(folderName);
  315. item.Tag = ele.Attributes["Name"].Value;
  316. CreateTreeViewItems(ele, item);
  317. item.IsExpanded = false;
  318. itemsControl.Items.Add(item);
  319. }
  320. }
  321. }
  322. void SelectRecipe(string recipeName)
  323. {
  324. try
  325. {
  326. string[] paths = recipeName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
  327. string fileName = "";
  328. for (int i = 0; i < paths.Length - 1; i++)
  329. fileName += paths[i] + "\\";
  330. fileName += paths[paths.Length - 1];
  331. selectRecipe(treeViewRcpList, paths, 0, fileName);
  332. }
  333. catch (Exception ex)
  334. {
  335. //LOG.Write(ex);
  336. }
  337. }
  338. ItemsControl selectRecipe(ItemsControl currentNode, string[] paths, int index, string fileName)
  339. {
  340. if (currentNode == null)
  341. return null;
  342. if (index == paths.Length - 1)
  343. {
  344. foreach (var item in currentNode.Items)
  345. {
  346. TreeViewFileItem tvf = item as TreeViewFileItem;
  347. if (tvf != null && tvf.FileName == fileName)
  348. {
  349. tvf.IsSelected = true;
  350. return null;
  351. }
  352. }
  353. }
  354. foreach (var item in currentNode.Items)
  355. {
  356. TreeViewFolderItem tvf = item as TreeViewFolderItem;
  357. if (tvf != null && tvf.FolderName == paths[index])
  358. {
  359. tvf.IsExpanded = true;
  360. selectRecipe(tvf, paths, index + 1, fileName);
  361. break;
  362. }
  363. }
  364. return null;
  365. }
  366. #endregion
  367. }
  368. }