using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
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.Collections.ObjectModel;
using Xceed.Wpf.DataGrid;
using System.Xml;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;
namespace Aitex.UI.RecipeEditor
{
///
/// Interaction logic for RecipeEditorControl.xaml
///
public partial class RecipeEditorControl : UserControl
{
public RecipeEditorControl()
{
InitializeComponent();
ControlViewModel = new RecipeEditorControlViewModel() { DataGridControl = this.dataGrid1 };
Loaded += new RoutedEventHandler(RecipeEditorView_Loaded);
}
///
/// indicate whether recipe modified
///
public bool IsRecipeModified
{
get { return ControlViewModel.IsRecipeModified; }
}
///
/// RecipeEditor's view model
///
public RecipeEditorControlViewModel ControlViewModel { get; set; }
///
/// mask recipe item display
///
///
public void SetDisplayMask(HashSet maskedTechNames = null, HashSet maskedCatalogNames = null)
{
if (ControlViewModel != null)
{
ControlViewModel.MaskedTechNames = maskedTechNames;
ControlViewModel.MaskedCatalogNames = maskedCatalogNames;
ControlViewModel.RefreshCellsDisplay(false);
}
}
public void SetEnableBarcode(bool enabled)
{
if (ControlViewModel != null)
{
ControlViewModel.EnableBarcode(enabled);
}
}
public void SetUser(string user)
{
if (ControlViewModel != null)
{
ControlViewModel.SetCurrentUser(user);
}
}
public void SetEndPointDefaultValue(string defaultValue)
{
if (ControlViewModel != null)
{
ControlViewModel.SetEndPointDefaultValue(defaultValue);
}
}
///
/// loaded handling
///
///
///
void RecipeEditorView_Loaded(object sender, RoutedEventArgs e)
{
grid1.DataContext = ControlViewModel;
}
///
/// open recipe file
///
///
///
private void OpenButtonPanelLoaded(object sender, RoutedEventArgs e)
{
try
{
string variation = (string)((Button)sender).Tag;
ControlViewModel.OpenLocalRecipe(variation);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
///
/// update recipe variations
///
///
///
private void NewButtonPanelLoaded(object sender, RoutedEventArgs e)
{
try
{
string variation = (string)((Button)sender).Tag;
newButton.IsOpen = false;
XmlDocument doc = new XmlDocument();
var dir = new System.IO.FileInfo(Process.GetCurrentProcess().MainModule.FileName).Directory;
string xmlPath = dir + "\\Config\\" + variation + ".xml";
doc.Load(xmlPath);
ControlViewModel.LoadRecipe(doc.SelectSingleNode("/Aitex/TableRecipeFormat").OuterXml, doc.SelectSingleNode("/Aitex/TableRecipeData").OuterXml);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
public void UpdateCultureResource(string culture)
{
return;
////string culture = language == 2 ? "zh-CN" : "en-US";
////Copy all MergedDictionarys into a auxiliar list.
//var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
////Search for the specified culture.
//string requestedCulture = string.Format(@"/RecipeEditorControl;component/Resources/StringResources.{0}.xaml", culture);
//var resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
//if (resourceDictionary == null)
//{
// //If not found, select our default language.
// requestedCulture = "StringResources.xaml";
// resourceDictionary = dictionaryList.
// FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
//}
////If we have the requested resource, remove it from the list and place at the end.
////Then this language will be our string table to use.
//if (resourceDictionary != null)
//{
// Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
// Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
//}
////Inform the threads of the new culture.
//Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
//Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
}
private void UIElement_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text);
}
private static bool IsTextAllowed(string text)
{
return true;
//Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
//return !regex.IsMatch(text);
}
}
}