RecipeEditorView.xaml.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using System.Xml;
  19. using Aitex.Core.Account;
  20. using Aitex.Triton160.UI.ViewModel;
  21. using ExcelLibrary.SpreadSheet;
  22. using Triton160.UI.ViewModel;
  23. using Aitex.Core.RT.Log;
  24. using Aitex.Core.UI.View.Common;
  25. using Aitex.UI.RecipeEditor.View;
  26. using Aitex.Triton160.Common;
  27. namespace Aitex.Triton160.UI.Views
  28. {
  29. /// <summary>
  30. /// Interaction logic for RecipeEditorView.xaml
  31. /// </summary>
  32. public partial class RecipeEditorView : UserControl
  33. {
  34. private string _culture;
  35. private string tragetFilePath = "";
  36. private object OriginalItem;
  37. RecipeEditorViewModel _viewModel;
  38. private Point _lastMouseDown;
  39. private ViewPermission _permission;
  40. public RecipeEditorView()
  41. {
  42. InitializeComponent();
  43. this.DataContext = _viewModel = new RecipeEditorViewModel(new UiRecipeManager(), ModuleName.System.ToString());
  44. this.Loaded += new RoutedEventHandler(RecipeEditorView_Loaded);
  45. this.Unloaded += new RoutedEventHandler(RecipeEditorView_Unloaded);
  46. Triton160UiSystem.Instance.CultureChanged += CultureChanged;
  47. }
  48. private void CultureChanged(string s)
  49. {
  50. _culture = s;
  51. this.tableRecipeGrid.UpdateCultureResource(s);
  52. }
  53. static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
  54. {
  55. while (source != null && source.GetType() != typeof(T))
  56. source = VisualTreeHelper.GetParent(source);
  57. return source;
  58. }
  59. void RecipeEditorView_Unloaded(object sender, RoutedEventArgs e)
  60. {
  61. }
  62. void RecipeEditorView_Loaded(object sender, RoutedEventArgs e)
  63. {
  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(Triton160UiSystem.Instance.CurrentUserName);
  72. UpdateRecipeFileList();
  73. this.Name = "recipeEditor";
  74. _permission = 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()
  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. ret = _viewModel.SaveRecipe(this._viewModel.CurrentRecipeName, this.tableRecipeGrid.ControlViewModel.GetRecipeContentString());
  145. if (ret)
  146. {
  147. tableRecipeGrid.Dispatcher.Invoke(new System.Action(() =>
  148. {
  149. this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, _viewModel.LoadRecipe(_viewModel.CurrentRecipeName));
  150. }));
  151. }
  152. }
  153. else
  154. {
  155. ret = false;
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. LOG.Write(ex);
  161. MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveException"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  162. ret = false;
  163. }
  164. return ret;
  165. }
  166. private void treeViewRcpList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  167. {
  168. if (_permission != ViewPermission.FullyControl)
  169. return;
  170. //black place right mouse click
  171. treeViewRcpList.ContextMenu = new ContextMenu();
  172. MenuItem menuItem = new MenuItem();
  173. menuItem = new MenuItem();
  174. menuItem.Tag = "\\";
  175. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  176. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"];
  177. this.treeViewRcpList.ContextMenu.Items.Add(menuItem);
  178. menuItem = new MenuItem();
  179. menuItem.Tag = "\\";
  180. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe);
  181. menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"];
  182. this.treeViewRcpList.ContextMenu.Items.Add(menuItem);
  183. this.treeViewRcpList.ContextMenu.Items.Add(new Separator());
  184. menuItem = new MenuItem();
  185. menuItem.Tag = "\\";
  186. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder);
  187. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"];
  188. this.treeViewRcpList.ContextMenu.Items.Add(menuItem);
  189. treeViewRcpList.ContextMenu.Visibility = Visibility.Visible;
  190. }
  191. private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  192. {
  193. if (_permission != ViewPermission.FullyControl)
  194. return;
  195. var treeViewFileItem = VisualUpwardSearch<TreeViewFileItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
  196. if (treeViewFileItem != null)
  197. {
  198. treeViewFileItem.Focus();
  199. treeViewFileItem.IsSelected = true;
  200. treeViewFileItem.ContextMenu = new ContextMenu();
  201. MenuItem menuItem = null;
  202. menuItem = new MenuItem();
  203. menuItem.Tag = treeViewFileItem.Tag as string;
  204. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ExportRecipe);
  205. menuItem.Header = Application.Current.Resources["GlobalLableMenuExportRecipe"];
  206. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  207. menuItem = new MenuItem();
  208. menuItem.Tag = treeViewFileItem.Tag as string;
  209. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ExportExcel);
  210. menuItem.Header = Application.Current.Resources["GlobalLableMenuExportRecipeAsExcel"];
  211. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  212. menuItem = new MenuItem();
  213. menuItem.Tag = treeViewFileItem.Tag as string;
  214. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_SaveAs);
  215. menuItem.Header = Application.Current.Resources["GlobalLableMenuSaveAs"];
  216. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  217. menuItem = new MenuItem();
  218. menuItem.Tag = treeViewFileItem.Tag as string;
  219. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameRecipe);
  220. menuItem.Header = Application.Current.Resources["GlobalLableMenuRenameRecipe"];
  221. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  222. menuItem = new MenuItem();
  223. menuItem.Tag = treeViewFileItem.Tag as string;
  224. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteRecipe);
  225. menuItem.Header = Application.Current.Resources["GlobalLableMenuDeleteRecipe"];
  226. treeViewFileItem.ContextMenu.Items.Add(menuItem);
  227. treeViewFileItem.ContextMenu.Visibility = Visibility.Visible;
  228. }
  229. else
  230. {
  231. var treeViewFolderItem = VisualUpwardSearch<TreeViewFolderItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
  232. if (treeViewFolderItem != null)
  233. {
  234. treeViewFolderItem.Focus();
  235. treeViewFolderItem.IsSelected = true;
  236. treeViewFolderItem.ContextMenu = new ContextMenu();
  237. MenuItem menuItem = null;
  238. menuItem = new MenuItem();
  239. menuItem.Tag = treeViewFolderItem.Tag as string;
  240. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  241. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"];
  242. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  243. menuItem = new MenuItem();
  244. menuItem.Tag = treeViewFolderItem.Tag as string;
  245. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe);
  246. menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"];
  247. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  248. treeViewFolderItem.ContextMenu.Items.Add(new Separator());
  249. menuItem = new MenuItem();
  250. menuItem.Tag = treeViewFolderItem.Tag as string;
  251. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder);
  252. menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"];
  253. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  254. menuItem = new MenuItem();
  255. menuItem.Tag = treeViewFolderItem.Tag as string;
  256. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameFolder);
  257. menuItem.Header = Application.Current.Resources["GlobalLableMenuRenameFolder"];
  258. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  259. menuItem = new MenuItem();
  260. menuItem.Tag = treeViewFolderItem.Tag as string;
  261. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteFolder);
  262. menuItem.Header = Application.Current.Resources["GlobalLableMenuDeleteFolder"];
  263. treeViewFolderItem.ContextMenu.Items.Add(menuItem);
  264. treeViewFolderItem.ContextMenu.Visibility = Visibility.Visible;
  265. }
  266. }
  267. }
  268. /// <summary>
  269. /// reload current recipe content
  270. /// </summary>
  271. /// <param name="sender"></param>
  272. /// <param name="e"></param>
  273. private void buttonCodeReload_Click(object sender, RoutedEventArgs e)
  274. {
  275. if (MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeReloadInfo"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
  276. {
  277. this.tableRecipeGrid.Dispatcher.Invoke(new System.Action(() =>
  278. {
  279. this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, _viewModel.LoadRecipe(_viewModel.CurrentRecipeName));
  280. }));
  281. }
  282. }
  283. /// <summary>
  284. /// Save recipe file
  285. /// </summary>
  286. /// <param name="sender"></param>
  287. /// <param name="e"></param>
  288. private void buttonCodeSave_Click(object sender, RoutedEventArgs e)
  289. {
  290. SaveRecipe();
  291. }
  292. void SelectRecipe(string recipeName)
  293. {
  294. try
  295. {
  296. string[] paths = recipeName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
  297. string fileName = "";
  298. for (int i = 0; i < paths.Length - 1; i++)
  299. fileName += paths[i] + "\\";
  300. fileName += paths[paths.Length - 1];
  301. selectRecipe(treeViewRcpList, paths, 0, fileName);
  302. }
  303. catch (Exception ex)
  304. {
  305. LOG.Write(ex);
  306. }
  307. }
  308. ItemsControl selectRecipe(ItemsControl currentNode, string[] paths, int index, string fileName)
  309. {
  310. if (currentNode == null)
  311. return null;
  312. if (index == paths.Length - 1)
  313. {
  314. foreach (var item in currentNode.Items)
  315. {
  316. TreeViewFileItem tvf = item as TreeViewFileItem;
  317. if (tvf != null && tvf.FileName == fileName)
  318. {
  319. tvf.IsSelected = true;
  320. return null;
  321. }
  322. }
  323. }
  324. foreach (var item in currentNode.Items)
  325. {
  326. TreeViewFolderItem tvf = item as TreeViewFolderItem;
  327. if (tvf != null && tvf.FolderName == paths[index])
  328. {
  329. tvf.IsExpanded = true;
  330. selectRecipe(tvf, paths, index + 1, fileName);
  331. break;
  332. }
  333. }
  334. return null;
  335. }
  336. private void UpdateRecipeFileList()
  337. {
  338. XmlDocument doc = new XmlDocument();
  339. try
  340. {
  341. string rcp = _viewModel.GetXmlRecipeList();
  342. if (string.IsNullOrWhiteSpace(rcp))
  343. return;
  344. doc.LoadXml(rcp);
  345. }
  346. catch (Exception ex)
  347. {
  348. LOG.Write(ex);
  349. MessageBox.Show("Cannot update recipe files", "RecipeEditorView", MessageBoxButton.OK, MessageBoxImage.Error);
  350. }
  351. this.treeViewRcpList.Items.Clear();
  352. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList);
  353. }
  354. void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl)
  355. {
  356. foreach (XmlElement ele in curElementNode.ChildNodes)
  357. {
  358. if (ele.Name == "File")
  359. {
  360. string fileName = ele.Attributes["Name"].Value;
  361. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  362. TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value);
  363. item.Tag = ele.Attributes["Name"].Value;
  364. item.ToolTip = fileName;
  365. itemsControl.Items.Add(item);
  366. }
  367. else if (ele.Name == "Folder")
  368. {
  369. string folderName = ele.Attributes["Name"].Value;
  370. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  371. TreeViewFolderItem item = new TreeViewFolderItem(folderName);
  372. item.Tag = ele.Attributes["Name"].Value;
  373. CreateTreeViewItems(ele, item);
  374. item.IsExpanded = false;
  375. itemsControl.Items.Add(item);
  376. }
  377. }
  378. }
  379. void menuItem_MouseClick_SaveAs(object sender, RoutedEventArgs e)
  380. {
  381. try
  382. {
  383. MenuItem mit = sender as MenuItem;
  384. var oldName = mit.Tag as string;
  385. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  386. {
  387. InputText = oldName,
  388. Owner = Application.Current.MainWindow
  389. };
  390. var ret = dlg.ShowDialog();
  391. if (ret.HasValue && ret.Value)
  392. {
  393. var newName = dlg.InputText;
  394. if (_viewModel.SaveAsRecipe(newName, _viewModel.LoadRecipe(oldName)))
  395. {
  396. UpdateRecipeFileList();
  397. SelectRecipe(newName);
  398. }
  399. else
  400. {
  401. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeSaveAsFailed"].ToString(), newName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  402. }
  403. }
  404. }
  405. catch (Exception ex)
  406. {
  407. LOG.Write(ex);
  408. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeSaveAsException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  409. }
  410. }
  411. void menuItem_MouseClick_CreateFolder(object sender, RoutedEventArgs e)
  412. {
  413. try
  414. {
  415. MenuItem mit = sender as MenuItem;
  416. string folderName = mit.Tag as string;
  417. var selectedRecipeItem = treeViewRcpList.SelectedItem as TreeViewFileItem;
  418. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputFolderName"].ToString())
  419. {
  420. Owner = Application.Current.MainWindow
  421. };
  422. if (dlg.ShowDialog() == true)
  423. {
  424. this._viewModel.CreateFolder(folderName + "\\" + dlg.InputText);
  425. UpdateRecipeFileList();
  426. if (selectedRecipeItem != null)
  427. SelectRecipe(selectedRecipeItem.FileName);
  428. }
  429. }
  430. catch (Exception ex)
  431. {
  432. LOG.Write(ex);
  433. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeFolderNewFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString());
  434. }
  435. }
  436. /// <summary>
  437. /// Create new recipe
  438. /// </summary>
  439. /// <param name="sender"></param>
  440. /// <param name="e"></param>
  441. void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e)
  442. {
  443. try
  444. {
  445. MenuItem mit = sender as MenuItem;
  446. string folderName = mit.Tag as string;
  447. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  448. {
  449. Owner = Application.Current.MainWindow
  450. };
  451. if (dlg.ShowDialog() == true)
  452. {
  453. var recipeName = folderName + "\\" + dlg.InputText;
  454. if (!this._viewModel.SaveAsRecipe(recipeName, this._viewModel.RecipeTemplate))
  455. {
  456. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateFailed"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  457. }
  458. else
  459. {
  460. UpdateRecipeFileList();
  461. SelectRecipe(folderName + "\\" + dlg.InputText);
  462. }
  463. }
  464. }
  465. catch (Exception ex)
  466. {
  467. LOG.Write(ex);
  468. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  469. }
  470. }
  471. /// <summary>
  472. /// delete folder
  473. /// </summary>
  474. /// <param name="sender"></param>
  475. /// <param name="e"></param>
  476. void menuItem_MouseClick_DeleteFolder(object sender, RoutedEventArgs e)
  477. {
  478. try
  479. {
  480. MenuItem mit = sender as MenuItem;
  481. var selectedRecipeItem = treeViewRcpList.SelectedItem as TreeViewFileItem;
  482. string folderName = mit.Tag as string;
  483. if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteFolderInfo"].ToString(), folderName),
  484. Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  485. {
  486. this._viewModel.DeleteFolder(folderName);
  487. UpdateRecipeFileList();
  488. if (selectedRecipeItem != null)
  489. SelectRecipe(selectedRecipeItem.FileName);
  490. }
  491. }
  492. catch (Exception ex)
  493. {
  494. LOG.Write(ex);
  495. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteFolderException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  496. }
  497. }
  498. /// <summary>
  499. /// rename folder
  500. /// </summary>
  501. /// <param name="sender"></param>
  502. /// <param name="e"></param>
  503. void menuItem_MouseClick_RenameFolder(object sender, RoutedEventArgs e)
  504. {
  505. try
  506. {
  507. MenuItem mit = sender as MenuItem;
  508. string folderName = mit.Tag as string;
  509. int lastSlashIndex = folderName.LastIndexOf('\\');
  510. string shortName = folderName.Substring(lastSlashIndex + 1);
  511. RecipeNameInputDlg dlg = new RecipeNameInputDlg(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderInfo"].ToString(), 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(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  521. }
  522. else
  523. {
  524. UpdateRecipeFileList();
  525. }
  526. }
  527. }
  528. catch (Exception ex)
  529. {
  530. LOG.Write(ex);
  531. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  532. }
  533. }
  534. /// <summary>
  535. /// import external recipe file into selected folder
  536. /// </summary>
  537. /// <param name="sender"></param>
  538. /// <param name="e"></param>
  539. void menuItem_MouseClick_ImportRecipe(object sender, RoutedEventArgs e)
  540. {
  541. try
  542. {
  543. MenuItem mit = sender as MenuItem;
  544. string folderName = mit.Tag as string;
  545. Microsoft.Win32.OpenFileDialog openDlg = new Microsoft.Win32.OpenFileDialog();
  546. openDlg.DefaultExt = ".rcp";
  547. openDlg.Filter = "Recipe file (.rcp)|*.rcp";
  548. openDlg.Multiselect = true;
  549. if (openDlg.ShowDialog() == true)
  550. {
  551. foreach (var openDlgFileName in openDlg.FileNames)
  552. {
  553. using (StreamReader srd = new StreamReader(openDlgFileName))
  554. {
  555. bool importFailed = false;
  556. string rcpContent = srd.ReadToEnd();
  557. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgRecipeImportInfo"].ToString(), Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  558. {
  559. Owner = Application.Current.MainWindow
  560. };
  561. dlg.InputText = System.IO.Path.GetFileNameWithoutExtension(openDlgFileName);
  562. if (dlg.ShowDialog() == true)
  563. {
  564. var recipeName = mit.Tag as string + "\\" + dlg.InputText;
  565. XmlDocument rcpDoc = new XmlDocument();
  566. rcpDoc.LoadXml(rcpContent);
  567. rcpDoc.DocumentElement.SetAttribute("RecipeVersion", _viewModel.RecipeVersion());
  568. if (!this._viewModel.SaveAsRecipe(recipeName, rcpDoc.OuterXml))
  569. {
  570. importFailed = true;
  571. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeImportFailed"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  572. }
  573. }
  574. srd.Close();
  575. if (!importFailed) File.Delete(openDlgFileName);
  576. UpdateRecipeFileList();
  577. SelectRecipe(mit.Tag as string + "\\" + dlg.InputText);
  578. }
  579. }
  580. }
  581. }
  582. catch (Exception ex)
  583. {
  584. LOG.Write(ex);
  585. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeImportException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  586. }
  587. }
  588. /// <summary>
  589. /// delete selected recipe files
  590. /// </summary>
  591. /// <param name="sender"></param>
  592. /// <param name="e"></param>
  593. void menuItem_MouseClick_DeleteRecipe(object sender, RoutedEventArgs e)
  594. {
  595. try
  596. {
  597. MenuItem mit = sender as MenuItem;
  598. string recipeName = mit.Tag as string;
  599. int sepIndex = recipeName.LastIndexOf("\\");
  600. if (sepIndex < 0)
  601. sepIndex = 0;
  602. else
  603. sepIndex++;
  604. recipeName = recipeName.Substring(sepIndex);
  605. if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteInfo"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(),
  606. MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  607. {
  608. this._viewModel.DeleteRecipe(mit.Tag as string);
  609. this.UpdateRecipeFileList();
  610. }
  611. }
  612. catch (Exception ex)
  613. {
  614. LOG.Write(ex);
  615. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  616. }
  617. }
  618. /// <summary>
  619. /// rename selected recipe files
  620. /// </summary>
  621. /// <param name="sender"></param>
  622. /// <param name="e"></param>
  623. void menuItem_MouseClick_RenameRecipe(object sender, RoutedEventArgs e)
  624. {
  625. try
  626. {
  627. MenuItem mit = sender as MenuItem;
  628. string recipeName = mit.Tag as string;
  629. int lastSlashIndex = recipeName.LastIndexOf('\\');
  630. string shortName = recipeName.Substring(lastSlashIndex + 1);
  631. TreeViewItem rightClickedItem = this.treeViewRcpList.SelectedItem as TreeViewItem;
  632. RecipeNameInputDlg dlg = new RecipeNameInputDlg(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameInfo"].ToString(), shortName), Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  633. {
  634. InputText = shortName,
  635. Owner = Application.Current.MainWindow
  636. };
  637. if (dlg.ShowDialog() == true)
  638. {
  639. string newRecipeName = lastSlashIndex == -1 ? dlg.InputText : (recipeName.Substring(0, lastSlashIndex) + "\\" + dlg.InputText);
  640. var ret = this._viewModel.RenameRecipe(recipeName, newRecipeName);
  641. if (ret)
  642. {
  643. UpdateRecipeFileList();
  644. SelectRecipe(newRecipeName);
  645. }
  646. else
  647. {
  648. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFailed"].ToString(), newRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
  649. }
  650. }
  651. }
  652. catch (Exception ex)
  653. {
  654. LOG.Write(ex);
  655. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  656. }
  657. }
  658. /// <summary>
  659. /// export selected recipe files
  660. /// </summary>
  661. /// <param name="sender"></param>
  662. /// <param name="e"></param>
  663. void menuItem_MouseClick_ExportRecipe(object sender, RoutedEventArgs e)
  664. {
  665. try
  666. {
  667. MenuItem mit = sender as MenuItem;
  668. string recipeName = mit.Tag as string;
  669. var dlg = new Microsoft.Win32.SaveFileDialog();
  670. dlg.FileName = recipeName.Substring(recipeName.LastIndexOf('\\') + 1);
  671. dlg.DefaultExt = ".rcp";
  672. dlg.Filter = Application.Current.Resources["GlobalLableMsgRecipeExportFilter"].ToString();
  673. if (dlg.ShowDialog() == true)
  674. {
  675. using (StreamWriter sw = new StreamWriter(dlg.FileName))
  676. {
  677. sw.Write(this._viewModel.LoadRecipe(recipeName));
  678. sw.Close();
  679. }
  680. }
  681. }
  682. catch (Exception ex)
  683. {
  684. LOG.Write(ex);
  685. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  686. }
  687. }
  688. void menuItem_MouseClick_ExportExcel(object sender, RoutedEventArgs e)
  689. {
  690. MenuItem mit = sender as MenuItem;
  691. string recipeName = mit.Tag as string;
  692. var dlg = new Microsoft.Win32.SaveFileDialog();
  693. dlg.FileName = recipeName.Substring(recipeName.LastIndexOf('\\') + 1);
  694. dlg.DefaultExt = ".xls";
  695. dlg.Filter = Application.Current.Resources["GlobalLableMsgRecipeExportExcelFilter"].ToString();
  696. var ret = dlg.ShowDialog();
  697. if (!ret.HasValue || !ret.Value)
  698. return;
  699. string fileName = dlg.FileName;
  700. this._viewModel.CurrentRecipeName = recipeName;
  701. string xmlRecipeData = _viewModel.LoadRecipe(recipeName);
  702. XmlDocument doc = new XmlDocument();
  703. doc.LoadXml(_viewModel.RecipeFormat);
  704. Task.Factory.StartNew(() =>
  705. {
  706. try
  707. {
  708. if (File.Exists(fileName))
  709. File.Delete(fileName);
  710. Workbook workbook = new Workbook();
  711. Worksheet worksheet = new Worksheet("First Sheet");
  712. var catalogNodes = doc.SelectNodes("/TableRecipeFormat/Catalog");
  713. int groupRowIndex = 0;
  714. int stepRowIndex = 0;
  715. int catalogRowIndex = 0;
  716. List<string> excelRowHeaderName = new List<string>();
  717. foreach (XmlElement cNode in catalogNodes)
  718. {
  719. XmlNodeList sNodes = cNode.SelectNodes("Group/Step");
  720. string catalogHeader = cNode.Attributes["DisplayName"].Value;
  721. worksheet.Cells[catalogRowIndex + 1, 0] = new Cell(catalogHeader);
  722. catalogRowIndex += sNodes.Count;
  723. XmlNodeList gNodes = cNode.SelectNodes("Group");
  724. foreach (XmlElement gNode in gNodes)
  725. {
  726. XmlNodeList stepNodes = gNode.SelectNodes("Step");
  727. string groupHeader = gNode.Attributes["DisplayName"].Value;
  728. worksheet.Cells[groupRowIndex + 1, 1] = new Cell(groupHeader);
  729. groupRowIndex += stepNodes.Count;
  730. foreach (XmlElement nd in stepNodes)
  731. {
  732. string stepHeader = nd.Attributes["DisplayName"].Value;
  733. worksheet.Cells[stepRowIndex + 1, 2] = new Cell(stepHeader);
  734. excelRowHeaderName.Add(nd.Attributes["ControlName"].Value);
  735. stepRowIndex++;
  736. }
  737. }
  738. }
  739. XmlDocument rcp = new XmlDocument();
  740. rcp.LoadXml(xmlRecipeData);
  741. XmlNodeList stepNodeList = rcp.SelectNodes("/TableRecipeData/Step");
  742. for (int stepNo = 0; stepNo < stepNodeList.Count; stepNo++)
  743. {
  744. var dataDic = new Dictionary<string, string>();
  745. foreach (XmlAttribute att in (stepNodeList[stepNo] as XmlElement).Attributes)
  746. {
  747. dataDic.Add(att.Name, att.Value);
  748. }
  749. //fill sub node's attributes
  750. foreach (XmlElement subStep in stepNodeList[stepNo].ChildNodes)
  751. {
  752. foreach (XmlAttribute att in subStep.Attributes)
  753. {
  754. dataDic.Add(att.Name, att.Value);
  755. }
  756. foreach (XmlElement subsubStep in subStep.ChildNodes)
  757. {
  758. foreach (XmlAttribute att2 in subsubStep.Attributes)
  759. {
  760. dataDic.Add(att2.Name, att2.Value);
  761. }
  762. }
  763. }
  764. long length = dataDic.LongCount();
  765. worksheet.Cells[0, 3] = new Cell(recipeName);
  766. for (int rowIndex = 0; rowIndex < excelRowHeaderName.Count; rowIndex++)
  767. {
  768. worksheet.Cells.ColumnWidth[(ushort)(rowIndex + 1)] = 4000;
  769. string cellValueString = dataDic[excelRowHeaderName[rowIndex]];
  770. double cellValue;
  771. if (double.TryParse(cellValueString, out cellValue))
  772. {
  773. worksheet.Cells[rowIndex + 1, stepNo + 3] = new Cell(cellValue);
  774. }
  775. else
  776. {
  777. worksheet.Cells[rowIndex + 1, stepNo + 3] = new Cell(cellValueString);
  778. }
  779. }
  780. }
  781. workbook.Worksheets.Add(worksheet);
  782. workbook.Save(fileName);
  783. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportExcelOk"].ToString(), recipeName, fileName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Information);
  784. }
  785. catch (Exception ex)
  786. {
  787. LOG.Write(ex);
  788. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportExcelFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  789. }
  790. });
  791. }
  792. private void TreeViewRcpList_MouseMove(object sender, MouseEventArgs e)
  793. {
  794. try
  795. {
  796. if (e.LeftButton == MouseButtonState.Pressed)
  797. {
  798. Point currentPosition = e.GetPosition(treeViewRcpList);
  799. OriginalItem = e.OriginalSource;
  800. if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 2.0) || (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 2.0))
  801. {
  802. if ((treeViewRcpList.SelectedItem as TreeViewItem) != null)
  803. {
  804. //DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeViewRcpList, treeViewRcpList.SelectedItem, DragDropEffects.Move);
  805. DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeViewRcpList.SelectedItem as TreeViewItem, sender, DragDropEffects.Move);
  806. }
  807. }
  808. }
  809. }
  810. catch (Exception ex)
  811. {
  812. LOG.Write(ex);
  813. }
  814. }
  815. private void TreeViewRcpList_MouseDown(object sender, MouseButtonEventArgs e)
  816. {
  817. if (e.ChangedButton == MouseButton.Left)
  818. {
  819. _lastMouseDown = e.GetPosition(treeViewRcpList);
  820. }
  821. }
  822. private void TreeViewRcpList_DragEnter(object sender, DragEventArgs e)
  823. {
  824. try
  825. {
  826. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) != null)
  827. {
  828. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) == GetAncestor<TreeViewFileItem>((DependencyObject)OriginalItem)
  829. || VisualTreeHelper.GetParent(GetAncestor<TreeViewFileItem>((DependencyObject)e.Source)) == VisualTreeHelper.GetParent(GetAncestor<TreeViewFileItem>((DependencyObject)OriginalItem)))
  830. e.Effects = DragDropEffects.None;
  831. }
  832. else if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  833. {
  834. if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) == GetAncestor<TreeViewFolderItem>((DependencyObject)OriginalItem))
  835. e.Effects = DragDropEffects.None;
  836. }
  837. else if (e.Source is TreeView)
  838. {
  839. e.Effects = DragDropEffects.None;
  840. }
  841. e.Handled = true;
  842. }
  843. catch (Exception ex)
  844. {
  845. LOG.Write(ex);
  846. }
  847. }
  848. private void TreeViewRcpList_Drop(object sender, DragEventArgs e)
  849. {
  850. try
  851. {
  852. TreeViewItem itemRemoved = treeViewRcpList.SelectedItem as TreeViewItem;
  853. if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) != null)
  854. {
  855. if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  856. {
  857. TreeViewFolderItem getTreeViewFolderItem = GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source);
  858. tragetFilePath = getTreeViewFolderItem.FolderName;
  859. GetAncestor<TreeView>(getTreeViewFolderItem);
  860. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath);
  861. }
  862. else
  863. {
  864. tragetFilePath = "";
  865. GetAncestor<TreeView>((DependencyObject)e.Source);
  866. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath);
  867. }
  868. }
  869. else if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
  870. {
  871. TreeViewFolderItem getTreeViewFolderItem = GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source);
  872. tragetFilePath = getTreeViewFolderItem.FolderName;
  873. GetAncestor<TreeView>(getTreeViewFolderItem);
  874. _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath);
  875. }
  876. UpdateRecipeFileList();
  877. SelectRecipe(tragetFilePath + "\\" + System.IO.Path.GetFileNameWithoutExtension(((TreeViewFileItem)itemRemoved).FileName));
  878. }
  879. catch (Exception ex)
  880. {
  881. LOG.Write(ex);
  882. }
  883. }
  884. public T GetAncestor<T>(DependencyObject reference) where T : DependencyObject
  885. {
  886. DependencyObject parent = VisualTreeHelper.GetParent(reference);
  887. while (!(parent is T) && parent != null)
  888. {
  889. parent = VisualTreeHelper.GetParent(parent);
  890. if (parent is TreeViewFolderItem)
  891. tragetFilePath = tragetFilePath.Insert(0, $"{((TreeViewFolderItem)parent).FolderName}\\");
  892. }
  893. if (parent != null)
  894. return (T)parent;
  895. else
  896. return null;
  897. }
  898. }
  899. }