Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2719_HeuristicLab.DatastreamAnalysis/HeuristicLab.DatastreamAnalysis/3.4/DataBarSet.cs @ 18242

Last change on this file since 18242 was 17980, checked in by jzenisek, 3 years ago

#2719 merged head of HeuristicLab.Problems.DataAnalysis into branch; added several minor items

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HEAL.Attic;
30
31namespace HeuristicLab.DatastreamAnalysis {
32  /// <summary>
33  /// A set of bars, each having name and value.
34  /// </summary>
35  [Item("DataBarSet", "A set of bars, each having name and value.")]
36  [StorableType("B07EF3A9-8500-4140-AF73-9C225A5AC2B5")]
37  public class DataBarSet : NamedItem {
38    public static new Image StaticItemImage {
39      get { return HeuristicLab.Common.Resources.VSImageLibrary.Performance; }
40    }
41
42    #region properties
43
44    [Storable]
45    private NamedItemCollection<DataBar> bars;
46
47    public NamedItemCollection<DataBar> Bars {
48      get { return bars; }
49      set {
50        if (bars != value) {
51          if(value == null) throw new ArgumentNullException("Value for Bars must not be null.");
52          if(bars != null) DeregisterBarsEvents();
53          bars = value;
54          RegisterBarsEvents();
55        }
56      }
57    }
58
59    #endregion
60
61    #region constructors, cloner,...
62    [StorableConstructor]
63    protected DataBarSet(StorableConstructorFlag _) : base(_) { }
64
65    public DataBarSet() : base() {
66      Name = "DataBarSet";
67      Bars = new NamedItemCollection<DataBar>();
68    }
69
70    public DataBarSet(string name) : base(name) {
71      Bars = new NamedItemCollection<DataBar>();
72    }
73
74    public DataBarSet(string name, string description) : base(name, description) {
75      Bars = new NamedItemCollection<DataBar>();
76    }
77
78    public DataBarSet(DataBarSet original, Cloner cloner) : base(original, cloner) {
79      Bars = cloner.Clone(original.bars);
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new DataBarSet(this, cloner);
84    }
85    #endregion
86
87    #region events
88
89    //public event EventHandler BarsPropertyChanged;
90
91
92    //protected virtual void OnBarsPropertyChanged() {
93    //  EventHandler handler = BarsPropertyChanged;
94    //  if (handler != null) handler(this, EventArgs.Empty);
95    //}
96
97    //private void Bars_PropertyChanged(object sender, PropertyChangedEventArgs e) {
98    //  OnBarsPropertyChanged();
99    //}
100
101    public event EventHandler ThresholdsPropertyChanged;
102
103    protected virtual void OnThresholdsPropertyChanged() {
104      EventHandler handler = ThresholdsPropertyChanged;
105      if (handler != null) handler(this, EventArgs.Empty);
106    }
107
108    private void Thresholds_PropertyChanged(object sender, PropertyChangedEventArgs e) {
109      OnThresholdsPropertyChanged();
110    }
111
112
113
114    public event EventHandler BarsPropertyChanged;
115    public event EventHandler BarValueChanged;
116    protected virtual void OnBarsPropertyChanged() {
117      EventHandler handler = BarsPropertyChanged;
118      if (handler != null) handler(this, EventArgs.Empty);
119    }
120
121    protected virtual void OnBarValueChanged(PropertyChangedEventArgs e) {
122      EventHandler handler = BarValueChanged;
123      if(handler != null) handler(this, e);
124    }
125
126    protected virtual void RegisterBarsEvents() {
127      bars.ItemsAdded += new CollectionItemsChangedEventHandler<DataBar>(Bars_ItemsAdded);
128      bars.ItemsRemoved += new CollectionItemsChangedEventHandler<DataBar>(Bars_ItemsChanged);
129      bars.ItemsReplaced += new CollectionItemsChangedEventHandler<DataBar>(Bars_ItemsChanged);
130      bars.CollectionReset += new CollectionItemsChangedEventHandler<DataBar>(Bars_ItemsChanged);
131    }
132
133    protected virtual void DeregisterBarsEvents() {
134      bars.ItemsAdded -= new CollectionItemsChangedEventHandler<DataBar>(Bars_ItemsAdded);
135      bars.ItemsRemoved -= new CollectionItemsChangedEventHandler<DataBar>(Bars_ItemsChanged);
136      bars.ItemsReplaced -= new CollectionItemsChangedEventHandler<DataBar>(Bars_ItemsChanged);
137      bars.CollectionReset -= new CollectionItemsChangedEventHandler<DataBar>(Bars_ItemsChanged);
138    }
139
140    private void Bars_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataBar> e) {
141      foreach (var bar in e.Items) {
142        bar.ValuePropertyChanged += new PropertyChangedEventHandler(Bars_ValueChanged);
143        //bar.ThresholdPropertyChanged += new PropertyChangedEventHandler();
144      }
145      OnBarsPropertyChanged();
146    }
147
148    private void Bars_ItemsChanged(object sender, CollectionItemsChangedEventArgs<DataBar> e) {
149      OnBarsPropertyChanged();
150    }
151
152    private void Bars_ValueChanged(object sender, PropertyChangedEventArgs e) {
153      OnBarValueChanged(e);
154    }
155
156    #endregion
157
158    #region helpers
159    public static Dictionary<TKey, TValue> CloneDictionary<TKey, TValue>(Dictionary<TKey, TValue> original) where TValue : ICloneable {
160      Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>(original.Count, original.Comparer);
161
162      foreach (KeyValuePair<TKey, TValue> entry in original) {
163        ret.Add(entry.Key, (TValue)entry.Value.Clone());
164      }
165      return ret;
166    }
167    #endregion
168  }
169}
Note: See TracBrowser for help on using the repository browser.