using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HeuristicLab.Analysis;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DatastreamAnalysis {
///
/// A set of bars, each having name and value.
///
[Item("DataBarSet", "A set of bars, each having name and value.")]
[StorableClass]
public class DataBarSet : NamedItem {
public static new Image StaticItemImage {
get { return HeuristicLab.Common.Resources.VSImageLibrary.Performance; }
}
#region properties
[Storable]
private NamedItemCollection bars;
public NamedItemCollection Bars {
get { return bars; }
set {
if (bars != value) {
if(value == null) throw new ArgumentNullException("Value for Bars must not be null.");
if(bars != null) DeregisterBarsEvents();
bars = value;
RegisterBarsEvents();
}
}
}
#endregion
#region constructors, cloner,...
[StorableConstructor]
protected DataBarSet(bool deserializing) : base(deserializing) { }
public DataBarSet() : base() {
Name = "DataBarSet";
Bars = new NamedItemCollection();
}
public DataBarSet(string name) : base(name) {
Bars = new NamedItemCollection();
}
public DataBarSet(string name, string description) : base(name, description) {
Bars = new NamedItemCollection();
}
public DataBarSet(DataBarSet original, Cloner cloner) : base(original, cloner) {
Bars = cloner.Clone(original.bars);
}
public override IDeepCloneable Clone(Cloner cloner) {
return new DataBarSet(this, cloner);
}
#endregion
#region events
//public event EventHandler BarsPropertyChanged;
//protected virtual void OnBarsPropertyChanged() {
// EventHandler handler = BarsPropertyChanged;
// if (handler != null) handler(this, EventArgs.Empty);
//}
//private void Bars_PropertyChanged(object sender, PropertyChangedEventArgs e) {
// OnBarsPropertyChanged();
//}
public event EventHandler ThresholdsPropertyChanged;
protected virtual void OnThresholdsPropertyChanged() {
EventHandler handler = ThresholdsPropertyChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
private void Thresholds_PropertyChanged(object sender, PropertyChangedEventArgs e) {
OnThresholdsPropertyChanged();
}
public event EventHandler BarsPropertyChanged;
public event EventHandler BarValueChanged;
protected virtual void OnBarsPropertyChanged() {
EventHandler handler = BarsPropertyChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
protected virtual void OnBarValueChanged(PropertyChangedEventArgs e) {
EventHandler handler = BarValueChanged;
if(handler != null) handler(this, e);
}
protected virtual void RegisterBarsEvents() {
bars.ItemsAdded += new CollectionItemsChangedEventHandler(Bars_ItemsAdded);
bars.ItemsRemoved += new CollectionItemsChangedEventHandler(Bars_ItemsChanged);
bars.ItemsReplaced += new CollectionItemsChangedEventHandler(Bars_ItemsChanged);
bars.CollectionReset += new CollectionItemsChangedEventHandler(Bars_ItemsChanged);
}
protected virtual void DeregisterBarsEvents() {
bars.ItemsAdded -= new CollectionItemsChangedEventHandler(Bars_ItemsAdded);
bars.ItemsRemoved -= new CollectionItemsChangedEventHandler(Bars_ItemsChanged);
bars.ItemsReplaced -= new CollectionItemsChangedEventHandler(Bars_ItemsChanged);
bars.CollectionReset -= new CollectionItemsChangedEventHandler(Bars_ItemsChanged);
}
private void Bars_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) {
foreach (var bar in e.Items) {
bar.ValuePropertyChanged += new PropertyChangedEventHandler(Bars_ValueChanged);
//bar.ThresholdPropertyChanged += new PropertyChangedEventHandler();
}
OnBarsPropertyChanged();
}
private void Bars_ItemsChanged(object sender, CollectionItemsChangedEventArgs e) {
OnBarsPropertyChanged();
}
private void Bars_ValueChanged(object sender, PropertyChangedEventArgs e) {
OnBarValueChanged(e);
}
#endregion
#region helpers
public static Dictionary CloneDictionary(Dictionary original) where TValue : ICloneable {
Dictionary ret = new Dictionary(original.Count, original.Comparer);
foreach (KeyValuePair entry in original) {
ret.Add(entry.Key, (TValue)entry.Value.Clone());
}
return ret;
}
#endregion
}
}