Free cookie consent management tool by TermsFeed Policy Generator

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

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

#320 added constructor for datarow

File size: 5.6 KB
Line 
1using System;
2using System.Drawing;
3using System.Collections.Generic;
4
5namespace HeuristicLab.Visualization {
6  public enum Action {
7    Added,
8    Modified,
9    Deleted
10  }
11
12  public delegate void DataRowChangedHandler(IDataRow row);
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);
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;
21    private List<double> dataRow = new List<double>();
22
23    public DataRow() {
24    }
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
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
51    protected void OnValuesChanged(double[] values, int index, Action action) {
52      if (ValuesChanged != null) {
53        ValuesChanged(this, values, index, action);
54      }
55    }
56
57    public event ValueChangedHandler ValueChanged;
58
59    protected void OnValueChanged(double value, int index, Action action) {
60      if (ValueChanged != null) {
61        ValueChanged(this, value, index, action);
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) {
98      dataRow.Add(value);
99      OnValueChanged(value, dataRow.Count - 1, Action.Added);
100    }
101
102    public void AddValue(double value, int index) {
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      }   
110    }
111
112    public void AddValues(double[] values) {
113      int startInd = dataRow.Count;
114
115      foreach (double d in values) {
116        dataRow.Add(d);
117      }
118      OnValuesChanged(values, startInd, Action.Added);
119    }
120
121    public void AddValues(double[] values, int index) {
122      int j = index;
123
124      //check if index to start changes is valid
125      if (index >=0 && (index + values.Length) < dataRow.Count) {
126        foreach (double d in values) {
127          dataRow.Insert(j, d);
128          j++;
129        }
130        OnValuesChanged(values, index, Action.Added);
131      } else {
132        throw new System.IndexOutOfRangeException();
133      }
134    }
135
136    public void ModifyValue(double value, int index) {
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      }
144    }
145
146    public void ModifyValues(double[] values, int index) {
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      }
160    }
161
162    public void RemoveValue(int index) {
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      }
171    }
172
173    public void RemoveValues(int index, int count) {
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      }
193    }
194
195    public int Count {
196      get { return dataRow.Count; }
197    }
198
199    public double this[int index] {
200      get { return dataRow[index]; }
201      set {
202        dataRow[index] = value;
203        OnValueChanged(value, index, Action.Modified);
204      }
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.