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

Last change on this file since 14467 was 14185, checked in by swagner, 5 years ago

#2526: Updated year of copyrights in license headers

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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        this.cbAttr.SelectedItem = this.cbAttr.Items[Content.ConstraintColumn];
75        cbAttr.SelectedItem = Content.ConstraintColumn;
76        if (Content.ConstraintData != null)
77          tbFilterData.Text = Content.ConstraintData.GetValue();
78        else
79          this.Content_ConstraintColumnChanged(cbAttr, EventArgs.Empty);
80      }
81    }
82
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      this.Content.ActiveChanged += new EventHandler(Content_ActiveChanged);
86      this.Content.ConstraintOperationChanged += new EventHandler(Content_ComparisonOperationChanged);
87      this.Content.ConstraintColumnChanged += new EventHandler(Content_ConstraintColumnChanged);
88      this.Content.ConstrainedValueChanged += new EventHandler(Content_ConstrainedValueChanged);
89      this.Content.ConstraintDataChanged += new EventHandler(Content_ConstrainedDataChanged);
90    }
91
92    protected override void DeregisterContentEvents() {
93      base.DeregisterContentEvents();
94      this.Content.ActiveChanged -= new EventHandler(Content_ActiveChanged);
95      this.Content.ConstraintOperationChanged -= new EventHandler(Content_ComparisonOperationChanged);
96      this.Content.ConstraintColumnChanged -= new EventHandler(Content_ConstraintColumnChanged);
97      this.Content.ConstrainedValueChanged -= new EventHandler(Content_ConstrainedValueChanged);
98      this.Content.ConstraintDataChanged -= new EventHandler(Content_ConstrainedDataChanged);
99    }
100
101    protected virtual void Content_ConstrainedValueChanged(object sender, EventArgs e) {
102      this.UpdateColumnComboBox();
103    }
104
105    private void Content_ConstrainedDataChanged(object sender, EventArgs e) {
106      if (Content.ConstraintData != null)
107        tbFilterData.Text = Content.ConstraintData.GetValue();
108      else
109        tbFilterData.Text = string.Empty;
110    }
111
112    protected virtual void Content_ConstraintColumnChanged(object sender, EventArgs e) {
113      if (Content.ConstrainedValue != null) {
114        if (cbAttr.Items.IndexOf(cbAttr.SelectedItem) != Content.ConstraintColumn) {
115          cbAttr.SelectedItem = this.cbAttr.Items[Content.ConstraintColumn];
116        }
117      }
118      if (Content.ConstraintData == null) {
119        this.Content.ConstraintData = CreateStringConvertibleValue(cbAttr.SelectedIndex);
120      }
121    }
122
123
124    protected virtual void Content_ComparisonOperationChanged(object sender, EventArgs e) {
125      if (Content.ConstraintOperation != (ConstraintOperation)this.cbFilterOperation.SelectedItem)
126        this.cbFilterOperation.SelectedItem = Content.ConstraintOperation;
127    }
128
129    protected override void SetEnabledStateOfControls() {
130      base.SetEnabledStateOfControls();
131      cbAttr.Enabled = Content != null && !Content.Active;
132      cbFilterOperation.Enabled = Content != null && !Content.Active;
133      tbFilterData.Enabled = Content != null && !Content.Active;
134    }
135
136    private void cbAttr_SelectedIndexChanged(object sender, EventArgs e) {
137      if (Content.ConstrainedValue != null) {
138        Content.ConstraintColumn = Content.ConstrainedValue.GetColumnIndex(cbAttr.SelectedItem.ToString());
139      }
140    }
141
142    private void cbFilterOperation_SelectedIndexChanged(object sender, EventArgs e) {
143      Content.ConstraintOperation = (ConstraintOperation)this.cbFilterOperation.SelectedItem;
144    }
145
146    protected virtual void Content_ActiveChanged(object sender, EventArgs e) {
147      this.ReadOnly = !Content.Active;
148      SetEnabledStateOfControls();
149      Refresh(); ResumeRepaint(true);
150    }
151
152    private void tbFilterData_Validated(object sender, EventArgs e) {
153      IStringConvertibleValue value = CreateStringConvertibleValue(cbAttr.SelectedIndex);
154      value.SetValue(tbFilterData.Text);
155      Content.ConstraintData = value;
156    }
157
158    private IStringConvertibleValue CreateStringConvertibleValue(int columnIndex) {
159      IStringConvertibleValue value;
160      if (Content.ConstrainedValue.VariableHasType<double>(columnIndex)) {
161        value = new DoubleValue();
162      } else if (Content.ConstrainedValue.VariableHasType<String>(columnIndex)) {
163        value = new StringValue();
164      } else if (Content.ConstrainedValue.VariableHasType<DateTime>(columnIndex)) {
165        value = new DateTimeValue();
166      } else {
167        throw new ArgumentException("unsupported type");
168      }
169      return value;
170    }
171
172    private void tbFilterData_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
173      string errorMessage = string.Empty;
174      if (!Content.ConstraintData.Validate(tbFilterData.Text, out errorMessage)) {
175        errorProvider.SetError(tbFilterData, errorMessage);
176        e.Cancel = true;
177      } else
178        errorProvider.Clear();
179    }
180
181  }
182}
Note: See TracBrowser for help on using the repository browser.