Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.DataPreprocessing.Views/3.4/ComparisonFilterView.cs @ 16662

Last change on this file since 16662 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 new ComparisonFilter Content {
35      get { return (ComparisonFilter)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public ComparisonFilterView() {
40      InitializeComponent();
41    }
42
43    protected override void OnContentChanged() {
44      base.OnContentChanged();
45      cbAttr.Items.Clear();
46      cbFilterOperation.Items.Clear();
47      tbFilterData.Text = string.Empty;
48      if (Content != null) {
49        cbFilterOperation.Items.AddRange(Content.AllowedConstraintOperations.ToArray());
50        if (Content.ConstraintOperation != null)
51          cbFilterOperation.SelectedItem = Content.ConstraintOperation;
52        else if (cbFilterOperation.Items.Count != 0)
53          cbFilterOperation.SelectedIndex = 0;
54        UpdateColumnComboBox();
55        ReadOnly = Content.Active;
56        if (Content.ConstraintData != null) {
57          tbFilterData.Text = Content.ConstraintData.GetValue();
58        } else {
59          this.Content_ConstraintColumnChanged(cbAttr, EventArgs.Empty); // TODO
60        }
61      }
62      if (Content == null || Content.ConstraintData == null) {
63        tbFilterData.Text = string.Empty;
64      } else {
65        tbFilterData.Text = Content.ConstraintData.GetValue();
66      }
67    }
68
69    protected virtual void UpdateColumnComboBox() {
70      this.cbAttr.Items.Clear();
71      if (Content.ConstrainedValue != null) {
72        this.cbAttr.Items.AddRange(Content.ConstrainedValue.VariableNames.ToArray<string>());
73        this.cbAttr.SelectedItem = this.cbAttr.Items[Content.ConstraintColumn];
74        cbAttr.SelectedItem = Content.ConstraintColumn;
75        if (Content.ConstraintData != null)
76          tbFilterData.Text = Content.ConstraintData.GetValue();
77        else
78          this.Content_ConstraintColumnChanged(cbAttr, EventArgs.Empty);
79      }
80    }
81
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      this.Content.ActiveChanged += new EventHandler(Content_ActiveChanged);
85      this.Content.ConstraintOperationChanged += new EventHandler(Content_ComparisonOperationChanged);
86      this.Content.ConstraintColumnChanged += new EventHandler(Content_ConstraintColumnChanged);
87      this.Content.ConstrainedValueChanged += new EventHandler(Content_ConstrainedValueChanged);
88      this.Content.ConstraintDataChanged += new EventHandler(Content_ConstrainedDataChanged);
89    }
90
91    protected override void DeregisterContentEvents() {
92      base.DeregisterContentEvents();
93      this.Content.ActiveChanged -= new EventHandler(Content_ActiveChanged);
94      this.Content.ConstraintOperationChanged -= new EventHandler(Content_ComparisonOperationChanged);
95      this.Content.ConstraintColumnChanged -= new EventHandler(Content_ConstraintColumnChanged);
96      this.Content.ConstrainedValueChanged -= new EventHandler(Content_ConstrainedValueChanged);
97      this.Content.ConstraintDataChanged -= new EventHandler(Content_ConstrainedDataChanged);
98    }
99
100    protected virtual void Content_ConstrainedValueChanged(object sender, EventArgs e) {
101      this.UpdateColumnComboBox();
102    }
103
104    private void Content_ConstrainedDataChanged(object sender, EventArgs e) {
105      if (Content.ConstraintData != null)
106        tbFilterData.Text = Content.ConstraintData.GetValue();
107      else
108        tbFilterData.Text = string.Empty;
109    }
110
111    protected virtual void Content_ConstraintColumnChanged(object sender, EventArgs e) {
112      if (Content.ConstrainedValue != null) {
113        if (cbAttr.Items.IndexOf(cbAttr.SelectedItem) != Content.ConstraintColumn) {
114          cbAttr.SelectedItem = this.cbAttr.Items[Content.ConstraintColumn];
115        }
116      }
117      if (Content.ConstraintData == null) {
118        this.Content.ConstraintData = CreateStringConvertibleValue(cbAttr.SelectedIndex);
119      }
120    }
121
122    protected virtual void Content_ComparisonOperationChanged(object sender, EventArgs e) {
123      if (Content.ConstraintOperation != (ConstraintOperation)this.cbFilterOperation.SelectedItem)
124        this.cbFilterOperation.SelectedItem = Content.ConstraintOperation;
125    }
126
127    protected override void SetEnabledStateOfControls() {
128      base.SetEnabledStateOfControls();
129      cbAttr.Enabled = Content != null && !Content.Active;
130      cbFilterOperation.Enabled = Content != null && !Content.Active;
131      tbFilterData.Enabled = Content != null && !Content.Active;
132    }
133
134    private void cbAttr_SelectedIndexChanged(object sender, EventArgs e) {
135      if (Content.ConstrainedValue != null) {
136        Content.ConstraintColumn = Content.ConstrainedValue.GetColumnIndex(cbAttr.SelectedItem.ToString());
137      }
138    }
139
140    private void cbFilterOperation_SelectedIndexChanged(object sender, EventArgs e) {
141      Content.ConstraintOperation = (ConstraintOperation)this.cbFilterOperation.SelectedItem;
142    }
143
144    protected virtual void Content_ActiveChanged(object sender, EventArgs e) {
145      this.ReadOnly = !Content.Active;
146      SetEnabledStateOfControls();
147      Refresh(); ResumeRepaint(true);
148    }
149
150    private void tbFilterData_Validated(object sender, EventArgs e) {
151      IStringConvertibleValue value = CreateStringConvertibleValue(cbAttr.SelectedIndex);
152      value.SetValue(tbFilterData.Text);
153      Content.ConstraintData = value;
154    }
155
156    private IStringConvertibleValue CreateStringConvertibleValue(int columnIndex) {
157      IStringConvertibleValue value;
158      if (Content.ConstrainedValue.VariableHasType<double>(columnIndex)) {
159        value = new DoubleValue();
160      } else if (Content.ConstrainedValue.VariableHasType<String>(columnIndex)) {
161        value = new StringValue();
162      } else if (Content.ConstrainedValue.VariableHasType<DateTime>(columnIndex)) {
163        value = new DateTimeValue();
164      } else {
165        throw new ArgumentException("unsupported type");
166      }
167      return value;
168    }
169
170    private void tbFilterData_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
171      string errorMessage = string.Empty;
172      if (!Content.ConstraintData.Validate(tbFilterData.Text, out errorMessage)) {
173        errorProvider.SetError(tbFilterData, errorMessage);
174        e.Cancel = true;
175      } else
176        errorProvider.Clear();
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.