Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/DataRow.cs @ 1009

Last change on this file since 1009 was 868, checked in by gkragl, 16 years ago

#320 added constructor for datarow

File size: 5.6 KB
RevLine 
[761]1using System;
2using System.Drawing;
[860]3using System.Collections.Generic;
[761]4
5namespace HeuristicLab.Visualization {
[867]6  public enum Action {
7    Added,
8    Modified,
9    Deleted
10  }
11
[761]12  public delegate void DataRowChangedHandler(IDataRow row);
[867]13  public delegate void ValuesChangedHandler(IDataRow row, double[] values, int index, Action action);
14  public delegate void ValueChangedHandler(IDataRow row, double value, int index, Action action);
[761]15
16  public class DataRow : IDataRow {
17    private string label = "";
18    private Color color = Color.Black;
19    private int thickness = 2;
20    private DrawingStyle style = DrawingStyle.Solid;
[860]21    private List<double> dataRow = new List<double>();
[761]22
[868]23    public DataRow() {
24    }
[867]25
26    public DataRow(string label) {
27      this.Label = label;
28    }
29
30    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
31      this.Label = label;
32      this.Color = color;
33      this.Thickness = thickness;
34      this.Style = style;
35      this.dataRow = dataRow;
36    }
37
[761]38    /// <summary>
39    /// Raised when data row data changed. Should cause redraw in the view.
40    /// </summary>
41    public event DataRowChangedHandler DataRowChanged;
42
43    protected void OnDataRowChanged(IDataRow row) {
44      if (DataRowChanged != null) {
45        DataRowChanged(this);
46      }
47    }
48
49    public event ValuesChangedHandler ValuesChanged;
50
[867]51    protected void OnValuesChanged(double[] values, int index, Action action) {
[761]52      if (ValuesChanged != null) {
[867]53        ValuesChanged(this, values, index, action);
[761]54      }
55    }
56
57    public event ValueChangedHandler ValueChanged;
58
[867]59    protected void OnValueChanged(double value, int index, Action action) {
[761]60      if (ValueChanged != null) {
[867]61        ValueChanged(this, value, index, action);
[761]62      }
63    }
64
65    public string Label {
66      get { return label; }
67      set {
68        label = value;
69        OnDataRowChanged(this);
70      }
71    }
72
73    public Color Color {
74      get { return color; }
75      set {
76        color = value;
77        OnDataRowChanged(this);
78      }
79    }
80
81    public int Thickness {
82      get { return thickness; }
83      set {
84        thickness = value;
85        OnDataRowChanged(this);
86      }
87    }
88
89    public DrawingStyle Style {
90      get { return style; }
91      set {
92        style = value;
93        OnDataRowChanged(this);
94      }
95    }
96
97    public void AddValue(double value) {
[860]98      dataRow.Add(value);
[867]99      OnValueChanged(value, dataRow.Count - 1, Action.Added);
[761]100    }
101
102    public void AddValue(double value, int index) {
[867]103      //check if index is valid
104      if (index >= 0 && index < dataRow.Count) {
105        dataRow.Insert(index, value);
106        OnValueChanged(value, index, Action.Added);
107      } else {
108        throw new System.IndexOutOfRangeException();
109      }   
[761]110    }
111
112    public void AddValues(double[] values) {
[860]113      int startInd = dataRow.Count;
114
115      foreach (double d in values) {
116        dataRow.Add(d);
117      }
[867]118      OnValuesChanged(values, startInd, Action.Added);
[761]119    }
120
121    public void AddValues(double[] values, int index) {
[867]122      int j = index;
123
[860]124      //check if index to start changes is valid
[867]125      if (index >=0 && (index + values.Length) < dataRow.Count) {
[860]126        foreach (double d in values) {
[867]127          dataRow.Insert(j, d);
128          j++;
[860]129        }
[867]130        OnValuesChanged(values, index, Action.Added);
[860]131      } else {
132        throw new System.IndexOutOfRangeException();
133      }
[761]134    }
135
136    public void ModifyValue(double value, int index) {
[867]137      //check if index is valid
138      if (index >= 0 && index < dataRow.Count) {
139        dataRow[index] = value;
140        OnValueChanged(value, index, Action.Modified);
141      } else {
142        throw new System.IndexOutOfRangeException();
143      }
[761]144    }
145
146    public void ModifyValues(double[] values, int index) {
[867]147      int startInd = index;
148      int modInd = index;
149
150      //check if index to start modification is valid
151      if (startInd >=0 && startInd + values.Length < dataRow.Count) {
152        foreach (double d in values) {
153          dataRow[modInd] = d;
154          modInd++;
155        }
156        OnValuesChanged(values, startInd, Action.Modified);
157      } else {
158        throw new System.IndexOutOfRangeException();
159      }
[761]160    }
161
162    public void RemoveValue(int index) {
[867]163      double remVal = dataRow[index];
164      //check if index is valid
165      if (index >= 0 && index < dataRow.Count) {
166        dataRow.RemoveAt(index);
167        OnValueChanged(remVal, index, Action.Deleted);
168      } else {
169        throw new System.IndexOutOfRangeException();
170      }
[761]171    }
172
173    public void RemoveValues(int index, int count) {
[867]174      double[] remValues = new double[count]; //removed values
175      int j = 0;
176
177      //check if count is valid
178      if (count > 0) {
179        //check if index is valid
180        if ((index >= 0) && (index + count <= dataRow.Count)) {
181          for (int i = index; i < (index + count); i++) {
182            remValues.SetValue(i, j);
183            dataRow.RemoveAt(i);
184            j++;
185          }
186          OnValuesChanged(remValues, index, Action.Deleted);
187        } else {
188          throw new System.IndexOutOfRangeException();
189        }
190      } else {
191        throw new System.Exception("parameter count must be > 0!");
192      }
[761]193    }
194
195    public int Count {
[860]196      get { return dataRow.Count; }
[761]197    }
198
199    public double this[int index] {
[860]200      get { return dataRow[index]; }
[761]201      set {
[860]202        dataRow[index] = value;
[867]203        OnValueChanged(value, index, Action.Modified);
[761]204      }
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.