123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984 |
- 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
- {
- /// <summary>
- /// Interaction logic for RecipeEditorView.xaml
- /// </summary>
- 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<T>(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<object> 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<TreeViewFileItem>(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<TreeViewFolderItem>(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;
- }
- }
- }
- /// <summary>
- /// reload current recipe content
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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));
- }));
- }
- }
- /// <summary>
- /// Save recipe file
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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());
- }
- }
- /// <summary>
- /// Create new recipe
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// delete folder
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// rename folder
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// import external recipe file into selected folder
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// delete selected recipe files
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// rename selected recipe files
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// export selected recipe files
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<string> excelRowHeaderName = new List<string>();
- 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<string, string>();
- 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<TreeViewFileItem>((DependencyObject)e.Source) != null)
- {
- if (GetAncestor<TreeViewFileItem>((DependencyObject)e.Source) == GetAncestor<TreeViewFileItem>((DependencyObject)OriginalItem)
- || VisualTreeHelper.GetParent(GetAncestor<TreeViewFileItem>((DependencyObject)e.Source)) == VisualTreeHelper.GetParent(GetAncestor<TreeViewFileItem>((DependencyObject)OriginalItem)))
- e.Effects = DragDropEffects.None;
- }
- else if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
- {
- if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) == GetAncestor<TreeViewFolderItem>((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<TreeViewFileItem>((DependencyObject)e.Source) != null)
- {
- if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
- {
- TreeViewFolderItem getTreeViewFolderItem = GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source);
- tragetFilePath = getTreeViewFolderItem.FolderName;
- GetAncestor<TreeView>(getTreeViewFolderItem);
- _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath);
- }
- else
- {
- tragetFilePath = "";
- GetAncestor<TreeView>((DependencyObject)e.Source);
- _viewModel.MoveRecipeFile(((TreeViewFileItem)itemRemoved).FileName, tragetFilePath);
- }
- }
- else if (GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source) != null)
- {
- TreeViewFolderItem getTreeViewFolderItem = GetAncestor<TreeViewFolderItem>((DependencyObject)e.Source);
- tragetFilePath = getTreeViewFolderItem.FolderName;
- GetAncestor<TreeView>(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<T>(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;
- }
- }
- }
|