SequenceViewModel.cs 36 KB

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