| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 | 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<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));        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<object> 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;        }        /// <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;                    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);            }        }        /// <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.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    }}
 |