#region License Information /* HeuristicLab * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Core; using HEAL.Attic; namespace HeuristicLab.DatastreamAnalysis { /// /// A set of bars, each having name and value. /// [Item("DataBarSet", "A set of bars, each having name and value.")] [StorableType("B07EF3A9-8500-4140-AF73-9C225A5AC2B5")] 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(StorableConstructorFlag _) : base(_) { } 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 } }