using Aitex.Core.RT.Log; using Aitex.Core.UI.View.Common; using Aitex.UI.RecipeEditor; using Aitex.UI.RecipeEditor.View; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; 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; private string m_chamId = "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(); } private void OnDeleteStep() { this.tableRecipeGrid.ControlViewModel.OnDeleteStep(); } 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; } } private void TreeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) { var selectedItem = e.NewValue as TreeViewFileItem; if (selectedItem == null) return; try { CurrentRecipeName = selectedItem.FileName; string xmlRecipeData = m_uiRecipeManager.LoadRecipe(m_chamId, 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(m_chamId, receipeName); } /// /// Save recipe by recipe name and recipe data /// /// /// /// public bool SaveRecipe(string recipeName, string recipeContent) { return m_uiRecipeManager.SaveRecipe(m_chamId, 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 = Application.Current.Resources["GlobalLableMenuNewRecipe"]; treeViewRcpList.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = "\\"; //menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe); menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"]; treeViewRcpList.ContextMenu.Items.Add(menuItem); treeViewRcpList.ContextMenu.Items.Add(new Separator()); menuItem = new MenuItem(); menuItem.Tag = "\\"; //menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder); menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"]; 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(m_chamId); //m_uiRecipeManager.SaveAsRecipe(m_chamId, 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); } } /// /// SaveAs recipe by recipe name and recipe data /// /// /// /// public bool SaveAsRecipe(string recipeName, string recipeContent) { return m_uiRecipeManager.SaveAsRecipe(m_chamId, recipeName, recipeContent); } /// /// Recipe template /// public string RecipeTemplate { get { string template = m_uiRecipeManager.GetRecipeTemplate(m_chamId); 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(m_chamId, 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); } } } //void SelectRecipe(string recipeName) //{ // try // { // string[] paths = recipeName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); // string fileName = ""; // for (int i = 0; i < paths.Length - 1; i++) // fileName += paths[i] + "\\"; // fileName += paths[paths.Length - 1]; // selectRecipe(treeViewRcpList, paths, 0, fileName); // } // catch (Exception ex) // { // LOG.WriteExeption(ex); // } //} //ItemsControl selectRecipe(ItemsControl currentNode, string[] paths, int index, string fileName) //{ // if (currentNode == null) // return null; // if (index == paths.Length - 1) // { // foreach (var item in currentNode.Items) // { // TreeViewFileItem tvf = item as TreeViewFileItem; // if (tvf != null && tvf.FileName == fileName) // { // tvf.IsSelected = true; // return null; // } // } // } // foreach (var item in currentNode.Items) // { // TreeViewFolderItem tvf = item as TreeViewFolderItem; // if (tvf != null && tvf.FolderName == paths[index]) // { // tvf.IsExpanded = true; // selectRecipe(tvf, paths, index + 1, fileName); // break; // } // } // return null; //} #endregion } }