RecipeEditorView.xaml.cs 49 KB

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