RecipeViewModel.cs 13 KB

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