| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 | 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<Object> _MouseRightButtonDownCommand;        public DelegateCommand<Object> MouseRightButtonDownCommand =>            _MouseRightButtonDownCommand ?? (_MouseRightButtonDownCommand = new DelegateCommand<Object>(OnMouseRightButtonDown));        private DelegateCommand<Object> _LoadedCommand;        public DelegateCommand<Object> LoadedCommand =>            _LoadedCommand ?? (_LoadedCommand = new DelegateCommand<Object>(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<object> 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);            }        }        /// <summary>        /// Recipe format        /// </summary>        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;        }        /// <summary>        /// Load recipe data by recipe name        /// </summary>        /// <param name="receipeName"></param>        /// <returns></returns>        public string LoadRecipe(string receipeName)        {            return m_uiRecipeManager.LoadRecipe(m_chamId, receipeName);        }        /// <summary>        /// Save recipe by recipe name and recipe data        /// </summary>        /// <param name="recipeName"></param>        /// <param name="recipe"></param>        /// <returns></returns>        public bool SaveRecipe(string recipeName, string recipeContent)        {            return m_uiRecipeManager.SaveRecipe(m_chamId, recipeName, recipeContent);        }        /// <summary>        /// 右击TreeView,上下文菜单选择功能        /// </summary>        /// <param name="treeView"></param>        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;        }        /// <summary>        /// 创建新的配方        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        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);            }        }        /// <summary>        /// SaveAs recipe by recipe name and recipe data        /// </summary>        /// <param name="recipeName"></param>        /// <param name="recipe"></param>        /// <returns></returns>        public bool SaveAsRecipe(string recipeName, string recipeContent)        {            return m_uiRecipeManager.SaveAsRecipe(m_chamId, recipeName, recipeContent);        }        /// <summary>        /// Recipe template        /// </summary>        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);        }        /// <summary>        /// Get recipe list in xml format        /// </summary>        /// <returns></returns>        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    }}
 |