using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Xml; using Aitex.Core.Account; using Aitex.Triton160.UI.ViewModel; using ExcelLibrary.SpreadSheet; using Triton160.UI.ViewModel; using Aitex.Core.RT.Log; using Aitex.Core.UI.View.Common; using Aitex.UI.RecipeEditor.View; using Aitex.Triton160.Common; namespace Aitex.Triton160.UI.Views { /// /// Interaction logic for RecipeEditorView.xaml /// public partial class RecipeEditorView : UserControl { private string _culture; private string tragetFilePath = ""; private object OriginalItem; RecipeEditorViewModel _viewModel; private Point _lastMouseDown; private ViewPermission _permission; public RecipeEditorView() { InitializeComponent(); this.DataContext = _viewModel = new RecipeEditorViewModel(new UiRecipeManager(), ModuleName.System.ToString()); this.Loaded += new RoutedEventHandler(RecipeEditorView_Loaded); this.Unloaded += new RoutedEventHandler(RecipeEditorView_Unloaded); Triton160UiSystem.Instance.CultureChanged += CultureChanged; } private void CultureChanged(string s) { _culture = s; this.tableRecipeGrid.UpdateCultureResource(s); } static DependencyObject VisualUpwardSearch(DependencyObject source) { while (source != null && source.GetType() != typeof(T)) source = VisualTreeHelper.GetParent(source); return source; } void RecipeEditorView_Unloaded(object sender, RoutedEventArgs e) { } void RecipeEditorView_Loaded(object sender, RoutedEventArgs e) { if (_culture!=Thread.CurrentThread.CurrentCulture.Name) { _culture = Thread.CurrentThread.CurrentCulture.Name; this.tableRecipeGrid.UpdateCultureResource(_culture); } _viewModel.UpdateAllConfig(); tableRecipeGrid.SetEnableBarcode(_viewModel.IsBarcodeEnabled); tableRecipeGrid.SetUser(Triton160UiSystem.Instance.CurrentUserName); UpdateRecipeFileList(); this.Name = "recipeEditor"; _permission = this.GetPermission(); switch (_permission) { case ViewPermission.Readonly: buttonReloadTableRecipe.IsEnabled = false; tableRecipeGrid.IsEnabled = false; buttonSaveTableRecipe.IsEnabled = false; break; default: break; } } private void treeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) { //var permission = this.GetPermission(); if (tableRecipeGrid.IsRecipeModified) { //if (permission == ViewPermission.FullyControl) { switch (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgSaveCurrentRecipe"].ToString(), _viewModel.CurrentRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question)) { case MessageBoxResult.Yes: if (SaveRecipe()) { 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 { this._viewModel.CurrentRecipeName = selectedItem.FileName; string xmlRecipeData = _viewModel.LoadRecipe(selectedItem.FileName); this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.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); } } private bool SaveRecipe() { 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 = _viewModel.SaveRecipe(this._viewModel.CurrentRecipeName, this.tableRecipeGrid.ControlViewModel.GetRecipeContentString()); if (ret) { tableRecipeGrid.Dispatcher.Invoke(new System.Action(() => { this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, _viewModel.LoadRecipe(_viewModel.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; } private void treeViewRcpList_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (_permission != ViewPermission.FullyControl) return; //black place right mouse click treeViewRcpList.ContextMenu = new ContextMenu(); MenuItem menuItem = new MenuItem(); menuItem = new MenuItem(); menuItem.Tag = "\\"; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe); menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"]; this.treeViewRcpList.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = "\\"; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe); menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"]; this.treeViewRcpList.ContextMenu.Items.Add(menuItem); this.treeViewRcpList.ContextMenu.Items.Add(new Separator()); menuItem = new MenuItem(); menuItem.Tag = "\\"; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder); menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"]; this.treeViewRcpList.ContextMenu.Items.Add(menuItem); treeViewRcpList.ContextMenu.Visibility = Visibility.Visible; } private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (_permission != ViewPermission.FullyControl) return; var treeViewFileItem = VisualUpwardSearch(e.OriginalSource as DependencyObject) as TreeViewItem; if (treeViewFileItem != null) { treeViewFileItem.Focus(); treeViewFileItem.IsSelected = true; treeViewFileItem.ContextMenu = new ContextMenu(); MenuItem menuItem = null; menuItem = new MenuItem(); menuItem.Tag = treeViewFileItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ExportRecipe); menuItem.Header = Application.Current.Resources["GlobalLableMenuExportRecipe"]; treeViewFileItem.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = treeViewFileItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ExportExcel); menuItem.Header = Application.Current.Resources["GlobalLableMenuExportRecipeAsExcel"]; treeViewFileItem.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = treeViewFileItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_SaveAs); menuItem.Header = Application.Current.Resources["GlobalLableMenuSaveAs"]; treeViewFileItem.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = treeViewFileItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameRecipe); menuItem.Header = Application.Current.Resources["GlobalLableMenuRenameRecipe"]; treeViewFileItem.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = treeViewFileItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteRecipe); menuItem.Header = Application.Current.Resources["GlobalLableMenuDeleteRecipe"]; treeViewFileItem.ContextMenu.Items.Add(menuItem); treeViewFileItem.ContextMenu.Visibility = Visibility.Visible; } else { var treeViewFolderItem = VisualUpwardSearch(e.OriginalSource as DependencyObject) as TreeViewItem; if (treeViewFolderItem != null) { treeViewFolderItem.Focus(); treeViewFolderItem.IsSelected = true; treeViewFolderItem.ContextMenu = new ContextMenu(); MenuItem menuItem = null; menuItem = new MenuItem(); menuItem.Tag = treeViewFolderItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe); menuItem.Header = Application.Current.Resources["GlobalLableMenuNewRecipe"]; treeViewFolderItem.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = treeViewFolderItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_ImportRecipe); menuItem.Header = Application.Current.Resources["GlobalLableMenuImportRecipe"]; treeViewFolderItem.ContextMenu.Items.Add(menuItem); treeViewFolderItem.ContextMenu.Items.Add(new Separator()); menuItem = new MenuItem(); menuItem.Tag = treeViewFolderItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateFolder); menuItem.Header = Application.Current.Resources["GlobalLableMenuNewFolder"]; treeViewFolderItem.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = treeViewFolderItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameFolder); menuItem.Header = Application.Current.Resources["GlobalLableMenuRenameFolder"]; treeViewFolderItem.ContextMenu.Items.Add(menuItem); menuItem = new MenuItem(); menuItem.Tag = treeViewFolderItem.Tag as string; menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteFolder); menuItem.Header = Application.Current.Resources["GlobalLableMenuDeleteFolder"]; treeViewFolderItem.ContextMenu.Items.Add(menuItem); treeViewFolderItem.ContextMenu.Visibility = Visibility.Visible; } } } /// /// reload current recipe content /// /// /// private void buttonCodeReload_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeReloadInfo"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes) { this.tableRecipeGrid.Dispatcher.Invoke(new System.Action(() => { this.tableRecipeGrid.ControlViewModel.LoadRecipe(_viewModel.RecipeFormat, _viewModel.LoadRecipe(_viewModel.CurrentRecipeName)); })); } } /// /// Save recipe file /// /// /// private void buttonCodeSave_Click(object sender, RoutedEventArgs e) { SaveRecipe(); } 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; } private void UpdateRecipeFileList() { XmlDocument doc = new XmlDocument(); try { string rcp = _viewModel.GetXmlRecipeList(); if (string.IsNullOrWhiteSpace(rcp)) return; doc.LoadXml(rcp); } catch (Exception ex) { LOG.Write(ex); MessageBox.Show("Cannot update recipe files", "RecipeEditorView", MessageBoxButton.OK, MessageBoxImage.Error); } this.treeViewRcpList.Items.Clear(); CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList); } 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 menuItem_MouseClick_SaveAs(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; var oldName = mit.Tag as string; RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString()) { InputText = oldName, Owner = Application.Current.MainWindow }; var ret = dlg.ShowDialog(); if (ret.HasValue && ret.Value) { var newName = dlg.InputText; if (_viewModel.SaveAsRecipe(newName, _viewModel.LoadRecipe(oldName))) { UpdateRecipeFileList(); SelectRecipe(newName); } else { MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeSaveAsFailed"].ToString(), newName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning); } } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeSaveAsException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } void menuItem_MouseClick_CreateFolder(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; string folderName = mit.Tag as string; var selectedRecipeItem = treeViewRcpList.SelectedItem as TreeViewFileItem; RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputFolderName"].ToString()) { Owner = Application.Current.MainWindow }; if (dlg.ShowDialog() == true) { this._viewModel.CreateFolder(folderName + "\\" + dlg.InputText); UpdateRecipeFileList(); if (selectedRecipeItem != null) SelectRecipe(selectedRecipeItem.FileName); } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeFolderNewFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString()); } } /// /// Create new recipe /// /// /// void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; string folderName = mit.Tag as string; RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString()) { Owner = Application.Current.MainWindow }; if (dlg.ShowDialog() == true) { var recipeName = folderName + "\\" + dlg.InputText; if (!this._viewModel.SaveAsRecipe(recipeName, this._viewModel.RecipeTemplate)) { MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateFailed"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning); } else { UpdateRecipeFileList(); SelectRecipe(folderName + "\\" + dlg.InputText); } } } 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); } } /// /// delete folder /// /// /// void menuItem_MouseClick_DeleteFolder(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; var selectedRecipeItem = treeViewRcpList.SelectedItem as TreeViewFileItem; string folderName = mit.Tag as string; if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteFolderInfo"].ToString(), folderName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { this._viewModel.DeleteFolder(folderName); UpdateRecipeFileList(); if (selectedRecipeItem != null) SelectRecipe(selectedRecipeItem.FileName); } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteFolderException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// rename folder /// /// /// void menuItem_MouseClick_RenameFolder(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; string folderName = mit.Tag as string; int lastSlashIndex = folderName.LastIndexOf('\\'); string shortName = folderName.Substring(lastSlashIndex + 1); RecipeNameInputDlg dlg = new RecipeNameInputDlg(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderInfo"].ToString(), shortName), Application.Current.Resources["GlobalLableMsgInputFolderName"].ToString()) { Owner = Application.Current.MainWindow }; if (dlg.ShowDialog() == true) { string dir = lastSlashIndex == -1 ? "" : folderName.Substring(0, lastSlashIndex); if (!_viewModel.RenameFolder(folderName, dir + "\\" + dlg.InputText)) { MessageBox.Show(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderFailed"].ToString(), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning); } else { UpdateRecipeFileList(); } } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFolderException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// import external recipe file into selected folder /// /// /// void menuItem_MouseClick_ImportRecipe(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; string folderName = mit.Tag as string; Microsoft.Win32.OpenFileDialog openDlg = new Microsoft.Win32.OpenFileDialog(); openDlg.DefaultExt = ".rcp"; openDlg.Filter = "Recipe file (.rcp)|*.rcp"; openDlg.Multiselect = true; if (openDlg.ShowDialog() == true) { foreach (var openDlgFileName in openDlg.FileNames) { using (StreamReader srd = new StreamReader(openDlgFileName)) { bool importFailed = false; string rcpContent = srd.ReadToEnd(); RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgRecipeImportInfo"].ToString(), Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString()) { Owner = Application.Current.MainWindow }; dlg.InputText = System.IO.Path.GetFileNameWithoutExtension(openDlgFileName); if (dlg.ShowDialog() == true) { var recipeName = mit.Tag as string + "\\" + dlg.InputText; XmlDocument rcpDoc = new XmlDocument(); rcpDoc.LoadXml(rcpContent); rcpDoc.DocumentElement.SetAttribute("RecipeVersion", _viewModel.RecipeVersion()); if (!this._viewModel.SaveAsRecipe(recipeName, rcpDoc.OuterXml)) { importFailed = true; MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeImportFailed"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning); } } srd.Close(); if (!importFailed) File.Delete(openDlgFileName); UpdateRecipeFileList(); SelectRecipe(mit.Tag as string + "\\" + dlg.InputText); } } } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeImportException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// delete selected recipe files /// /// /// void menuItem_MouseClick_DeleteRecipe(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; string recipeName = mit.Tag as string; int sepIndex = recipeName.LastIndexOf("\\"); if (sepIndex < 0) sepIndex = 0; else sepIndex++; recipeName = recipeName.Substring(sepIndex); if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteInfo"].ToString(), recipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { this._viewModel.DeleteRecipe(mit.Tag as string); this.UpdateRecipeFileList(); } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeDeleteException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// rename selected recipe files /// /// /// void menuItem_MouseClick_RenameRecipe(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; string recipeName = mit.Tag as string; int lastSlashIndex = recipeName.LastIndexOf('\\'); string shortName = recipeName.Substring(lastSlashIndex + 1); TreeViewItem rightClickedItem = this.treeViewRcpList.SelectedItem as TreeViewItem; RecipeNameInputDlg dlg = new RecipeNameInputDlg(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameInfo"].ToString(), shortName), Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString()) { InputText = shortName, Owner = Application.Current.MainWindow }; if (dlg.ShowDialog() == true) { string newRecipeName = lastSlashIndex == -1 ? dlg.InputText : (recipeName.Substring(0, lastSlashIndex) + "\\" + dlg.InputText); var ret = this._viewModel.RenameRecipe(recipeName, newRecipeName); if (ret) { UpdateRecipeFileList(); SelectRecipe(newRecipeName); } else { MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameFailed"].ToString(), newRecipeName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning); } } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeRenameException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// export selected recipe files /// /// /// void menuItem_MouseClick_ExportRecipe(object sender, RoutedEventArgs e) { try { MenuItem mit = sender as MenuItem; string recipeName = mit.Tag as string; var dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = recipeName.Substring(recipeName.LastIndexOf('\\') + 1); dlg.DefaultExt = ".rcp"; dlg.Filter = Application.Current.Resources["GlobalLableMsgRecipeExportFilter"].ToString(); if (dlg.ShowDialog() == true) { using (StreamWriter sw = new StreamWriter(dlg.FileName)) { sw.Write(this._viewModel.LoadRecipe(recipeName)); sw.Close(); } } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } } void menuItem_MouseClick_ExportExcel(object sender, RoutedEventArgs e) { MenuItem mit = sender as MenuItem; string recipeName = mit.Tag as string; var dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = recipeName.Substring(recipeName.LastIndexOf('\\') + 1); dlg.DefaultExt = ".xls"; dlg.Filter = Application.Current.Resources["GlobalLableMsgRecipeExportExcelFilter"].ToString(); var ret = dlg.ShowDialog(); if (!ret.HasValue || !ret.Value) return; string fileName = dlg.FileName; this._viewModel.CurrentRecipeName = recipeName; string xmlRecipeData = _viewModel.LoadRecipe(recipeName); XmlDocument doc = new XmlDocument(); doc.LoadXml(_viewModel.RecipeFormat); Task.Factory.StartNew(() => { try { if (File.Exists(fileName)) File.Delete(fileName); Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet("First Sheet"); var catalogNodes = doc.SelectNodes("/TableRecipeFormat/Catalog"); int groupRowIndex = 0; int stepRowIndex = 0; int catalogRowIndex = 0; List excelRowHeaderName = new List(); foreach (XmlElement cNode in catalogNodes) { XmlNodeList sNodes = cNode.SelectNodes("Group/Step"); string catalogHeader = cNode.Attributes["DisplayName"].Value; worksheet.Cells[catalogRowIndex + 1, 0] = new Cell(catalogHeader); catalogRowIndex += sNodes.Count; XmlNodeList gNodes = cNode.SelectNodes("Group"); foreach (XmlElement gNode in gNodes) { XmlNodeList stepNodes = gNode.SelectNodes("Step"); string groupHeader = gNode.Attributes["DisplayName"].Value; worksheet.Cells[groupRowIndex + 1, 1] = new Cell(groupHeader); groupRowIndex += stepNodes.Count; foreach (XmlElement nd in stepNodes) { string stepHeader = nd.Attributes["DisplayName"].Value; worksheet.Cells[stepRowIndex + 1, 2] = new Cell(stepHeader); excelRowHeaderName.Add(nd.Attributes["ControlName"].Value); stepRowIndex++; } } } XmlDocument rcp = new XmlDocument(); rcp.LoadXml(xmlRecipeData); XmlNodeList stepNodeList = rcp.SelectNodes("/TableRecipeData/Step"); for (int stepNo = 0; stepNo < stepNodeList.Count; stepNo++) { var dataDic = new Dictionary(); foreach (XmlAttribute att in (stepNodeList[stepNo] as XmlElement).Attributes) { dataDic.Add(att.Name, att.Value); } //fill sub node's attributes foreach (XmlElement subStep in stepNodeList[stepNo].ChildNodes) { foreach (XmlAttribute att in subStep.Attributes) { dataDic.Add(att.Name, att.Value); } foreach (XmlElement subsubStep in subStep.ChildNodes) { foreach (XmlAttribute att2 in subsubStep.Attributes) { dataDic.Add(att2.Name, att2.Value); } } } long length = dataDic.LongCount(); worksheet.Cells[0, 3] = new Cell(recipeName); for (int rowIndex = 0; rowIndex < excelRowHeaderName.Count; rowIndex++) { worksheet.Cells.ColumnWidth[(ushort)(rowIndex + 1)] = 4000; string cellValueString = dataDic[excelRowHeaderName[rowIndex]]; double cellValue; if (double.TryParse(cellValueString, out cellValue)) { worksheet.Cells[rowIndex + 1, stepNo + 3] = new Cell(cellValue); } else { worksheet.Cells[rowIndex + 1, stepNo + 3] = new Cell(cellValueString); } } } workbook.Worksheets.Add(worksheet); workbook.Save(fileName); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportExcelOk"].ToString(), recipeName, fileName), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { LOG.Write(ex); MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeExportExcelFailed"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error); } }); } private void TreeViewRcpList_MouseMove(object sender, MouseEventArgs e) { try { if (e.LeftButton == MouseButtonState.Pressed) { Point currentPosition = e.GetPosition(treeViewRcpList); OriginalItem = e.OriginalSource; if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 2.0) || (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 2.0)) { if ((treeViewRcpList.SelectedItem as TreeViewItem) != null) { //DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeViewRcpList, treeViewRcpList.SelectedItem, DragDropEffects.Move); DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeViewRcpList.SelectedItem as TreeViewItem, sender, DragDropEffects.Move); } } } } catch (Exception ex) { LOG.Write(ex); } } private void TreeViewRcpList_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) { _lastMouseDown = e.GetPosition(treeViewRcpList); } } private void TreeViewRcpList_DragEnter(object sender, DragEventArgs e) { try { if (GetAncestor((DependencyObject)e.Source) != null) { if (GetAncestor((DependencyObject)e.Source) == GetAncestor((DependencyObject)OriginalItem) || VisualTreeHelper.GetParent(GetAncestor((DependencyObject)e.Source)) == VisualTreeHelper.GetParent(GetAncestor((DependencyObject)OriginalItem))) e.Effects = DragDropEffects.None; } else if (GetAncestor((DependencyObject)e.Source) != null) { if (GetAncestor((DependencyObject)e.Source) == GetAncestor((DependencyObject)OriginalItem)) e.Effects = DragDropEffects.None; } else if (e.Source is TreeView) { e.Effects = DragDropEffects.None; } e.Handled = true; } catch (Exception ex) { LOG.Write(ex); } } private void TreeViewRcpList_Drop(object sender, DragEventArgs e) { try { TreeViewItem itemRemoved = treeViewRcpList.SelectedItem as TreeViewItem; if (GetAncestor((DependencyObject)e.Source) != null) { if (GetAncestor((DependencyObject)e.Source) != null) { TreeViewFolderItem getTreeViewFolderItem = GetAncestor((DependencyObject)e.Source); tragetFilePath = getTreeViewFolderItem.FolderName; GetAncestor(getTreeViewFolderItem); _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath); } else { tragetFilePath = ""; GetAncestor((DependencyObject)e.Source); _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath); } } else if (GetAncestor((DependencyObject)e.Source) != null) { TreeViewFolderItem getTreeViewFolderItem = GetAncestor((DependencyObject)e.Source); tragetFilePath = getTreeViewFolderItem.FolderName; GetAncestor(getTreeViewFolderItem); _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath); } UpdateRecipeFileList(); SelectRecipe(tragetFilePath + "\\" + System.IO.Path.GetFileNameWithoutExtension(((TreeViewFileItem)itemRemoved).FileName)); } catch (Exception ex) { LOG.Write(ex); } } public T GetAncestor(DependencyObject reference) where T : DependencyObject { DependencyObject parent = VisualTreeHelper.GetParent(reference); while (!(parent is T) && parent != null) { parent = VisualTreeHelper.GetParent(parent); if (parent is TreeViewFolderItem) tragetFilePath = tragetFilePath.Insert(0, $"{((TreeViewFolderItem)parent).FolderName}\\"); } if (parent != null) return (T)parent; else return null; } } }