RecipeEditorView.xaml.cs 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Xml;
  12. using Aitex.Core.Account;
  13. using Aitex.Core.RT.Log;
  14. using Aitex.Core.UI.View.Common;
  15. using Aitex.UI.RecipeEditor.View;
  16. using ExcelLibrary.SpreadSheet;
  17. using MECF.Framework.Common.DataCenter;
  18. using MECF.Framework.Common.Equipment;
  19. using MECF.Framework.UI.Core.Accounts;
  20. namespace VirgoUI.Client.Models.PMs
  21. {
  22. /// <summary>
  23. /// Interaction logic for RecipeEditorView.xaml
  24. /// </summary>
  25. public partial class RecipeEditorView : UserControl
  26. {
  27. private string _culture;
  28. private string tragetFilePath = "";
  29. private object OriginalItem;
  30. private RecipeEditorViewModel _viewModel;
  31. private int MenuPermission;
  32. private ViewPermission _permission;
  33. private Point _lastMouseDown;
  34. public RecipeEditorView()
  35. {
  36. InitializeComponent();
  37. this.buttonSaveTableRecipe.ContextMenu = null;
  38. this.DataContext = _viewModel = new RecipeEditorViewModel(new UiRecipeManager(), ModuleName.System.ToString());
  39. this.Loaded += new RoutedEventHandler(RecipeEditorView_Loaded);
  40. this.Unloaded += new RoutedEventHandler(RecipeEditorView_Unloaded);
  41. //VirgoUiSystem.Instance.CultureChanged += CultureChanged;
  42. }
  43. private void CultureChanged(string s)
  44. {
  45. _culture = s;
  46. this.tableRecipeGrid.UpdateCultureResource(s);
  47. }
  48. static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
  49. {
  50. while (source != null && source.GetType() != typeof(T))
  51. source = VisualTreeHelper.GetParent(source);
  52. return source;
  53. }
  54. void RecipeEditorView_Unloaded(object sender, RoutedEventArgs e)
  55. {
  56. }
  57. void RecipeEditorView_Loaded(object sender, RoutedEventArgs e)
  58. {
  59. buttonSaveTableRecipe.Click += (obj, args) => { SetMenu.IsOpen = true; };
  60. SaveToSingle.Header = $"Save To {((RecipeEditorViewModel)this.DataContext).SystemName}";
  61. SaveToDouble.Header = "Save To PMA And PMB";
  62. _viewModel.ChamberId = ((RecipeEditorViewModel)this.DataContext).SystemName;
  63. MenuPermission = ClientApp.Instance.GetPermission($"Recipe{_viewModel.ChamberId}");
  64. if (_culture != Thread.CurrentThread.CurrentCulture.Name)
  65. {
  66. _culture = Thread.CurrentThread.CurrentCulture.Name;
  67. this.tableRecipeGrid.UpdateCultureResource(_culture);
  68. }
  69. _viewModel.UpdateAllConfig();
  70. tableRecipeGrid.SetEnableBarcode(_viewModel.IsBarcodeEnabled);
  71. tableRecipeGrid.SetUser(ClientApp.Instance.UserContext.LoginName);
  72. tableRecipeGrid.SetEndPointDefaultValue((string)QueryDataClient.Instance.Service.GetConfig("System.EndPoint.EndPointDefaultValue"));
  73. UpdateRecipeFileList();
  74. UpdateRecipeFileListClean();
  75. this.Name = "recipeEditor";
  76. _permission = ViewPermission.FullyControl;//this.GetPermission();
  77. switch (_permission)
  78. {
  79. case ViewPermission.Readonly:
  80. buttonReloadTableRecipe.IsEnabled = false;
  81. tableRecipeGrid.IsEnabled = false;
  82. buttonSaveTableRecipe.IsEnabled = false;
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. private void treeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  89. {
  90. //var permission = this.GetPermission();
  91. if (tableRecipeGrid.IsRecipeModified)
  92. {
  93. //if (permission == ViewPermission.FullyControl)
  94. {
  95. switch (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgSaveCurrentRecipe"].ToString(), _viewModel.CurrentRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question))
  96. {
  97. case MessageBoxResult.Yes:
  98. if (SaveRecipe())
  99. {
  100. var item = e.NewValue as TreeViewFileItem;
  101. if (item != null)
  102. item.IsSelected = true;
  103. }
  104. else
  105. {
  106. var item = e.OldValue as TreeViewFileItem;
  107. if (item != null)
  108. item.IsSelected = true;
  109. return;
  110. }
  111. break;
  112. case MessageBoxResult.No:
  113. {
  114. var item = e.NewValue as TreeViewFileItem;
  115. if (item != null)
  116. item.IsSelected = true;
  117. }
  118. break;
  119. }
  120. }
  121. }
  122. var selectedItem = e.NewValue as TreeViewFileItem;
  123. if (selectedItem == null)
  124. return;
  125. try
  126. {
  127. this._viewModel.CurrentRecipeName = selectedItem.FileName;
  128. string rcpType = "Clean";
  129. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  130. {
  131. rcpType = "Process";
  132. }
  133. string xmlRecipeData = _viewModel.LoadRecipe(selectedItem.FileName,rcpType);
  134. this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, xmlRecipeData);
  135. }
  136. catch (Exception ex)
  137. {
  138. LOG.Write(ex);
  139. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  140. }
  141. }
  142. private bool SaveRecipe(string flag = "Single")
  143. {
  144. bool ret = true;
  145. try
  146. {
  147. var hasErr = this.tableRecipeGrid.ControlViewModel.Errors.Count > 0;
  148. if (!hasErr ||
  149. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveInfo"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
  150. {
  151. string rcpType = "Clean";
  152. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  153. {
  154. rcpType = "Process";
  155. }
  156. if (flag == "Single")
  157. ret = _viewModel.SaveRecipe(this._viewModel.CurrentRecipeName, rcpType, this.tableRecipeGrid.ControlViewModel.GetRecipeContentString());
  158. else if (flag == "Double")
  159. ret = _viewModel.SaveRecipeDouble(this._viewModel.CurrentRecipeName,rcpType, this.tableRecipeGrid.ControlViewModel.GetRecipeContentString());
  160. if (ret)
  161. {
  162. tableRecipeGrid.Dispatcher.Invoke(new System.Action(() =>
  163. {
  164. this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, _viewModel.LoadRecipe(_viewModel.CurrentRecipeName,rcpType));
  165. }));
  166. }
  167. }
  168. else
  169. {
  170. ret = false;
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. LOG.Write(ex);
  176. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveException"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  177. ret = false;
  178. }
  179. return ret;
  180. }
  181. private void treeViewRcpList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  182. {
  183. if (_permission != ViewPermission.FullyControl)
  184. return;
  185. //black place right mouse click
  186. var rcp = treeViewRcpListClean;
  187. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  188. {
  189. rcp = treeViewRcpList;
  190. }
  191. rcp.ContextMenu = new ContextMenu();
  192. MenuItem menuItem = new MenuItem();
  193. menuItem = new MenuItem();
  194. menuItem.Tag = "\\";
  195. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  196. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"];
  197. rcp.ContextMenu.Items.Add(menuItem);
  198. menuItem = new MenuItem();
  199. menuItem.Tag = "\\";
  200. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe);
  201. menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"];
  202. rcp.ContextMenu.Items.Add(menuItem);
  203. rcp.ContextMenu.Items.Add(new Separator());
  204. menuItem = new MenuItem();
  205. menuItem.Tag = "\\";
  206. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder);
  207. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"];
  208. rcp.ContextMenu.Items.Add(menuItem);
  209. rcp.ContextMenu.Visibility = Visibility.Visible;
  210. }
  211. private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  212. {
  213. if (_permission != ViewPermission.FullyControl)
  214. return;
  215. var treeViewFileItem = VisualUpwardSearch<TreeViewFileItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
  216. if (treeViewFileItem != null)
  217. {
  218. treeViewFileItem.Focus();
  219. treeViewFileItem.IsSelected = true;
  220. treeViewFileItem.ContextMenu = new ContextMenu();
  221. MenuItem menuItem = null;
  222. menuItem = new MenuItem();
  223. menuItem.Tag = treeViewFileItem.Tag as string;
  224. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ExportRecipe);
  225. menuItem.Header = Application.Current.Resources["GlobalLableMenuExportRecipe"];
  226. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  227. menuItem = new MenuItem();
  228. menuItem.Tag = treeViewFileItem.Tag as string;
  229. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ExportExcel);
  230. menuItem.Header = Application.Current.Resources["GlobalLableMenuExportRecipeAsExcel"];
  231. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  232. menuItem = new MenuItem();
  233. menuItem.Tag = treeViewFileItem.Tag as string;
  234. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_SaveAs);
  235. menuItem.Header = Application.Current.Resources["GlobalLableMenuSaveAs"];
  236. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  237. menuItem = new MenuItem();
  238. menuItem.Tag = treeViewFileItem.Tag as string;
  239. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameRecipe);
  240. menuItem.Header = Application.Current.Resources["GlobalLableMenuRenameRecipe"];
  241. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  242. menuItem = new MenuItem();
  243. menuItem.Tag = treeViewFileItem.Tag as string;
  244. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteRecipe);
  245. menuItem.Header = Application.Current.Resources["GlobalLableMenuDeleteRecipe"];
  246. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  247. treeViewFileItem.ContextMenu.Visibility = Visibility.Visible;
  248. }
  249. else
  250. {
  251. var treeViewFolderItem = VisualUpwardSearch<TreeViewFolderItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
  252. if (treeViewFolderItem != null)
  253. {
  254. treeViewFolderItem.Focus();
  255. treeViewFolderItem.IsSelected = true;
  256. treeViewFolderItem.ContextMenu = new ContextMenu();
  257. MenuItem menuItem = null;
  258. menuItem = new MenuItem();
  259. menuItem.Tag = treeViewFolderItem.Tag as string;
  260. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  261. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"];
  262. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  263. menuItem = new MenuItem();
  264. menuItem.Tag = treeViewFolderItem.Tag as string;
  265. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe);
  266. menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"];
  267. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  268. treeViewFolderItem.ContextMenu.Items.Add(new Separator());
  269. menuItem = new MenuItem();
  270. menuItem.Tag = treeViewFolderItem.Tag as string;
  271. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder);
  272. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"];
  273. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  274. menuItem = new MenuItem();
  275. menuItem.Tag = treeViewFolderItem.Tag as string;
  276. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameFolder);
  277. menuItem.Header = Application.Current.Resources["GlobalLableMenuRenameFolder"];
  278. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  279. menuItem = new MenuItem();
  280. menuItem.Tag = treeViewFolderItem.Tag as string;
  281. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteFolder);
  282. menuItem.Header = Application.Current.Resources["GlobalLableMenuDeleteFolder"];
  283. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  284. treeViewFolderItem.ContextMenu.Visibility = Visibility.Visible;
  285. }
  286. }
  287. }
  288. /// <summary>
  289. /// reload current recipe content
  290. /// </summary>
  291. /// <param name="sender"></param>
  292. /// <param name="e"></param>
  293. private void buttonCodeReload_Click(object sender, RoutedEventArgs e)
  294. {
  295. if (MenuPermission != 3) return;
  296. if (MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeReloadInfo"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
  297. {
  298. this.tableRecipeGrid.Dispatcher.Invoke(new System.Action(() =>
  299. {
  300. string rcpType = "Clean";
  301. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  302. {
  303. rcpType = "Process";
  304. }
  305. this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, _viewModel.LoadRecipe(_viewModel.CurrentRecipeName,rcpType));
  306. }));
  307. }
  308. }
  309. ///// <summary>
  310. ///// Save recipe file
  311. ///// </summary>
  312. ///// <param name="sender"></param>
  313. ///// <param name="e"></param>
  314. //private void buttonCodeSave_Click(object sender, RoutedEventArgs e)
  315. //{
  316. // SaveRecipe();
  317. //}
  318. /// <summary>
  319. /// Save recipe file
  320. /// </summary>
  321. /// <param name="sender"></param>
  322. /// <param name="e"></param>
  323. private void SaveToSingle_Click(object sender, RoutedEventArgs e)
  324. {
  325. if (MenuPermission != 3) return;
  326. SaveRecipe();
  327. }
  328. /// <summary>
  329. /// Save recipe file
  330. /// </summary>
  331. /// <param name="sender"></param>
  332. /// <param name="e"></param>
  333. private void SaveToDouble_Click(object sender, RoutedEventArgs e)
  334. {
  335. if (MenuPermission != 3) return;
  336. SaveRecipe("Double");
  337. }
  338. void SelectRecipe(string recipeName)
  339. {
  340. try
  341. {
  342. string[] paths = recipeName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
  343. string fileName = "";
  344. for (int i = 0; i < paths.Length - 1; i++)
  345. fileName += paths[i] + "\\";
  346. fileName += paths[paths.Length - 1];
  347. var rcp = treeViewRcpListClean;
  348. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  349. {
  350. rcp = treeViewRcpList;
  351. }
  352. selectRecipe(rcp, paths, 0, fileName);
  353. }
  354. catch (Exception ex)
  355. {
  356. LOG.Write(ex);
  357. }
  358. }
  359. ItemsControl selectRecipe(ItemsControl currentNode, string[] paths, int index, string fileName)
  360. {
  361. if (currentNode == null)
  362. return null;
  363. if (index == paths.Length - 1)
  364. {
  365. foreach (var item in currentNode.Items)
  366. {
  367. TreeViewFileItem tvf = item as TreeViewFileItem;
  368. if (tvf != null && tvf.FileName == fileName)
  369. {
  370. tvf.IsSelected = true;
  371. return null;
  372. }
  373. }
  374. }
  375. foreach (var item in currentNode.Items)
  376. {
  377. TreeViewFolderItem tvf = item as TreeViewFolderItem;
  378. if (tvf != null && tvf.FolderName == paths[index])
  379. {
  380. tvf.IsExpanded = true;
  381. selectRecipe(tvf, paths, index + 1, fileName);
  382. break;
  383. }
  384. }
  385. return null;
  386. }
  387. private void UpdateRecipeFileList()
  388. {
  389. XmlDocument doc = new XmlDocument();
  390. doc.LoadXml(_viewModel.GetXmlRecipeList("Process"));
  391. treeViewRcpList.Items.Clear();
  392. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList);
  393. }
  394. private void UpdateRecipeFileListClean()
  395. {
  396. XmlDocument doc = new XmlDocument();
  397. doc.LoadXml(_viewModel.GetXmlRecipeList("Clean"));
  398. treeViewRcpListClean.Items.Clear();
  399. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpListClean);
  400. }
  401. void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl)
  402. {
  403. foreach (XmlElement ele in curElementNode.ChildNodes)
  404. {
  405. if (ele.Name == "File")
  406. {
  407. string fileName = ele.Attributes["Name"].Value;
  408. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  409. TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value);
  410. item.Tag = ele.Attributes["Name"].Value;
  411. item.ToolTip = fileName;
  412. itemsControl.Items.Add(item);
  413. }
  414. else if (ele.Name == "Folder")
  415. {
  416. string folderName = ele.Attributes["Name"].Value;
  417. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  418. TreeViewFolderItem item = new TreeViewFolderItem(folderName);
  419. item.Tag = ele.Attributes["Name"].Value;
  420. CreateTreeViewItems(ele, item);
  421. item.IsExpanded = false;
  422. itemsControl.Items.Add(item);
  423. }
  424. }
  425. }
  426. void menuItem_MouseClick_SaveAs(object sender, RoutedEventArgs e)
  427. {
  428. try
  429. {
  430. MenuItem mit = sender as MenuItem;
  431. var oldName = mit.Tag as string;
  432. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  433. {
  434. InputText = oldName,
  435. Owner = Application.Current.MainWindow
  436. };
  437. var ret = dlg.ShowDialog();
  438. if (ret.HasValue && ret.Value)
  439. {
  440. var newName = dlg.InputText;
  441. string rcpType = "Clean";
  442. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  443. {
  444. rcpType = "Process";
  445. }
  446. if (_viewModel.SaveAsRecipe(newName,rcpType, _viewModel.LoadRecipe(oldName,rcpType)))
  447. {
  448. UpdateRecipeFileList();
  449. UpdateRecipeFileListClean();
  450. SelectRecipe(newName);
  451. }
  452. else
  453. {
  454. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeSaveAsFailed"].ToString(), newName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  455. }
  456. }
  457. }
  458. catch (Exception ex)
  459. {
  460. LOG.Write(ex);
  461. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeSaveAsException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  462. }
  463. }
  464. void menuItem_MouseClick_CreateFolder(object sender, RoutedEventArgs e)
  465. {
  466. try
  467. {
  468. MenuItem mit = sender as MenuItem;
  469. string folderName = mit.Tag as string;
  470. var rcp = treeViewRcpListClean;
  471. string rcpType = "Clean";
  472. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  473. {
  474. rcp = treeViewRcpList;
  475. rcpType = "Process";
  476. }
  477. var selectedRecipeItem = rcp.SelectedItem as TreeViewFileItem;
  478. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputFolderName"].ToString())
  479. {
  480. Owner = Application.Current.MainWindow
  481. };
  482. if (dlg.ShowDialog() == true)
  483. {
  484. this._viewModel.CreateFolder(folderName + "\\" + dlg.InputText,rcpType);
  485. UpdateRecipeFileListClean();
  486. UpdateRecipeFileList();
  487. if (selectedRecipeItem != null)
  488. SelectRecipe(selectedRecipeItem.FileName);
  489. }
  490. }
  491. catch (Exception ex)
  492. {
  493. LOG.Write(ex);
  494. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeFolderNewFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString());
  495. }
  496. }
  497. /// <summary>
  498. /// Create new recipe
  499. /// </summary>
  500. /// <param name="sender"></param>
  501. /// <param name="e"></param>
  502. void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e)
  503. {
  504. MenuItem mit = sender as MenuItem;
  505. string folderName = mit.Tag as string;
  506. PerformCreateRecipe(folderName);
  507. }
  508. /// <summary>
  509. /// delete folder
  510. /// </summary>
  511. /// <param name="sender"></param>
  512. /// <param name="e"></param>
  513. void menuItem_MouseClick_DeleteFolder(object sender, RoutedEventArgs e)
  514. {
  515. MenuItem mit = sender as MenuItem;
  516. string folderName = mit.Tag as string;
  517. PerformDeleteFolder(folderName);
  518. }
  519. private void PerformDeleteFolder(string folderName)
  520. {
  521. try
  522. {
  523. var rcp = treeViewRcpListClean;
  524. string rcpType = "Clean";
  525. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  526. {
  527. rcp = treeViewRcpList;
  528. rcpType = "Process";
  529. }
  530. var selectedRecipeItem = rcp.SelectedItem as TreeViewFileItem;
  531. if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteFolderInfo"].ToString(), folderName),
  532. Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  533. {
  534. this._viewModel.DeleteFolder(folderName,rcpType);
  535. UpdateRecipeFileList();
  536. UpdateRecipeFileListClean();
  537. if (selectedRecipeItem != null)
  538. SelectRecipe(selectedRecipeItem.FileName);
  539. }
  540. }
  541. catch (Exception ex)
  542. {
  543. LOG.Write(ex);
  544. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteFolderException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  545. }
  546. }
  547. /// <summary>
  548. /// rename folder
  549. /// </summary>
  550. /// <param name="sender"></param>
  551. /// <param name="e"></param>
  552. void menuItem_MouseClick_RenameFolder(object sender, RoutedEventArgs e)
  553. {
  554. MenuItem mit = sender as MenuItem;
  555. string folderName = mit.Tag as string;
  556. PerformRenameFolder(folderName);
  557. }
  558. private void PerformRenameFolder(string folderName)
  559. {
  560. try
  561. {
  562. int lastSlashIndex = folderName.LastIndexOf('\\');
  563. string shortName = folderName.Substring(lastSlashIndex + 1);
  564. RecipeNameInputDlg dlg =
  565. new RecipeNameInputDlg(
  566. string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderInfo"].ToString(),
  567. shortName), Application.Current.Resources["GlobalLableMsgInputFolderName"].ToString())
  568. {
  569. Owner = Application.Current.MainWindow
  570. };
  571. if (dlg.ShowDialog() == true)
  572. {
  573. string dir = lastSlashIndex == -1 ? "" : folderName.Substring(0, lastSlashIndex);
  574. string rcpType = "Clean";
  575. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  576. {
  577. rcpType = "Process";
  578. }
  579. if (!_viewModel.RenameFolder(folderName,rcpType, dir + "\\" + dlg.InputText))
  580. {
  581. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderFailed"].ToString(),
  582. Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK,
  583. MessageBoxImage.Warning);
  584. }
  585. else
  586. {
  587. UpdateRecipeFileList();
  588. UpdateRecipeFileListClean();
  589. }
  590. }
  591. }
  592. catch (Exception ex)
  593. {
  594. LOG.Write(ex);
  595. MessageBox.Show(
  596. string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderException"].ToString(),
  597. ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(),
  598. MessageBoxButton.OK, MessageBoxImage.Error);
  599. }
  600. }
  601. /// <summary>
  602. /// import external recipe file into selected folder
  603. /// </summary>
  604. /// <param name="sender"></param>
  605. /// <param name="e"></param>
  606. void menuItem_MouseClick_ImportRecipe(object sender, RoutedEventArgs e)
  607. {
  608. try
  609. {
  610. MenuItem mit = sender as MenuItem;
  611. string folderName = mit.Tag as string;
  612. Microsoft.Win32.OpenFileDialog openDlg = new Microsoft.Win32.OpenFileDialog();
  613. openDlg.DefaultExt = ".rcp";
  614. openDlg.Filter = "Recipe file (.rcp)|*.rcp";
  615. openDlg.Multiselect = true;
  616. if (openDlg.ShowDialog() == true)
  617. {
  618. foreach(var openDlgFileName in openDlg.FileNames)
  619. {
  620. using (StreamReader srd = new StreamReader(openDlgFileName))
  621. {
  622. bool importFailed = false;
  623. string rcpContent = srd.ReadToEnd();
  624. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgRecipeImportInfo"].ToString(), Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  625. {
  626. Owner = Application.Current.MainWindow
  627. };
  628. dlg.InputText = System.IO.Path.GetFileNameWithoutExtension(openDlgFileName);
  629. if (dlg.ShowDialog() == true)
  630. {
  631. var recipeName = mit.Tag as string + "\\" + dlg.InputText;
  632. XmlDocument rcpDoc = new XmlDocument();
  633. rcpDoc.LoadXml(rcpContent);
  634. rcpDoc.DocumentElement.SetAttribute("RecipeVersion", _viewModel.RecipeVersion());
  635. string rcpType = "Clean";
  636. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  637. {
  638. rcpType = "Process";
  639. }
  640. if (!this._viewModel.SaveAsRecipe(recipeName,rcpType,rcpDoc.OuterXml))
  641. {
  642. importFailed = true;
  643. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeImportFailed"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  644. }
  645. }
  646. srd.Close();
  647. if (!importFailed) File.Delete(openDlgFileName);
  648. UpdateRecipeFileList();
  649. UpdateRecipeFileListClean();
  650. SelectRecipe(mit.Tag as string + "\\" + dlg.InputText);
  651. }
  652. }
  653. }
  654. }
  655. catch (Exception ex)
  656. {
  657. LOG.Write(ex);
  658. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeImportException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  659. }
  660. }
  661. /// <summary>
  662. /// delete selected recipe files
  663. /// </summary>
  664. /// <param name="sender"></param>
  665. /// <param name="e"></param>
  666. void menuItem_MouseClick_DeleteRecipe(object sender, RoutedEventArgs e)
  667. {
  668. MenuItem mit = sender as MenuItem;
  669. string recipeName = mit.Tag as string;
  670. recipeName = PerformDeleteRecipe( recipeName);
  671. }
  672. private string PerformDeleteRecipe( string recipeName)
  673. {
  674. try
  675. {
  676. string origin = recipeName;
  677. int sepIndex = recipeName.LastIndexOf("\\");
  678. if (sepIndex < 0)
  679. sepIndex = 0;
  680. else
  681. sepIndex++;
  682. recipeName = recipeName.Substring(sepIndex);
  683. if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteInfo"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(),
  684. MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  685. {
  686. string rcpType = "Clean";
  687. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  688. {
  689. rcpType = "Process";
  690. }
  691. this._viewModel.DeleteRecipe(origin,rcpType);
  692. this.UpdateRecipeFileList();
  693. UpdateRecipeFileListClean();
  694. }
  695. }
  696. catch (Exception ex)
  697. {
  698. LOG.Write(ex);
  699. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  700. }
  701. return recipeName;
  702. }
  703. /// <summary>
  704. /// rename selected recipe files
  705. /// </summary>
  706. /// <param name="sender"></param>
  707. /// <param name="e"></param>
  708. void menuItem_MouseClick_RenameRecipe(object sender, RoutedEventArgs e)
  709. {
  710. MenuItem mit = sender as MenuItem;
  711. string recipeName = mit.Tag as string;
  712. PerformRenameRecipe(recipeName);
  713. }
  714. private void PerformRenameRecipe(string recipeName)
  715. {
  716. try
  717. {
  718. int lastSlashIndex = recipeName.LastIndexOf('\\');
  719. string shortName = recipeName.Substring(lastSlashIndex + 1);
  720. var rcp = treeViewRcpListClean;
  721. string rcpType = "Clean";
  722. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  723. {
  724. rcp = treeViewRcpList;
  725. rcpType = "Process";
  726. }
  727. TreeViewItem rightClickedItem = rcp.SelectedItem as TreeViewItem;
  728. RecipeNameInputDlg dlg = new RecipeNameInputDlg(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameInfo"].ToString(), shortName), Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  729. {
  730. InputText = shortName,
  731. Owner = Application.Current.MainWindow
  732. };
  733. if (dlg.ShowDialog() == true)
  734. {
  735. string newRecipeName = lastSlashIndex == -1 ? dlg.InputText : (recipeName.Substring(0, lastSlashIndex) + "\\" + dlg.InputText);
  736. var ret = this._viewModel.RenameRecipe(recipeName,rcpType, newRecipeName);
  737. if (ret)
  738. {
  739. UpdateRecipeFileList();
  740. UpdateRecipeFileListClean();
  741. SelectRecipe(newRecipeName);
  742. }
  743. else
  744. {
  745. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFailed"].ToString(), newRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  746. }
  747. }
  748. }
  749. catch (Exception ex)
  750. {
  751. LOG.Write(ex);
  752. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  753. }
  754. }
  755. /// <summary>
  756. /// export selected recipe files
  757. /// </summary>
  758. /// <param name="sender"></param>
  759. /// <param name="e"></param>
  760. void menuItem_MouseClick_ExportRecipe(object sender, RoutedEventArgs e)
  761. {
  762. try
  763. {
  764. MenuItem mit = sender as MenuItem;
  765. string recipeName = mit.Tag as string;
  766. var dlg = new Microsoft.Win32.SaveFileDialog();
  767. dlg.FileName = recipeName.Substring(recipeName.LastIndexOf('\\') + 1);
  768. dlg.DefaultExt = ".rcp";
  769. dlg.Filter = Application.Current.Resources["GlobalLableMsgRecipeExportFilter"].ToString();
  770. if (dlg.ShowDialog() == true)
  771. {
  772. using (StreamWriter sw = new StreamWriter(dlg.FileName))
  773. {
  774. string rcpType = "Clean";
  775. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  776. {
  777. rcpType = "Process";
  778. }
  779. sw.Write(this._viewModel.LoadRecipe(recipeName,rcpType));
  780. sw.Close();
  781. }
  782. }
  783. }
  784. catch (Exception ex)
  785. {
  786. LOG.Write(ex);
  787. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  788. }
  789. }
  790. void menuItem_MouseClick_ExportExcel(object sender, RoutedEventArgs e)
  791. {
  792. MenuItem mit = sender as MenuItem;
  793. string recipeName = mit.Tag as string;
  794. var dlg = new Microsoft.Win32.SaveFileDialog();
  795. dlg.FileName = recipeName.Substring(recipeName.LastIndexOf('\\') + 1);
  796. dlg.DefaultExt = ".xls";
  797. dlg.Filter = Application.Current.Resources["GlobalLableMsgRecipeExportExcelFilter"].ToString();
  798. var ret = dlg.ShowDialog();
  799. if (!ret.HasValue || !ret.Value)
  800. return;
  801. string fileName = dlg.FileName;
  802. this._viewModel.CurrentRecipeName = recipeName;
  803. string rcpType = "Clean";
  804. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  805. {
  806. rcpType = "Process";
  807. }
  808. string xmlRecipeData = _viewModel.LoadRecipe(recipeName,rcpType);
  809. XmlDocument doc = new XmlDocument();
  810. doc.LoadXml(_viewModel.RecipeFormat);
  811. Task.Factory.StartNew(() =>
  812. {
  813. try
  814. {
  815. if (File.Exists(fileName))
  816. File.Delete(fileName);
  817. Workbook workbook = new Workbook();
  818. Worksheet worksheet = new Worksheet("First Sheet");
  819. var catalogNodes = doc.SelectNodes("/TableRecipeFormat/Catalog");
  820. int groupRowIndex = 0;
  821. int stepRowIndex = 0;
  822. int catalogRowIndex = 0;
  823. List<string> excelRowHeaderName = new List<string>();
  824. foreach (XmlElement cNode in catalogNodes)
  825. {
  826. XmlNodeList sNodes = cNode.SelectNodes("Group/Step");
  827. string catalogHeader = cNode.Attributes["DisplayName"].Value;
  828. worksheet.Cells[catalogRowIndex + 1, 0] = new Cell(catalogHeader);
  829. catalogRowIndex += sNodes.Count;
  830. XmlNodeList gNodes = cNode.SelectNodes("Group");
  831. foreach (XmlElement gNode in gNodes)
  832. {
  833. XmlNodeList stepNodes = gNode.SelectNodes("Step");
  834. string groupHeader = gNode.Attributes["DisplayName"].Value;
  835. worksheet.Cells[groupRowIndex + 1, 1] = new Cell(groupHeader);
  836. groupRowIndex += stepNodes.Count;
  837. foreach (XmlElement nd in stepNodes)
  838. {
  839. string stepHeader = nd.Attributes["DisplayName"].Value;
  840. worksheet.Cells[stepRowIndex + 1, 2] = new Cell(stepHeader);
  841. excelRowHeaderName.Add(nd.Attributes["ControlName"].Value);
  842. stepRowIndex++;
  843. }
  844. }
  845. }
  846. XmlDocument rcp = new XmlDocument();
  847. rcp.LoadXml(xmlRecipeData);
  848. XmlNodeList stepNodeList = rcp.SelectNodes("/TableRecipeData/Step");
  849. for (int stepNo = 0; stepNo < stepNodeList.Count; stepNo++)
  850. {
  851. var dataDic = new Dictionary<string, string>();
  852. foreach (XmlAttribute att in (stepNodeList[stepNo] as XmlElement).Attributes)
  853. {
  854. dataDic.Add(att.Name, att.Value);
  855. }
  856. //fill sub node's attributes
  857. foreach (XmlElement subStep in stepNodeList[stepNo].ChildNodes)
  858. {
  859. foreach (XmlAttribute att in subStep.Attributes)
  860. {
  861. dataDic.Add(att.Name, att.Value);
  862. }
  863. foreach (XmlElement subsubStep in subStep.ChildNodes)
  864. {
  865. foreach (XmlAttribute att2 in subsubStep.Attributes)
  866. {
  867. dataDic.Add(att2.Name, att2.Value);
  868. }
  869. }
  870. }
  871. long length = dataDic.LongCount();
  872. worksheet.Cells[0, 3] = new Cell(recipeName);
  873. for (int rowIndex = 0; rowIndex < excelRowHeaderName.Count; rowIndex++)
  874. {
  875. worksheet.Cells.ColumnWidth[(ushort)(rowIndex + 1)] = 4000;
  876. string cellValueString = dataDic[excelRowHeaderName[rowIndex]];
  877. double cellValue;
  878. if (double.TryParse(cellValueString, out cellValue))
  879. {
  880. worksheet.Cells[rowIndex + 1, stepNo + 3] = new Cell(cellValue);
  881. }
  882. else
  883. {
  884. worksheet.Cells[rowIndex + 1, stepNo + 3] = new Cell(cellValueString);
  885. }
  886. }
  887. }
  888. workbook.Worksheets.Add(worksheet);
  889. workbook.Save(fileName);
  890. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportExcelOk"].ToString(), recipeName, fileName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Information);
  891. }
  892. catch (Exception ex)
  893. {
  894. LOG.Write(ex);
  895. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportExcelFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  896. }
  897. });
  898. }
  899. private void ButtonNew(object sender, RoutedEventArgs e)
  900. {
  901. if (MenuPermission != 3) return;
  902. var rcp = treeViewRcpListClean;
  903. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  904. {
  905. rcp = treeViewRcpList;
  906. }
  907. var focus = rcp.SelectedItem;
  908. string folder = "";
  909. if (focus is TreeViewFileItem file)
  910. {
  911. var name = file.FileName;
  912. if (name.Contains("\\"))
  913. folder = name.Substring(0, name.LastIndexOf('\\'));
  914. }
  915. else if (focus is TreeViewFolderItem folderItem)
  916. {
  917. folder = folderItem.FolderName;
  918. }
  919. PerformCreateRecipe(folder);
  920. }
  921. private void ButtonRename(object sender, RoutedEventArgs e)
  922. {
  923. if (MenuPermission != 3) return;
  924. var rcp = treeViewRcpListClean;
  925. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  926. {
  927. rcp = treeViewRcpList;
  928. }
  929. var focus = rcp.SelectedItem;
  930. if (focus is TreeViewFileItem file)
  931. {
  932. var name = file.FileName;
  933. PerformRenameRecipe(file.FileName);
  934. }
  935. else if (focus is TreeViewFolderItem folderItem)
  936. {
  937. PerformRenameFolder(folderItem.FolderName);
  938. }
  939. }
  940. private void ButtonDelete(object sender, RoutedEventArgs e)
  941. {
  942. if (MenuPermission != 3) return;
  943. var rcp = treeViewRcpListClean;
  944. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  945. {
  946. rcp = treeViewRcpList;
  947. }
  948. var focus = rcp.SelectedItem;
  949. if (focus is TreeViewFileItem file)
  950. {
  951. var name = file.FileName;
  952. PerformDeleteRecipe(file.FileName);
  953. }
  954. else if (focus is TreeViewFolderItem folderItem)
  955. {
  956. PerformDeleteFolder(folderItem.FolderName);
  957. }
  958. }
  959. private void PerformCreateRecipe(string folderName)
  960. {
  961. try
  962. {
  963. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  964. {
  965. Owner = Application.Current.MainWindow
  966. };
  967. if (dlg.ShowDialog() == true)
  968. {
  969. var recipeName = folderName + "\\" + dlg.InputText;
  970. string rcpType = "Clean";
  971. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  972. {
  973. rcpType = "Process";
  974. }
  975. if (!this._viewModel.SaveAsRecipe(recipeName,rcpType, this._viewModel.RecipeTemplate))
  976. {
  977. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateFailed"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  978. }
  979. else
  980. {
  981. UpdateRecipeFileList();
  982. UpdateRecipeFileListClean();
  983. SelectRecipe(folderName + "\\" + dlg.InputText);
  984. }
  985. }
  986. }
  987. catch (Exception ex)
  988. {
  989. LOG.Write(ex);
  990. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  991. }
  992. }
  993. private void TreeViewRcpList_DragEnter(object sender, DragEventArgs e)
  994. {
  995. try
  996. {
  997. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) != null)
  998. {
  999. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) == GetAncestor<TreeViewFileItem>((DependencyObject)OriginalItem)
  1000. || VisualTreeHelper.GetParent(GetAncestor<TreeViewFileItem>((DependencyObject)e.Source)) == VisualTreeHelper.GetParent(GetAncestor<TreeViewFileItem>((DependencyObject)OriginalItem)))
  1001. e.Effects = DragDropEffects.None;
  1002. }
  1003. else if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  1004. {
  1005. if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) == GetAncestor<TreeViewFolderItem>((DependencyObject)OriginalItem))
  1006. e.Effects = DragDropEffects.None;
  1007. }
  1008. else if (e.Source is TreeView)
  1009. {
  1010. e.Effects = DragDropEffects.None;
  1011. }
  1012. e.Handled = true;
  1013. }
  1014. catch (Exception ex)
  1015. {
  1016. LOG.Write(ex);
  1017. }
  1018. }
  1019. private void TreeViewRcpList_Drop(object sender, DragEventArgs e)
  1020. {
  1021. try
  1022. {
  1023. var rcp = treeViewRcpListClean;
  1024. string rcpType = "Clean";
  1025. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  1026. {
  1027. rcp = treeViewRcpList;
  1028. rcpType = "Process";
  1029. }
  1030. TreeViewItem itemRemoved = rcp.SelectedItem as TreeViewItem;
  1031. #region
  1032. //foreach (TreeViewItem item in treeViewRcpList.Items)
  1033. //{
  1034. // if (itemRemoved.Equals(item))
  1035. // {
  1036. // treeViewRcpList.Items.Remove(itemRemoved);
  1037. // break;
  1038. // }
  1039. // if (item.Items.Count > 0)
  1040. // {
  1041. // foreach (TreeViewItem subItem in item.Items)
  1042. // {
  1043. // if (itemRemoved.Equals(subItem))
  1044. // {
  1045. // item.Items.Remove(itemRemoved);
  1046. // break;
  1047. // }
  1048. // }
  1049. // }
  1050. //}
  1051. //(getTreeViewFolderItem as TreeViewFolderItem).Items.Add(itemRemoved);
  1052. #endregion
  1053. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) != null)
  1054. {
  1055. if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  1056. {
  1057. TreeViewFolderItem getTreeViewFolderItem = GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source);
  1058. tragetFilePath = getTreeViewFolderItem.FolderName;
  1059. GetAncestor<TreeView>(getTreeViewFolderItem);
  1060. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName,rcpType, tragetFilePath);
  1061. }
  1062. else
  1063. {
  1064. tragetFilePath = "";
  1065. GetAncestor<TreeView>((DependencyObject)e.Source);
  1066. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName,rcpType, tragetFilePath);
  1067. }
  1068. }
  1069. else if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  1070. {
  1071. TreeViewFolderItem getTreeViewFolderItem = GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source);
  1072. tragetFilePath = getTreeViewFolderItem.FolderName;
  1073. GetAncestor<TreeView>(getTreeViewFolderItem);
  1074. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName,rcpType, tragetFilePath);
  1075. }
  1076. UpdateRecipeFileList();
  1077. UpdateRecipeFileListClean();
  1078. SelectRecipe(tragetFilePath + "\\" + System.IO.Path.GetFileNameWithoutExtension(((TreeViewFileItem)itemRemoved).FileName));
  1079. }
  1080. catch (Exception ex)
  1081. {
  1082. LOG.Write(ex);
  1083. }
  1084. }
  1085. private void TreeViewRcpList_MouseDown(object sender, MouseButtonEventArgs e)
  1086. {
  1087. var rcp = treeViewRcpListClean;
  1088. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  1089. {
  1090. rcp = treeViewRcpList;
  1091. }
  1092. if (e.ChangedButton == MouseButton.Left)
  1093. {
  1094. _lastMouseDown = e.GetPosition(rcp);
  1095. }
  1096. }
  1097. private void TreeViewRcpList_MouseMove(object sender, MouseEventArgs e)
  1098. {
  1099. try
  1100. {
  1101. var rcp = treeViewRcpListClean;
  1102. if (Rcptype.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:Process Content:")
  1103. {
  1104. rcp = treeViewRcpList;
  1105. }
  1106. if (e.LeftButton == MouseButtonState.Pressed)
  1107. {
  1108. Point currentPosition = e.GetPosition(rcp);
  1109. OriginalItem = e.OriginalSource;
  1110. if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 2.0) || (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 2.0))
  1111. {
  1112. if ((treeViewRcpList.SelectedItem as TreeViewItem) != null)
  1113. {
  1114. //DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeViewRcpList, treeViewRcpList.SelectedItem, DragDropEffects.Move);
  1115. DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeViewRcpList.SelectedItem as TreeViewItem, sender, DragDropEffects.Move);
  1116. }
  1117. }
  1118. }
  1119. }
  1120. catch (Exception ex)
  1121. {
  1122. LOG.Write(ex);
  1123. }
  1124. }
  1125. /// <summary>
  1126. /// 子页面获取父类窗体
  1127. /// </summary>
  1128. /// <typeparam name="T"></typeparam>
  1129. /// <param name="reference"></param>
  1130. /// <returns></returns>
  1131. public T GetAncestor<T>(DependencyObject reference) where T : DependencyObject
  1132. {
  1133. DependencyObject parent = VisualTreeHelper.GetParent(reference);
  1134. while (!(parent is T) && parent != null)
  1135. {
  1136. parent = VisualTreeHelper.GetParent(parent);
  1137. if (parent is TreeViewFolderItem)
  1138. tragetFilePath = tragetFilePath.Insert(0, $"{((TreeViewFolderItem)parent).FolderName}\\");
  1139. }
  1140. if (parent != null)
  1141. return (T)parent;
  1142. else
  1143. return null;
  1144. }//在子页面中调用GetAncestor<MainWindow>(this);
  1145. }
  1146. }