SequenceViewModel.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Xml;
  8. using Caliburn.Micro;
  9. using Caliburn.Micro.Core;
  10. using OpenSEMI.ClientBase;
  11. using RecipeEditorLib.DGExtension.CustomColumn;
  12. using RecipeEditorLib.RecipeModel.Params;
  13. using MECF.Framework.Common.RecipeCenter;
  14. using VirgoUI.Client.Dialog;
  15. using MECF.Framework.Common.Equipment;
  16. using OpenSEMI.ClientBase.Command;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. namespace VirgoUI.Client.Models.Recipe.Sequence
  20. {
  21. public class SequenceViewModel : BaseModel
  22. {
  23. private int MenuPermission;
  24. public ObservableCollection<FileNode> Files { get; set; }
  25. protected override void OnInitialize()
  26. {
  27. base.OnInitialize();
  28. this.CurrentSequence = new SequenceData();
  29. this.Columns = this.columnBuilder.Build();
  30. this.editMode = EditMode.None;
  31. this.IsSavedDesc = true;
  32. List<string> names = this.provider.GetSequences();
  33. this.Files = new ObservableCollection<FileNode>(RecipeSequenceTreeBuilder.GetFiles(names));
  34. this.CurrentFileNode = this.Files[0];
  35. this.SelectDefault(this.CurrentFileNode);
  36. }
  37. protected override void OnActivate()
  38. {
  39. MenuPermission = ClientApp.Instance.GetPermission("Sequence");
  40. base.OnActivate();
  41. }
  42. protected override void OnDeactivate(bool close)
  43. {
  44. base.OnDeactivate(close);
  45. if (this.IsChanged)
  46. {
  47. if (MessageBox.Show("This sequence is changed,do you want to save it?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  48. this.SaveSequence();
  49. }
  50. }
  51. protected override void OnViewLoaded(object view)
  52. {
  53. base.OnViewLoaded(view);
  54. SequenceColumnBuilder.ApplyTemplate((UserControl)view, this.Columns);
  55. SequenceView u = (SequenceView)view;
  56. this.Columns.Apply((c) =>
  57. {
  58. c.Header = c;
  59. u.dgCustom.Columns.Add(c);
  60. });
  61. u.dgCustom.ItemsSource = CurrentSequence.Steps;
  62. this.UpdateView();
  63. }
  64. #region Sequence selection
  65. public void TreeSelectChanged(FileNode file)
  66. {
  67. if (file.IsFile)
  68. {
  69. if (this.IsChanged)
  70. {
  71. if (MessageBox.Show("This sequence is changed,do you want to save it?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  72. {
  73. this.Save(this.CurrentSequence);
  74. }
  75. }
  76. this.LoadData(file.FullPath);
  77. }
  78. else
  79. {
  80. this.ClearData();
  81. this.editMode = EditMode.None;
  82. }
  83. this.UpdateView();
  84. this.CurrentFileNode = file;
  85. }
  86. private TreeViewItem GetParentObjectEx<TreeViewItem>(DependencyObject obj) where TreeViewItem : FrameworkElement
  87. {
  88. DependencyObject parent = VisualTreeHelper.GetParent(obj);
  89. while (parent != null)
  90. {
  91. if (parent is TreeViewItem)
  92. {
  93. return (TreeViewItem)parent;
  94. }
  95. parent = VisualTreeHelper.GetParent(parent);
  96. }
  97. return null;
  98. }
  99. public void TreeRightMouseDown(MouseButtonEventArgs e)
  100. {
  101. var item = GetParentObjectEx<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
  102. if (item != null)
  103. {
  104. item.Focus();
  105. }
  106. }
  107. #endregion
  108. private ICommand _NotImplementCommand;
  109. public ICommand NotImplementCommand
  110. {
  111. get
  112. {
  113. if (this._NotImplementCommand == null)
  114. this._NotImplementCommand = new BaseCommand(() => this.NotImplement());
  115. return this._NotImplementCommand;
  116. }
  117. }
  118. public void NotImplement()
  119. {
  120. MessageBox.Show("Not Implement");
  121. }
  122. #region Sequence operation
  123. private ICommand _NewFolderCommand;
  124. public ICommand NewFolderCommand
  125. {
  126. get
  127. {
  128. if (this._NewFolderCommand == null)
  129. this._NewFolderCommand = new BaseCommand(() => this.NewFolder());
  130. return this._NewFolderCommand;
  131. }
  132. }
  133. public void NewFolder()
  134. {
  135. if (!this.CurrentFileNode.IsFile)
  136. {
  137. InputFileNameDialogViewModel dialog = new InputFileNameDialogViewModel("Input Folder Name");
  138. WindowManager wm = new WindowManager();
  139. bool? bret = wm.ShowDialog(dialog);
  140. if (!(bool)bret)
  141. return;
  142. string fullpath = (this.CurrentFileNode.FullPath == "" ? dialog.DialogResult : this.CurrentFileNode.FullPath + "\\" + dialog.DialogResult);
  143. if (this.provider.CreateSequenceFolder(fullpath))
  144. {
  145. FileNode folder = new FileNode();
  146. folder.Name = dialog.DialogResult;
  147. folder.FullPath = fullpath;
  148. folder.Parent = this.CurrentFileNode;
  149. this.CurrentFileNode.Files.Add(folder);
  150. }
  151. else
  152. MessageBox.Show("Create folder failed!");
  153. }
  154. }
  155. private ICommand _NewFolderInParentCommand;
  156. public ICommand NewFolderInParentCommand
  157. {
  158. get
  159. {
  160. if (this._NewFolderInParentCommand == null)
  161. this._NewFolderInParentCommand = new BaseCommand(() => this.NewFolderInParent());
  162. return this._NewFolderInParentCommand;
  163. }
  164. }
  165. public void NewFolderInParent()
  166. {
  167. InputFileNameDialogViewModel dialog = new InputFileNameDialogViewModel("Input Folder Name");
  168. WindowManager wm = new WindowManager();
  169. bool? bret = wm.ShowDialog(dialog);
  170. if (!(bool)bret)
  171. return;
  172. string fullpath = dialog.DialogResult;
  173. if (this.provider.CreateSequenceFolder(fullpath))
  174. {
  175. FileNode folder = new FileNode();
  176. folder.Name = dialog.DialogResult;
  177. folder.FullPath = fullpath;
  178. folder.Parent = this.Files[0];
  179. this.Files[0].Files.Add(folder);
  180. }
  181. else
  182. MessageBox.Show("Create folder failed!");
  183. }
  184. private ICommand _NewSequenceInParentCommand;
  185. public ICommand NewSequenceInParentCommand
  186. {
  187. get
  188. {
  189. if (this._NewSequenceInParentCommand == null)
  190. this._NewSequenceInParentCommand = new BaseCommand(() => this.NewSequenceInParent());
  191. return this._NewSequenceInParentCommand;
  192. }
  193. }
  194. public void NewSequenceInParent()
  195. {
  196. InputFileNameDialogViewModel dialog = new InputFileNameDialogViewModel("Input New Sequence Name");
  197. WindowManager wm = new WindowManager();
  198. bool? bret = wm.ShowDialog(dialog);
  199. if ((bool)bret)
  200. {
  201. if (this.IsChanged)
  202. {
  203. if (MessageBox.Show("This sequence is changed,do you want to save it?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  204. {
  205. this.CurrentSequence.Revisor = BaseApp.Instance.UserContext.LoginName;
  206. this.CurrentSequence.ReviseTime = DateTime.Now;
  207. this.Save(this.CurrentSequence);
  208. }
  209. }
  210. else
  211. {
  212. string fullpath = dialog.DialogResult;
  213. if (this.IsExist(fullpath))
  214. {
  215. MessageBox.Show("Sequence already existed!");
  216. }
  217. else
  218. {
  219. SequenceData sequence = new SequenceData();
  220. sequence.Name = fullpath;
  221. sequence.Creator = BaseApp.Instance.UserContext.LoginName;
  222. sequence.CreateTime = DateTime.Now;
  223. sequence.Revisor = BaseApp.Instance.UserContext.LoginName;
  224. sequence.ReviseTime = DateTime.Now;
  225. sequence.Description = string.Empty;
  226. if (this.Save(sequence))
  227. {
  228. this.CurrentSequence.Name = sequence.Name;
  229. this.CurrentSequence.Creator = sequence.Creator;
  230. this.CurrentSequence.CreateTime = sequence.CreateTime;
  231. this.CurrentSequence.Revisor = sequence.Revisor;
  232. this.CurrentSequence.ReviseTime = sequence.ReviseTime;
  233. this.CurrentSequence.Description = sequence.Description;
  234. this.CurrentSequence.Steps.Clear();
  235. FileNode file = new FileNode();
  236. file.Name = dialog.DialogResult;
  237. file.FullPath = this.CurrentSequence.Name;
  238. file.IsFile = true;
  239. file.Parent = this.Files[0];
  240. this.Files[0].Files.Insert(this.findInsertPosition(this.Files[0].Files), file);
  241. this.editMode = EditMode.Normal;
  242. this.UpdateView();
  243. }
  244. }
  245. }
  246. }
  247. }
  248. public int findInsertPosition(ObservableCollection<FileNode> files)
  249. {
  250. int pos = -1;
  251. if (files.Count == 0)
  252. pos = 0;
  253. else
  254. {
  255. bool foundfolder = false;
  256. for (var index = 0; index < files.Count; index++)
  257. {
  258. if (!files[index].IsFile)
  259. {
  260. foundfolder = true;
  261. pos = index;
  262. break;
  263. }
  264. }
  265. if (!foundfolder)
  266. pos = files.Count;
  267. }
  268. return pos;
  269. }
  270. private ICommand _SaveAsCommand;
  271. public ICommand SaveAsCommand
  272. {
  273. get
  274. {
  275. if (this._SaveAsCommand == null)
  276. this._SaveAsCommand = new BaseCommand(() => this.SaveAsSequence());
  277. return this._SaveAsCommand;
  278. }
  279. }
  280. public void SaveAsSequence()
  281. {
  282. if (this.CurrentFileNode.IsFile)
  283. {
  284. InputFileNameDialogViewModel dialog = new InputFileNameDialogViewModel("SaveAs Sequence");
  285. WindowManager wm = new WindowManager();
  286. dialog.FileName = this.CurrentFileNode.Name;
  287. bool? bret = wm.ShowDialog(dialog);
  288. if (!(bool)bret)
  289. return;
  290. string fullpath = (this.CurrentFileNode.Parent.FullPath == "" ? dialog.DialogResult : this.CurrentFileNode.Parent.FullPath + "\\" + dialog.DialogResult);
  291. if (this.IsExist(fullpath))
  292. {
  293. MessageBox.Show("Sequence already existed!");
  294. }
  295. else
  296. {
  297. if (this.IsChanged)
  298. {
  299. if (MessageBox.Show("This sequence is changed,do you want to save it?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  300. {
  301. this.Save(this.CurrentSequence);
  302. }
  303. }
  304. this.CurrentSequence.Revisor = BaseApp.Instance.UserContext.LoginName;
  305. this.CurrentSequence.ReviseTime = DateTime.Now;
  306. string tempname = this.CurrentSequence.Name;
  307. this.CurrentSequence.Name = fullpath;
  308. if (this.provider.SaveAs(fullpath, this.CurrentSequence))
  309. {
  310. FileNode node = new FileNode();
  311. node.Parent = this.CurrentFileNode.Parent;
  312. node.Name = dialog.DialogResult;
  313. node.FullPath = fullpath;
  314. node.IsFile = true;
  315. this.CurrentFileNode.Parent.Files.Add(node);
  316. }
  317. else
  318. {
  319. this.CurrentSequence.Name = tempname;
  320. MessageBox.Show("SaveAs failed!");
  321. }
  322. }
  323. }
  324. }
  325. private ICommand _DeleteFolderCommand;
  326. public ICommand DeleteFolderCommand
  327. {
  328. get
  329. {
  330. if (this._DeleteFolderCommand == null)
  331. this._DeleteFolderCommand = new BaseCommand(() => this.DeleteFolder());
  332. return this._DeleteFolderCommand;
  333. }
  334. }
  335. public void DeleteFolder()
  336. {
  337. if (!this.CurrentFileNode.IsFile && this.CurrentFileNode.FullPath != "")
  338. {
  339. if (DialogBox.Confirm("Do you want to delete this folder? this operation will delete all files in this folder."))
  340. {
  341. if (this.provider.DeleteSequenceFolder(this.CurrentFileNode.FullPath))
  342. {
  343. this.CurrentFileNode.Parent.Files.Remove(this.CurrentFileNode);
  344. }
  345. else
  346. DialogBox.ShowInfo("Delete folder failed!");
  347. }
  348. }
  349. }
  350. private ICommand _NewSequenceCommand;
  351. public ICommand NewSequenceCommand
  352. {
  353. get
  354. {
  355. if (this._NewSequenceCommand == null)
  356. this._NewSequenceCommand = new BaseCommand(() => this.NewSequence());
  357. return this._NewSequenceCommand;
  358. }
  359. }
  360. public void NewSequence()
  361. {
  362. if (MenuPermission != 3) return;
  363. InputFileNameDialogViewModel dialog = new InputFileNameDialogViewModel("Input New Sequence Name");
  364. WindowManager wm = new WindowManager();
  365. bool? bret = wm.ShowDialog(dialog);
  366. if ((bool)bret)
  367. {
  368. if (this.IsChanged)
  369. {
  370. if (MessageBox.Show("This sequence is changed,do you want to save it?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  371. {
  372. this.CurrentSequence.Revisor = BaseApp.Instance.UserContext.LoginName;
  373. this.CurrentSequence.ReviseTime = DateTime.Now;
  374. this.Save(this.CurrentSequence);
  375. }
  376. }
  377. else
  378. {
  379. string fullpath = "";
  380. if (this.CurrentFileNode.IsFile)
  381. fullpath = (this.CurrentFileNode.Parent.FullPath == "" ? dialog.DialogResult : this.CurrentFileNode.Parent.FullPath + "\\" + dialog.DialogResult);
  382. else
  383. fullpath = (this.CurrentFileNode.FullPath == "" ? dialog.DialogResult : this.CurrentFileNode.FullPath + "\\" + dialog.DialogResult);
  384. if (this.IsExist(fullpath))
  385. {
  386. MessageBox.Show("Sequence already existed!");
  387. }
  388. else
  389. {
  390. SequenceData sequence = new SequenceData();
  391. sequence.Name = fullpath;
  392. sequence.Creator = BaseApp.Instance.UserContext.LoginName;
  393. sequence.CreateTime = DateTime.Now;
  394. sequence.Revisor = BaseApp.Instance.UserContext.LoginName;
  395. sequence.ReviseTime = DateTime.Now;
  396. sequence.Description = string.Empty;
  397. if (this.Save(sequence))
  398. {
  399. this.CurrentSequence.Name = sequence.Name;
  400. this.CurrentSequence.Creator = sequence.Creator;
  401. this.CurrentSequence.CreateTime = sequence.CreateTime;
  402. this.CurrentSequence.Revisor = sequence.Revisor;
  403. this.CurrentSequence.ReviseTime = sequence.ReviseTime;
  404. this.CurrentSequence.Description = sequence.Description;
  405. this.CurrentSequence.Steps.Clear();
  406. FileNode file = new FileNode();
  407. file.Name = dialog.DialogResult;
  408. file.FullPath = this.CurrentSequence.Name;
  409. file.IsFile = true;
  410. file.Parent = this.CurrentFileNode.IsFile ? this.CurrentFileNode.Parent : this.CurrentFileNode;
  411. if (this.CurrentFileNode.IsFile)
  412. this.CurrentFileNode.Parent.Files.Insert(this.findInsertPosition(this.CurrentFileNode.Parent.Files), file);
  413. else
  414. this.CurrentFileNode.Files.Insert(this.findInsertPosition(this.CurrentFileNode.Files), file);
  415. this.editMode = EditMode.Normal;
  416. this.UpdateView();
  417. }
  418. }
  419. }
  420. }
  421. }
  422. private ICommand _RenameCommand;
  423. public ICommand RenameCommand
  424. {
  425. get
  426. {
  427. if (this._RenameCommand == null)
  428. this._RenameCommand = new BaseCommand(() => this.RenameSequence());
  429. return this._RenameCommand;
  430. }
  431. }
  432. public void RenameSequence()
  433. {
  434. if (MenuPermission != 3) return;
  435. if (this.CurrentFileNode.IsFile)
  436. {
  437. InputFileNameDialogViewModel dialog = new InputFileNameDialogViewModel("Rename Sequence");
  438. WindowManager wm = new WindowManager();
  439. dialog.FileName = this.CurrentFileNode.Name;
  440. bool? bret = wm.ShowDialog(dialog);
  441. if (!(bool)bret)
  442. return;
  443. string fullpath = this.CurrentFileNode.Parent.FullPath == "" ? dialog.DialogResult : this.CurrentFileNode.Parent.FullPath + "\\" + dialog.DialogResult;
  444. if (this.IsExist(fullpath))
  445. {
  446. MessageBox.Show("Sequence already existed!");
  447. }
  448. else
  449. {
  450. if (this.IsChanged)
  451. {
  452. if (MessageBox.Show("This sequence is changed,do you want to save it?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  453. {
  454. this.Save(this.CurrentSequence);
  455. }
  456. }
  457. this.CurrentSequence.Revisor = BaseApp.Instance.UserContext.LoginName;
  458. this.CurrentSequence.ReviseTime = DateTime.Now;
  459. if (this.provider.Rename(this.CurrentSequence.Name, fullpath))
  460. {
  461. this.CurrentFileNode.Name = dialog.DialogResult;
  462. this.CurrentFileNode.FullPath = fullpath;
  463. this.CurrentSequence.Name = fullpath;
  464. }
  465. else
  466. MessageBox.Show("Rename failed!");
  467. }
  468. }
  469. }
  470. public void SaveSequence()
  471. {
  472. if (this.IsChanged)
  473. {
  474. this.Save(this.CurrentSequence);
  475. }
  476. }
  477. private ICommand _DeleteCommand;
  478. public ICommand DeleteSequenceCommand
  479. {
  480. get
  481. {
  482. if (this._DeleteCommand == null)
  483. this._DeleteCommand = new BaseCommand(() => this.DeleteSequence());
  484. return this._DeleteCommand;
  485. }
  486. }
  487. public void DeleteSequence()
  488. {
  489. if (MenuPermission != 3) return;
  490. if (this.CurrentFileNode.IsFile)
  491. {
  492. if (DialogBox.Confirm("Do you want to delete this sequence?"))
  493. {
  494. if (this.provider.Delete(this.CurrentSequence.Name))
  495. {
  496. this.CurrentFileNode.Parent.Files.Remove(this.CurrentFileNode);
  497. }
  498. }
  499. }
  500. }
  501. public void ReloadSequence()
  502. {
  503. if (MenuPermission != 3) return;
  504. if (this.editMode == EditMode.Normal || this.editMode == EditMode.Edit)
  505. {
  506. this.LoadData(this.CurrentSequence.Name);
  507. this.UpdateView();
  508. }
  509. }
  510. private bool Save(SequenceData seq)
  511. {
  512. if (MenuPermission != 3) return false;
  513. bool result = false;
  514. if (string.IsNullOrEmpty(seq.Name))
  515. {
  516. MessageBox.Show("Sequence name can't be empty");
  517. return false;
  518. }
  519. string ruleCheckMessage = null;
  520. if (!ValidateSequenceRules(seq, ref ruleCheckMessage))
  521. {
  522. MessageBox.Show(string.Format($"{seq.Name} rules check failed, don't allow to save: {ruleCheckMessage}"));
  523. return false;
  524. }
  525. seq.Revisor = BaseApp.Instance.UserContext.LoginName;
  526. seq.ReviseTime = DateTime.Now;
  527. result = this.provider.Save(seq);
  528. if (result)
  529. {
  530. foreach (ObservableCollection<Param> parameters in seq.Steps)
  531. {
  532. parameters.Apply(param => param.IsSaved = true);
  533. }
  534. this.editMode = EditMode.Normal;
  535. this.IsSavedDesc = true;
  536. this.NotifyOfPropertyChange("IsSavedDesc");
  537. this.UpdateView();
  538. }
  539. else
  540. MessageBox.Show("Save failed!");
  541. return result;
  542. }
  543. private bool IsExist(string sequencename)
  544. {
  545. bool existed = false;
  546. FileNode filenode = this.CurrentFileNode.IsFile ? this.CurrentFileNode.Parent : this.CurrentFileNode;
  547. for (var index = 0; index < filenode.Files.Count; index++)
  548. {
  549. if (filenode.Files[index].FullPath.ToLower() == sequencename.ToLower())
  550. {
  551. existed = true;
  552. break;
  553. }
  554. }
  555. return existed;
  556. }
  557. private bool ValidateSequenceRules(SequenceData currentSequence, ref string ruleCheckMessage)
  558. {
  559. return true;
  560. }
  561. #endregion
  562. #region Steps operation
  563. public void AddStep()
  564. {
  565. if (MenuPermission != 3) return;
  566. this.CurrentSequence.Steps.Add(CurrentSequence.CreateStep(this.Columns));
  567. if (this.editMode != EditMode.New && this.editMode != EditMode.ReName)
  568. this.editMode = EditMode.Edit;
  569. int index = 1;
  570. foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)
  571. {
  572. (parameters[0] as StepParam).Value = index.ToString();
  573. index++;
  574. }
  575. this.UpdateView();
  576. }
  577. public void AppendStep()
  578. {
  579. if (MenuPermission != 3) return;
  580. int index = -1;
  581. bool found = false;
  582. for (var i = 0; i < this.CurrentSequence.Steps.Count; i++)
  583. {
  584. if (this.CurrentSequence.Steps[i][0] is StepParam && ((StepParam)this.CurrentSequence.Steps[i][0]).Checked)
  585. {
  586. index = i;
  587. found = true;
  588. break;
  589. }
  590. }
  591. if (found)
  592. {
  593. if (this.editMode != EditMode.New && this.editMode != EditMode.ReName)
  594. this.editMode = EditMode.Edit;
  595. this.CurrentSequence.Steps.Insert(index, this.CurrentSequence.CreateStep(this.Columns));
  596. index = 1;
  597. foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)
  598. {
  599. (parameters[0] as StepParam).Value = index.ToString();
  600. index++;
  601. }
  602. }
  603. }
  604. private ObservableCollection<ObservableCollection<Param>> copySteps = new ObservableCollection<ObservableCollection<Param>>();
  605. public void CopyStep()
  606. {
  607. if (MenuPermission != 3) return;
  608. this.copySteps.Clear();
  609. for (var i = 0; i < this.CurrentSequence.Steps.Count; i++)
  610. {
  611. if (this.CurrentSequence.Steps[i][0] is StepParam && ((StepParam)this.CurrentSequence.Steps[i][0]).Checked)
  612. {
  613. this.copySteps.Add(this.CurrentSequence.CloneStep(this.Columns, this.CurrentSequence.Steps[i]));
  614. }
  615. }
  616. }
  617. public void PasteStep()
  618. {
  619. if (MenuPermission != 3) return;
  620. if (this.copySteps.Count > 0)
  621. {
  622. if (this.editMode != EditMode.New && this.editMode != EditMode.ReName)
  623. this.editMode = EditMode.Edit;
  624. for (var i = 0; i < this.CurrentSequence.Steps.Count; i++)
  625. {
  626. if (this.CurrentSequence.Steps[i][0] is StepParam && ((StepParam)this.CurrentSequence.Steps[i][0]).Checked)
  627. {
  628. for (var copyindex = 0; copyindex < this.copySteps.Count; copyindex++)
  629. {
  630. this.CurrentSequence.Steps.Insert(i, this.CurrentSequence.CloneStep(this.Columns, this.copySteps[copyindex]));
  631. i++;
  632. }
  633. break;
  634. }
  635. }
  636. int index = 1;
  637. foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)
  638. {
  639. (parameters[0] as StepParam).Value = index.ToString();
  640. index++;
  641. }
  642. this.UpdateView();
  643. }
  644. }
  645. public void DeleteStep()
  646. {
  647. if (MenuPermission != 3) return;
  648. if (this.editMode != EditMode.New && this.editMode != EditMode.ReName)
  649. this.editMode = EditMode.Edit;
  650. List<ObservableCollection<Param>> steps = this.CurrentSequence.Steps.ToList();
  651. for (var i = 0; i < steps.Count; i++)
  652. {
  653. if (steps[i][0] is StepParam && ((StepParam)steps[i][0]).Checked)
  654. {
  655. this.CurrentSequence.Steps.Remove(steps[i]);
  656. }
  657. }
  658. int index = 1;
  659. foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)
  660. {
  661. (parameters[0] as StepParam).Value = index.ToString();
  662. index++;
  663. }
  664. }
  665. public void SelectRecipe(PathFileParam param)
  666. {
  667. RecipeSequenceSelectDialogViewModel dialog = new RecipeSequenceSelectDialogViewModel();
  668. dialog.DisplayName = "Select Recipe";
  669. ObservableCollection<Param> parameters = param.Parent;
  670. PositionParam posParam = null;
  671. for (var index = 0; index < parameters.Count; index++)
  672. {
  673. if (parameters[index] is PositionParam)
  674. {
  675. posParam = parameters[index] as PositionParam;
  676. break;
  677. }
  678. }
  679. string module = ModuleName.System.ToString();
  680. List<string> lstFiles;
  681. if (Enum.TryParse<ModuleName>(posParam.Value, out ModuleName mod))
  682. module = posParam.Value;
  683. if (string.IsNullOrEmpty(param.Path))
  684. {
  685. lstFiles = RecipeClient.Instance.Service.GetRecipesByPath($"{module}", false).ToList();
  686. }
  687. else
  688. {
  689. lstFiles = RecipeClient.Instance.Service.GetRecipesByPath($"{param.Path}", false).ToList();
  690. }
  691. dialog.Files = new ObservableCollection<FileNode>(RecipeSequenceTreeBuilder.GetFiles(lstFiles));
  692. WindowManager wm = new WindowManager();
  693. bool? bret = wm.ShowDialog(dialog);
  694. if ((bool)bret)
  695. {
  696. param.Value = dialog.DialogResult;
  697. param.IsSaved = false;
  698. }
  699. }
  700. #endregion
  701. private void SelectDefault(FileNode node)
  702. {
  703. if (!node.IsFile)
  704. {
  705. if (node.Files.Count > 0)
  706. {
  707. foreach (FileNode file in node.Files)
  708. {
  709. if (file.IsFile)
  710. {
  711. this.TreeSelectChanged(file);
  712. break;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. private void LoadData(string newSeqName)
  719. {
  720. SequenceData Sequence = this.provider.GetSequenceByName(this.Columns, newSeqName);
  721. this.CurrentSequence.Name = Sequence.Name;
  722. this.CurrentSequence.Creator = Sequence.Creator;
  723. this.CurrentSequence.CreateTime = Sequence.CreateTime;
  724. this.CurrentSequence.Revisor = Sequence.Revisor;
  725. this.CurrentSequence.ReviseTime = Sequence.ReviseTime;
  726. this.CurrentSequence.Description = Sequence.Description;
  727. this.CurrentSequence.Steps.Clear();
  728. Sequence.Steps.ToList().ForEach(step =>
  729. this.CurrentSequence.Steps.Add(step));
  730. int index = 1;
  731. foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)
  732. {
  733. (parameters[0] as StepParam).Value = index.ToString();
  734. index++;
  735. }
  736. this.IsSavedDesc = true;
  737. this.NotifyOfPropertyChange("IsSavedDesc");
  738. this.editMode = EditMode.Normal;
  739. }
  740. private void ClearData()
  741. {
  742. this.editMode = EditMode.None;
  743. this.CurrentSequence.Steps.Clear();
  744. this.CurrentSequence.Name = string.Empty;
  745. this.CurrentSequence.Description = string.Empty;
  746. this.IsSavedDesc = true;
  747. this.NotifyOfPropertyChange("IsSavedGroup");
  748. this.NotifyOfPropertyChange("IsSavedDesc");
  749. }
  750. public void UpdateView()
  751. {
  752. this.EnableSequenceName = false;
  753. this.NotifyOfPropertyChange("EnableSequenceName");
  754. this.EnableNew = true;
  755. this.EnableReName = true;
  756. this.EnableCopy = true;
  757. this.EnableDelete = true;
  758. this.EnableSave = true;
  759. this.EnableStep = true;
  760. this.NotifyOfPropertyChange("EnableNew");
  761. this.NotifyOfPropertyChange("EnableReName");
  762. this.NotifyOfPropertyChange("EnableCopy");
  763. this.NotifyOfPropertyChange("EnableDelete");
  764. this.NotifyOfPropertyChange("EnableSave");
  765. this.NotifyOfPropertyChange("EnableStep");
  766. if (this.editMode == EditMode.None)
  767. {
  768. this.EnableNew = true;
  769. this.EnableReName = false;
  770. this.EnableCopy = false;
  771. this.EnableDelete = false;
  772. this.EnableStep = false;
  773. this.EnableSave = false;
  774. this.NotifyOfPropertyChange("EnableNew");
  775. this.NotifyOfPropertyChange("EnableReName");
  776. this.NotifyOfPropertyChange("EnableCopy");
  777. this.NotifyOfPropertyChange("EnableDelete");
  778. this.NotifyOfPropertyChange("EnableSave");
  779. this.NotifyOfPropertyChange("EnableStep");
  780. }
  781. }
  782. private bool IsChanged
  783. {
  784. get
  785. {
  786. bool changed = false;
  787. if (!this.IsSavedDesc || this.editMode == EditMode.Edit)
  788. changed = true;
  789. else
  790. {
  791. foreach (ObservableCollection<Param> parameters in this.CurrentSequence.Steps)
  792. {
  793. if (parameters.Where(param => param.IsSaved == false).Count() > 0)
  794. {
  795. changed = true;
  796. break;
  797. }
  798. }
  799. }
  800. return changed;
  801. }
  802. }
  803. public bool EnableNew { get; set; }
  804. public bool EnableReName { get; set; }
  805. public bool EnableCopy { get; set; }
  806. public bool EnableDelete { get; set; }
  807. public bool EnableSave { get; set; }
  808. public bool EnableStep { get; set; }
  809. public bool IsSavedDesc { get; set; }
  810. public FileNode CurrentFileNode { get; private set; }
  811. public SequenceData CurrentSequence { get; private set; }
  812. public ObservableCollection<ColumnBase> Columns { get; set; }
  813. public bool EnableSequenceName { get; set; }
  814. private string selectedSequenceName = string.Empty;
  815. private string SequenceNameBeforeRename = string.Empty;
  816. private string lastSequenceName = string.Empty;
  817. private SequenceColumnBuilder columnBuilder = new SequenceColumnBuilder();
  818. private EditMode editMode;
  819. private SequenceDataProvider provider = new SequenceDataProvider();
  820. }
  821. }