source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/ComparisonFilterView.cs @ 10947

Last change on this file since 10947 was 10947, checked in by rstoll, 8 years ago
  • Changed ComparisonFilter, using DoubleValue and DateTime rather than just StringValue
  • Included better input validation for ComparisonFilterView
  • Bug "Search and Sorted DataGridContentView" fixed
File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Core.Views;
26using HeuristicLab.Data;
27using HeuristicLab.DataPreprocessing.Filter;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.DataPreprocessing.Views {
31  [View("Comparison Filter View")]
32  [Content(typeof(ComparisonFilter), false)]
33  public partial class ComparisonFilterView : ItemView {
34    public ComparisonFilterView() {
35      InitializeComponent();
36    }
37
38    public new ComparisonFilter Content {
39      get { return (ComparisonFilter)base.Content; }
40      set { base.Content = value; }
41    }
42
43
44    protected override void OnContentChanged() {
45      base.OnContentChanged();
46      cbAttr.Items.Clear(); //cmbConstraintColumn.Items.Clear();
47      cbFilterOperation.Items.Clear(); //cmbConstraintOperation.Items.Clear();
48      tbFilterData.Text = string.Empty;
49      if (Content != null) {
50        cbFilterOperation.Items.AddRange(Content.AllowedConstraintOperations.ToArray());
51        if (Content.ConstraintOperation != null)
52          cbFilterOperation.SelectedItem = Content.ConstraintOperation;
53        else if (cbFilterOperation.Items.Count != 0)
54          cbFilterOperation.SelectedIndex = 0;
55        UpdateColumnComboBox();
56        ReadOnly = Content.Active;
57        if (Content.ConstraintData != null) {
58          tbFilterData.Text = Content.ConstraintData.GetValue();
59        } else {
60          this.Content_ConstraintColumnChanged(cbAttr, EventArgs.Empty); // TODO
61        }
62      }
63      if (Content == null || Content.ConstraintData == null) {
64        tbFilterData.Text = string.Empty;
65      } else {
66        tbFilterData.Text = Content.ConstraintData.GetValue();
67      }
68    }
69
70    protected virtual void UpdateColumnComboBox() {
71      this.cbAttr.Items.Clear();
72      if (Content.ConstrainedValue != null) {
73        this.cbAttr.Items.AddRange(Content.ConstrainedValue.VariableNames.ToArray<string>());
74        //if (!string.IsNullOrEmpty(Content.ConstraintColumn))
75        this.cbAttr.SelectedItem = this.cbAttr.Items[Content.ConstraintColumn];
76
77        //if (Content.ConstraintColumn != null)
78        //{
79        cbAttr.SelectedItem = Content.ConstraintColumn;
80        if (Content.ConstraintData != null)
81          tbFilterData.Text = Content.ConstraintData.GetValue();
82        else
83          this.Content_ConstraintColumnChanged(cbAttr, EventArgs.Empty);
84        //}
85      }
86    }
87
88    protected override void RegisterContentEvents() {
89      base.RegisterContentEvents();
90      this.Content.ActiveChanged += new EventHandler(Content_ActiveChanged);
91      this.Content.ConstraintOperationChanged += new EventHandler(Content_ComparisonOperationChanged);
92      this.Content.ConstraintColumnChanged += new EventHandler(Content_ConstraintColumnChanged);
93      this.Content.ConstrainedValueChanged += new EventHandler(Content_ConstrainedValueChanged);
94      this.Content.ConstraintDataChanged += new EventHandler(Content_ConstrainedDataChanged);
95    }
96
97    protected override void DeregisterContentEvents() {
98      base.DeregisterContentEvents();
99      this.Content.ActiveChanged -= new EventHandler(Content_ActiveChanged);
100      this.Content.ConstraintOperationChanged -= new EventHandler(Content_ComparisonOperationChanged);
101      this.Content.ConstraintColumnChanged -= new EventHandler(Content_ConstraintColumnChanged);
102      this.Content.ConstrainedValueChanged -= new EventHandler(Content_ConstrainedValueChanged);
103      this.Content.ConstraintDataChanged -= new EventHandler(Content_ConstrainedDataChanged);
104    }
105
106    protected virtual void Content_ConstrainedValueChanged(object sender, EventArgs e) {
107      this.UpdateColumnComboBox();
108    }
109
110    private void Content_ConstrainedDataChanged(object sender, EventArgs e) {
111      if (Content.ConstraintData != null)
112        tbFilterData.Text = Content.ConstraintData.GetValue();
113      else
114        tbFilterData.Text = string.Empty;
115    }
116
117    protected virtual void Content_ConstraintColumnChanged(object sender, EventArgs e) {
118      if (Content.ConstrainedValue != null) {
119        if (cbAttr.Items.IndexOf(cbAttr.SelectedItem) != Content.ConstraintColumn) {
120          cbAttr.SelectedItem = this.cbAttr.Items[Content.ConstraintColumn];
121        }
122      }
123      if (Content.ConstraintData == null) {
124        this.Content.ConstraintData = CreateStringConvertibleValue(cbAttr.SelectedIndex);
125      }
126    }
127
128
129    protected virtual void Content_ComparisonOperationChanged(object sender, EventArgs e) {
130      if (Content.ConstraintOperation != (ConstraintOperation)this.cbFilterOperation.SelectedItem)
131        this.cbFilterOperation.SelectedItem = Content.ConstraintOperation;
132    }
133
134    protected override void SetEnabledStateOfControls() {
135      base.SetEnabledStateOfControls();
136      /*
137      cbAttr.Enabled = !this.ReadOnly && Content != null;
138      cbFilterOperation.Enabled = !this.ReadOnly && Content != null;
139      tbFilterData.Enabled = Content != null;
140      tbFilterData.ReadOnly = ReadOnly;
141       * */
142      cbAttr.Enabled = Content != null && !Content.Active;
143      cbFilterOperation.Enabled = Content != null && !Content.Active;
144      tbFilterData.Enabled = Content != null && !Content.Active;
145    }
146
147    private void cbAttr_SelectedIndexChanged(object sender, EventArgs e) {
148      if (Content.ConstrainedValue != null) {
149        Content.ConstraintColumn = Content.ConstrainedValue.GetColumnIndex(cbAttr.SelectedItem.ToString());
150      }
151    }
152
153    private void cbFilterOperation_SelectedIndexChanged(object sender, EventArgs e) {
154      Content.ConstraintOperation = (ConstraintOperation)this.cbFilterOperation.SelectedItem;
155    }
156
157    protected virtual void Content_ActiveChanged(object sender, EventArgs e) {
158      this.ReadOnly = !Content.Active;
159      SetEnabledStateOfControls();
160      Refresh(); ResumeRepaint(true);
161    }
162
163    private void tbFilterData_Validated(object sender, EventArgs e) {
164      IStringConvertibleValue value = CreateStringConvertibleValue(cbAttr.SelectedIndex);
165      value.SetValue(tbFilterData.Text);
166      Content.ConstraintData = value;
167    }
168
169    private IStringConvertibleValue CreateStringConvertibleValue(int columnIndex) {
170      IStringConvertibleValue value;
171      if (Content.ConstrainedValue.IsType<double>(columnIndex)) {
172        value = new DoubleValue();
173      } else if (Content.ConstrainedValue.IsType<String>(columnIndex)) {
174        value = new StringValue();
175      } else if (Content.ConstrainedValue.IsType<DateTime>(columnIndex)) {
176        value = new DateTimeValue();
177      } else {
178        throw new ArgumentException("unsupported type");
179      }
180      return value;
181    }
182
183    private void tbFilterData_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
184      string errorMessage = string.Empty;
185      if (!Content.ConstraintData.Validate(tbFilterData.Text, out errorMessage)) {
186        errorProvider.SetError(tbFilterData, errorMessage);
187        e.Cancel = true;
188      } else
189        errorProvider.Clear();
190    }
191
192  }
193}
Note: See TracBrowser for help on using the repository browser.