using Aitex.Core.RT.Log; using Aitex.Core.UI.View.Common; using Aitex.UI.RecipeEditor; using Aitex.UI.RecipeEditor.View; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Equipment; using Microsoft.VisualBasic; using Microsoft.Win32; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Xml; using Venus_Core; using Venus_MainPages.PMs; using Venus_MainPages.Views; using WPF.Themes.UserControls; namespace Venus_MainPages.ViewModels { internal class RecipeViewModel : BindableBase { #region 私有字段 private string m_Title; public string ModuleName = "PMA"; private string m_CurrentRecipeName; private UiRecipeManager m_uiRecipeManager = new UiRecipeManager(); private RecipeView recipeView; private TreeView treeViewRcpList; private RecipeEditorControl tableRecipeGrid; private Recipe m_currentRecipe; private string m_recipeType; private bool firstLoad=true; #endregion #region 属性 public string Title { get { return m_Title; } set { SetProperty(ref m_Title, value); } } public string CurrentRecipeName { get { return m_CurrentRecipeName; } set { SetProperty(ref m_CurrentRecipeName, value); } } public Recipe CurrentRecipe { get { return m_currentRecipe; } set { m_currentRecipe = value; RecipeType = m_currentRecipe.Header.Type.ToString(); } } public string RecipeType { get { return m_recipeType; } set { SetProperty(ref m_recipeType, value); } } #endregion #region 命令 private DelegateCommand _NewCommand; public DelegateCommand NewCommand => _NewCommand ?? (_NewCommand = new DelegateCommand(OnNew)); private DelegateCommand _ReNameCommand; public DelegateCommand ReNameCommand => _ReNameCommand ?? (_ReNameCommand = new DelegateCommand(OnReName)); private DelegateCommand _DeleteCommand; public DelegateCommand DeleteCommand => _DeleteCommand ?? (_DeleteCommand = new DelegateCommand(OnDelete)); private DelegateCommand _MouseRightButtonDownCommand; public DelegateCommand MouseRightButtonDownCommand => _MouseRightButtonDownCommand ?? (_MouseRightButtonDownCommand = new DelegateCommand(OnMouseRightButtonDown)); private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(OnLoaded)); private DelegateCommand _SaveRecipeCommand; public DelegateCommand SaveRecipeCommand => _SaveRecipeCommand ?? (_SaveRecipeCommand = new DelegateCommand(OnSaveRecipe)); private DelegateCommand _AddStepCommand; public DelegateCommand AddStepCommand => _AddStepCommand ?? (_AddStepCommand = new DelegateCommand(OnAddStep)); private DelegateCommand _DeleteStepCommand; public DelegateCommand DeleteStepCommand => _DeleteStepCommand ?? (_DeleteStepCommand = new DelegateCommand(OnDeleteStep)); //private DelegateCommand _ReLoadCommand; //public DelegateCommand ReLoadCommand => // _ReLoadCommand ?? (_ReLoadCommand = new DelegateCommand(OnReLoad)); #endregion #region 构造函数 public RecipeViewModel() { Title = "配方管理"; } #endregion #region 命令方法 private void OnNew() { } private void OnReName() { } private void OnDelete() { } private void OnAddStep() { this.tableRecipeGrid.ControlViewModel.OnAddStep(CurrentRecipe.Steps.Count+1); } private void OnDeleteStep() { this.tableRecipeGrid.ControlViewModel.OnDeleteStep(CurrentRecipe.Steps.Count-1); } private void OnReLoad() { this.tableRecipeGrid.ControlViewModel.LoadRecipe(CurrentRecipe); } private void OnSaveRecipe() { SaveRecipe(CurrentRecipeName, RecipeUnity.RecipeToString(CurrentRecipe)); } private void OnLoaded(Object myrecipeView) { if (firstLoad == true) { firstLoad = false; recipeView = myrecipeView as RecipeView; treeViewRcpList = recipeView.treeViewRcpList; tableRecipeGrid = recipeView.tableRecipeGrid; UpdateRecipeFileList(); treeViewRcpList.SelectedItemChanged += TreeViewRcpList_SelectedItemChanged; } } TreeViewFileItem selectedItem; private void TreeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) { selectedItem = e.NewValue as TreeViewFileItem; if (selectedItem == null) return; try { CurrentRecipeName = selectedItem.FileName; string xmlRecipeData = m_uiRecipeManager.LoadRecipe(ModuleName, selectedItem.FileName); if (xmlRecipeData == "") { treeViewRcpList.Items.Remove(selectedItem); return; } CurrentRecipe = Recipe.Load(xmlRecipeData); this.tableRecipeGrid.ControlViewModel.LoadRecipe(CurrentRecipe); } catch (Exception ex) { MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } e.Handled = true; } /// /// Load recipe data by recipe name /// /// /// public string LoadRecipe(string receipeName) { return m_uiRecipeManager.LoadRecipe(ModuleName, receipeName); } /// /// Save recipe by recipe name and recipe data /// /// /// /// public bool SaveRecipe(string recipeName, string recipeContent) { return m_uiRecipeManager.SaveRecipe(ModuleName, recipeName, recipeContent); } /// /// 右击TreeView,上下文菜单选择功能 /// /// private void OnMouseRightButtonDown(Object treeView) { //treeViewRcpList = treeView as TreeView; treeViewRcpList.ContextMenu = new ContextMenu() { Background = new SolidColorBrush(Colors.AliceBlue)}; MenuItem menuItem = new MenuItem(); menuItem = new MenuItem(); menuItem.Tag = "\\"; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe); menuItem.Header = "New Recipe"; treeViewRcpList.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = "\\"; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteRecipe); menuItem.Header = "Delete Recipe"; treeViewRcpList.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = "\\"; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_SaveAsRecipe); menuItem.Header = "Save As Recipe"; treeViewRcpList.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = "\\"; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameRecipe); menuItem.Header = "Rename"; treeViewRcpList.ContextMenu.Items.Add(menuItem); treeViewRcpList.ContextMenu.Visibility = Visibility.Visible; } /// /// 创建新的配方 /// /// /// private void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e) { MenuItem mit = sender as MenuItem; string folderName = mit.Tag as string; PerformCreateRecipe(folderName); } private void PerformCreateRecipe(string folderName) { try { RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString()) { Owner = Application.Current.MainWindow }; if (dlg.ShowDialog() == true) { var recipeName = folderName + "\\" + dlg.InputText; RecipeType type = (RecipeType)Enum.Parse(typeof(RecipeType), dlg.SelectedType); string newRecipe =RecipeUnity.CreateRecipe(type, dlg.InputText); //string recipeContent = m_uiRecipeManager.GetRecipeTemplate(ModuleName); //m_uiRecipeManager.SaveAsRecipe(ModuleName, recipeName, m_uiRecipeManager.LoadRecipe("system",folderName)); if (SaveAsRecipe(recipeName, newRecipe)) { //UpdateRecipeFileList(); //SelectRecipe(recipeName); treeViewRcpList.Items.Add(new TreeViewFileItem(dlg.InputText)); } else { WPFMessageBox.ShowError("Error"); } } } catch (Exception ex) { LOG.WriteExeption(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// 删除配方 /// /// /// private void menuItem_MouseClick_DeleteRecipe(object sender, RoutedEventArgs e) { if (CurrentRecipe == null) { return; } if (WPFMessageBox.ShowQuestion($"Delete {CurrentRecipeName}?","删除后无法恢复!!!") == MessageBoxResult.Yes) { MenuItem mit = sender as MenuItem; //string recipename = mit.Header.ToString(); m_uiRecipeManager.DeleteRecipe(ModuleName, CurrentRecipeName); //PerformCreateRecipe(folderName); treeViewRcpList.Items.Remove(selectedItem); } } /// /// 另存为配方 /// /// /// private void menuItem_MouseClick_SaveAsRecipe(object sender, RoutedEventArgs e) { if (CurrentRecipe == null) { return; } var dlg = new SaveFileDialog() { Title = "Save As", DefaultExt = "rcp", Filter = "rcp files (*.rcp)|*.rcp", }; if (dlg.ShowDialog() == true) { var newRecipe = CurrentRecipe; newRecipe.Header.Name = Path.GetFileNameWithoutExtension( dlg.FileName); newRecipe.Header.CreateTime = DateTime.Now.ToString(); File.WriteAllText(dlg.FileName, RecipeUnity.RecipeToString(newRecipe)); } UpdateRecipeFileList(); } private void menuItem_MouseClick_RenameRecipe(object sender, RoutedEventArgs e) { if (CurrentRecipe == null) { return; } var newName= Interaction.InputBox("Rename Recipe", "", CurrentRecipeName, -1, -1); if (newName != CurrentRecipeName && newName!="") { var oldrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, CurrentRecipeName + ".rcp"); var newrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, newName + ".rcp"); CurrentRecipe.Header.Name = newName; SaveRecipe(CurrentRecipeName, RecipeUnity.RecipeToString(CurrentRecipe)); File.Move(oldrecipePath, newrecipePath); UpdateRecipeFileList(); } } /// /// SaveAs recipe by recipe name and recipe data /// /// /// /// public bool SaveAsRecipe(string recipeName, string recipeContent) { return m_uiRecipeManager.SaveAsRecipe(ModuleName, recipeName, recipeContent); } /// /// Recipe template /// public string RecipeTemplate { get { string template = m_uiRecipeManager.GetRecipeTemplate(ModuleName); return template; } } private void UpdateRecipeFileList() { XmlDocument doc = new XmlDocument(); doc.LoadXml(GetXmlRecipeList()); treeViewRcpList.Items.Clear(); CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList); } /// /// Get recipe list in xml format /// /// public string GetXmlRecipeList() { return m_uiRecipeManager.GetXmlRecipeList(ModuleName, true) ?? ""; } void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl) { foreach (XmlElement ele in curElementNode.ChildNodes) { if (ele.Name == "File") { string fileName = ele.Attributes["Name"].Value; fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1); TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value); item.Tag = ele.Attributes["Name"].Value; //item.ToolTip = fileName; itemsControl.Items.Add(item); } else if (ele.Name == "Folder") { string folderName = ele.Attributes["Name"].Value; folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1); TreeViewFolderItem item = new TreeViewFolderItem(folderName); item.Tag = ele.Attributes["Name"].Value; CreateTreeViewItems(ele, item); item.IsExpanded = false; itemsControl.Items.Add(item); } } } #endregion } }