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 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_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; #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); } } #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)); #endregion #region 构造函数 public RecipeViewModel() { Title = "配方管理"; } #endregion #region 方法 private void OnNew() { } private void OnReName() { } private void OnDelete() { } private void OnLoaded(Object myrecipeView) { recipeView = myrecipeView as RecipeView; treeViewRcpList = recipeView.treeViewRcpList; tableRecipeGrid = recipeView.tableRecipeGrid; UpdateRecipeFileList(); treeViewRcpList.SelectedItemChanged += TreeViewRcpList_SelectedItemChanged; } private void TreeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) { if (tableRecipeGrid.IsRecipeModified) { //if (permission == ViewPermission.FullyControl) { switch (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgSaveCurrentRecipe"].ToString(), CurrentRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question)) { case MessageBoxResult.Yes: if (SaveRecipeV()) { var item = e.NewValue as TreeViewFileItem; if (item != null) item.IsSelected = true; } else { var item = e.OldValue as TreeViewFileItem; if (item != null) item.IsSelected = true; return; } break; case MessageBoxResult.No: { var item = e.NewValue as TreeViewFileItem; if (item != null) item.IsSelected = true; } break; } } } var selectedItem = e.NewValue as TreeViewFileItem; if (selectedItem == null) return; try { CurrentRecipeName = selectedItem.FileName; string xmlRecipeData = m_uiRecipeManager.LoadRecipe(m_chamId,selectedItem.FileName); this.tableRecipeGrid.ControlViewModel.LoadRecipe(RecipeFormat, xmlRecipeData); } catch (Exception ex) { //LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// Recipe format /// public string RecipeFormat { get { return m_uiRecipeManager.GetRecipeFormatXml(m_chamId); } } private bool SaveRecipeV(string flag = "Single") { bool ret = true; try { var hasErr = this.tableRecipeGrid.ControlViewModel.Errors.Count > 0; if (!hasErr || MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveInfo"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes) { ret = SaveRecipe(CurrentRecipeName, this.tableRecipeGrid.ControlViewModel.GetRecipeContentString()); tableRecipeGrid.Dispatcher.Invoke(new System.Action(() => { tableRecipeGrid.ControlViewModel.LoadRecipe(RecipeFormat, LoadRecipe(CurrentRecipeName)); })); } else { ret = false; } } catch (Exception ex) { //LOG.Write(ex); MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeSaveException"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning); ret = false; } return ret; } /// /// 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; string recipeContent = m_uiRecipeManager.GetRecipeTemplate(m_chamId); //m_uiRecipeManager.SaveAsRecipe(m_chamId, recipeName, m_uiRecipeManager.LoadRecipe("system",folderName)); if (SaveAsRecipe(recipeName, RecipeTemplate)) { UpdateRecipeFileList(); SelectRecipe(recipeName); } else { WPFMessageBox.ShowError("Error"); } } } catch (Exception ex) { //LOG.Write(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.Write(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 } }