RecipeEditorView.xaml.cs 49 KB

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