using Caliburn.Micro.Core; using Prism.Mvvm; using RecipeEditorLib.DGExtension.CustomColumn; using RecipeEditorLib.RecipeModel.Params; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace CyberX8_MainPages.Sequence { public class SequenceData : BindableBase { public SequenceData() { this.Steps = new ObservableCollection>(); } public ObservableCollection> Steps { get; set; } public ObservableCollection CreateStep(ObservableCollection _columns, XmlNode step = null) { PositionParam posParam = null; string posValue = string.Empty; ObservableCollection Params = new ObservableCollection(); foreach (EditorDataGridTemplateColumnBase col in _columns) { Param parm = null; string value = string.Empty; if (!(col is ExpanderColumn) && step != null && !(col is StepColumn)) { if (step.Attributes[col.ControlName] != null) value = step.Attributes[col.ControlName].Value; } if (col is StepColumn) { parm = new StepParam() { Name = col.ControlName }; } else if (col is TextBoxColumn) { parm = new StringParam() { Name = col.ControlName, Value = value, IsEnabled = !col.IsReadOnly }; } else if (col is NumColumn) { if (!string.IsNullOrEmpty(value)) parm = new IntParam() { Name = col.ControlName, Value = int.Parse(value), IsEnabled = !col.IsReadOnly }; else parm = new IntParam() { Name = col.ControlName, Value = 0, IsEnabled = !col.IsReadOnly }; } else if (col is ComboxColumn) { parm = new ComboxParam() { Name = col.ControlName, Value = value, Options = ((ComboxColumn)col).Options, IsEnabled = !col.IsReadOnly }; } else if (col is PositionColumn) { posParam = new PositionParam() { Name = col.ControlName, Options = ((PositionColumn)col).Options, IsEnabled = !col.IsReadOnly }; parm = posParam; posValue = value; } else if (col is ExpanderColumn) { parm = new ExpanderParam() { }; } else if (col is RecipeSelectColumn) { //string path = ((RecipeSelectColumn) col).RecipeProcessType; //if (!string.IsNullOrEmpty(path) && value.StartsWith(path)) //{ // value = value.Substring(path.Length + 1); //} parm = new PathFileParam() { Name = col.ControlName, Value = value, IsEnabled = !col.IsReadOnly, PrefixPath = ((RecipeSelectColumn)col).RecipeProcessType }; } else if (col is MultipleSelectColumn) { parm = new MultipleSelectParam((MultipleSelectColumn)col) { Name = col.ControlName, IsEnabled = !col.IsReadOnly }; string[] opts = value.Split(','); ((MultipleSelectParam)parm).Options.Apply(opt => { for (var index = 0; index < opts.Length; index++) { if (opt.ControlName == opts[index]) { opt.IsChecked = true; break; } } }); parm.IsSaved = true; } parm.Parent = Params; Params.Add(parm); } posParam.Value = posValue; return Params; } public ObservableCollection CloneStep(ObservableCollection _columns, ObservableCollection _sourceParams) { ObservableCollection targetParams = this.CreateStep(_columns); PositionParam posParam = null; string posValue = string.Empty; for (var index = 0; index < _sourceParams.Count; index++) { if (_sourceParams[index] is PositionParam) { posParam = (PositionParam)targetParams[index]; posValue = ((PositionParam)_sourceParams[index]).Value; } else if (_sourceParams[index] is StringParam) { ((StringParam)targetParams[index]).Value = ((StringParam)_sourceParams[index]).Value; } else if (_sourceParams[index] is PathFileParam) { ((PathFileParam)targetParams[index]).Value = ((PathFileParam)_sourceParams[index]).Value; ((PathFileParam)targetParams[index]).PrefixPath = ((PathFileParam)_sourceParams[index]).PrefixPath; } else if (_sourceParams[index] is IntParam) { ((IntParam)targetParams[index]).Value = ((IntParam)_sourceParams[index]).Value; } else if (_sourceParams[index] is ComboxParam) { ((ComboxParam)targetParams[index]).Value = ((ComboxParam)_sourceParams[index]).Value; } else if (_sourceParams[index] is BoolParam) { ((BoolParam)targetParams[index]).Value = ((BoolParam)_sourceParams[index]).Value; } else if (_sourceParams[index] is IntParam) { ((IntParam)targetParams[index]).Value = ((IntParam)_sourceParams[index]).Value; } else if (_sourceParams[index] is DoubleParam) { ((DoubleParam)targetParams[index]).Value = ((DoubleParam)_sourceParams[index]).Value; } targetParams[index].Parent = targetParams; } posParam.Value = posValue; return targetParams; } public void LoadSteps(ObservableCollection _columns, XmlNodeList _steps) { foreach (XmlNode _step in _steps) { this.Steps.Add(this.CreateStep(_columns, _step)); } } public void Load(ObservableCollection _columns, XmlDocument doc) { XmlNode node = doc.SelectSingleNode("Aitex/TableSequenceData"); if (node.Attributes["CreatedBy"] != null) this.Creator = node.Attributes["CreatedBy"].Value; if (node.Attributes["CreationTime"] != null) this.CreateTime = DateTime.Parse(node.Attributes["CreationTime"].Value); if (node.Attributes["LastRevisedBy"] != null) this.Revisor = node.Attributes["LastRevisedBy"].Value; if (node.Attributes["LastRevisionTime"] != null) this.ReviseTime = DateTime.Parse(node.Attributes["LastRevisionTime"].Value); if (node.Attributes["Description"] != null) this.Description = node.Attributes["Description"].Value; foreach (XmlNode _step in doc.SelectNodes("Aitex/TableSequenceData/Step")) { this.Steps.Add(this.CreateStep(_columns, _step)); } } public string ToXml() { StringBuilder builder = new StringBuilder(); builder.Append(""); builder.Append(string.Format("", this.Creator, this.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"), this.Revisor, this.ReviseTime.ToString("yyyy-MM-dd HH:mm:ss"), this.Description)); foreach (ObservableCollection parameters in Steps) { builder.Append(" selected = new List(); ((MultipleSelectParam)parameter).Options.Apply( opt => { if (opt.IsChecked) selected.Add(opt.ControlName); } ); builder.Append(parameter.Name + "=\"" + string.Join(",", selected) + "\" "); } } } builder.Append("/>"); } builder.Append(""); return builder.ToString(); } private string name; public string Name { get { return this.name; } set { SetProperty(ref name, value); } } private string creator; public string Creator { get { return this.creator; } set { SetProperty(ref creator, value); } } private DateTime createTime; public DateTime CreateTime { get { return this.createTime; } set { SetProperty(ref createTime, value); } } private string description; public string Description { get { return this.description; } set { SetProperty(ref description, value); } } private string devisor; public string Revisor { get { return this.devisor; } set { SetProperty(ref devisor, value); } } private DateTime deviseTime; public DateTime ReviseTime { get { return this.deviseTime; } set { SetProperty(ref deviseTime, value); } } } }