using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Xml; using System.Text; using RecipeEditorLib.DGExtension.CustomColumn; using RecipeEditorLib.RecipeModel.Params; using Caliburn.Micro.Core; namespace VirgoUI.Client.Models.Recipe.Sequence { public class SequenceData : PropertyChangedBase { public bool IsChanged { get; set; } 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 (ColumnBase 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).RecipeFilePath; if (!string.IsNullOrEmpty(path) && value.StartsWith(path)) { value = value.Substring(path.Length + 1); } parm = new PathFileParam() { Name = col.ControlName, Value = value, IsEnabled = !col.IsReadOnly, Path = ((RecipeSelectColumn)col).RecipeFilePath }; } 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]).Path = ((PathFileParam)_sourceParams[index]).Path; } 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; if (node.Attributes["ThicknessType"] != null) { string thickness = node.Attributes["ThicknessType"].Value; IsThick = thickness == "Thick"; } foreach (XmlNode _step in doc.SelectNodes("Aitex/TableSequenceData/Step")) { this.Steps.Add(this.CreateStep(_columns, _step)); } IsChanged = false; } public string ToXml() { StringBuilder builder = new StringBuilder(); builder.Append(""); builder.Append(string.Format("", this.Creator, this.CreateTime, this.Revisor, this.ReviseTime, this.Description, ThicknessType)); 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 { this.name = value; this.NotifyOfPropertyChange("Name"); } } private string creator; public string Creator { get { return this.creator; } set { this.creator = value; this.NotifyOfPropertyChange("Creator"); } } private DateTime createTime; public DateTime CreateTime { get { return this.createTime; } set { this.createTime = value; this.NotifyOfPropertyChange("CreateTime"); } } private string description; public string Description { get { return this.description; } set { this.description = value; this.NotifyOfPropertyChange("Description"); } } public string ThicknessType { get { return _isThick ? "Thick":"Thin"; } } public bool IsThin { get { return !_isThick; } set { _isThick = !value; IsChanged = true; NotifyOfPropertyChange("IsThin"); NotifyOfPropertyChange("IsThick"); } } private bool _isThick; public bool IsThick { get { return _isThick; } set { _isThick = value; IsChanged = true; NotifyOfPropertyChange("IsThin"); NotifyOfPropertyChange("IsThick"); } } private string devisor; public string Revisor { get { return this.devisor; } set { this.devisor = value; this.NotifyOfPropertyChange("Revisor"); } } private DateTime deviseTime; public DateTime ReviseTime { get { return this.deviseTime; } set { this.deviseTime = value; this.NotifyOfPropertyChange("ReviseTime"); } } } }