using Biesse.PlantConfigurator.Core.Exceptions; using Biesse.ScriptTuning; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using ToolClasses = Biesse.ScriptTuning.ToolClasses; using ToolTwinTypes = Biesse.ScriptTuning.ToolTwinTypes; namespace Biesse.PlantConfigurator.Core.SxData.SxParameterEdit { [Serializable] public class ToolParameterEdit : ParameterEdit, IDisposable { public ToolParameterEdit() { Initialize(); } protected ToolParameterEdit(SerializationInfo info, StreamingContext context) : base(info, context) { if (Misc.IsSerializedVariable(info, nameof(Classes))) { _couplingType = (int?)info.GetValue(nameof(CouplingType), typeof(int?)); _allowEmpty = info.GetBoolean(nameof(AllowEmpty)); _editorMode = (ParameterChooseEditorMode)info.GetValue(nameof(EditorMode), typeof(ParameterChooseEditorMode)); Classes = (CustomObservableCollection)info.GetValue(nameof(Classes), typeof(CustomObservableCollection)); SubClasses = Misc.TryGetSerializedVariableValue(info, nameof(SubClasses), () => new CustomObservableCollection()); Rotations = (CustomObservableCollection)info.GetValue(nameof(Rotations), typeof(CustomObservableCollection)); TwinTypes = Misc.TryGetSerializedVariableValue(info, nameof(TwinTypes), () => new CustomObservableCollection()); } else { _couplingType = (int?)info.GetValue(nameof(_couplingType), typeof(int?)); _allowEmpty = info.GetBoolean(nameof(_allowEmpty)); _editorMode = (ParameterChooseEditorMode)info.GetValue(nameof(_editorMode), typeof(ParameterChooseEditorMode)); Classes = (CustomObservableCollection)info.GetValue("k__BackingField", typeof(CustomObservableCollection)); SubClasses = (CustomObservableCollection)info.GetValue("k__BackingField", typeof(CustomObservableCollection)); Rotations = (CustomObservableCollection)info.GetValue("k__BackingField", typeof(CustomObservableCollection)); TwinTypes = Misc.TryGetSerializedVariableValue(info, nameof(TwinTypes), () => new CustomObservableCollection()); } } public CustomObservableCollection Classes { get; set; } public CustomObservableCollection SubClasses { get; set; } public CustomObservableCollection Rotations { get; set; } public CustomObservableCollection TwinTypes { get; set; } private int? _couplingType; public int? CouplingType { get => _couplingType; set { if (IsReadOnly) throw new ReadOnlyElementException(); if (_couplingType != value) { _couplingType = value; OnModified(); } } } private bool _allowEmpty; public bool AllowEmpty { get => _allowEmpty; set { if (IsReadOnly) throw new ReadOnlyElementException(); if (_allowEmpty != value) { _allowEmpty = value; OnModified(); } } } private ParameterChooseEditorMode _editorMode; public ParameterChooseEditorMode EditorMode { get => _editorMode; set { if (IsReadOnly) throw new ReadOnlyElementException(); if (_editorMode != value) { _editorMode = value; OnModified(); } } } protected override Dictionary TokenToProperty { get { var baseTokenProperty = base.TokenToProperty; baseTokenProperty.Add("Classes", nameof(this.Classes)); baseTokenProperty.Add("SubClasses", nameof(this.SubClasses)); baseTokenProperty.Add("Rotations", nameof(this.Rotations)); baseTokenProperty.Add("CouplingType", nameof(this.CouplingType)); baseTokenProperty.Add("AllowEmpty", nameof(this.AllowEmpty)); baseTokenProperty.Add("EditorMode", nameof(this.EditorMode)); return baseTokenProperty; } } private void Initialize() { Classes = new CustomObservableCollection { IsReadOnly = () => this.IsReadOnly }; Classes.CollectionChanged += CustomObservableCollection_CollectionChanged; SubClasses = new CustomObservableCollection { IsReadOnly = () => this.IsReadOnly }; SubClasses.CollectionChanged += CustomObservableCollection_CollectionChanged; Rotations = new CustomObservableCollection { IsReadOnly = () => this.IsReadOnly }; Rotations.CollectionChanged += CustomObservableCollection_CollectionChanged; _couplingType = null; _allowEmpty = false; _editorMode = ParameterChooseEditorMode.Combo; } private void CustomObservableCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { OnModified(); } public override ParameterTypes ParameterType => ParameterTypes.Tool; protected override bool CustomEquals(object obj) { ToolParameterEdit refObj = (ToolParameterEdit)obj; //bool diff = Classes.SequenceEqual(refObj.Classes); // Se l'ordine degli elementi è significativo. bool diff = Classes.Count == refObj.Classes.Count && Classes.All(refObj.Classes.Contains); // Se l'ordine degli elementi NON è significativo. if (!diff) return false; diff = SubClasses.Count == refObj.SubClasses.Count && SubClasses.All(refObj.SubClasses.Contains); if (!diff) return false; diff = Rotations.Count == refObj.Rotations.Count && Rotations.All(refObj.Rotations.Contains); if (!diff) return false; return CouplingType == refObj.CouplingType && AllowEmpty == refObj.AllowEmpty && EditorMode == refObj.EditorMode; } public void Dispose() { Classes.CollectionChanged -= CustomObservableCollection_CollectionChanged; SubClasses.CollectionChanged -= CustomObservableCollection_CollectionChanged; Rotations.CollectionChanged -= CustomObservableCollection_CollectionChanged; } public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); info.AddValue(nameof(CouplingType), _couplingType); info.AddValue(nameof(AllowEmpty), _allowEmpty); info.AddValue(nameof(EditorMode), _editorMode); info.AddValue(nameof(Classes), Classes); info.AddValue(nameof(SubClasses), SubClasses); info.AddValue(nameof(Rotations), Rotations); info.AddValue(nameof(TwinTypes), TwinTypes); } } }