Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding.Views/3.3/ValueConfigurationViews/ValueView.cs @ 8544

Last change on this file since 8544 was 8544, checked in by jkarder, 12 years ago

#1853: ValueView now uses the TypeSelectorDialog instead of the ObjectSelectorDialog

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.Encodings.ParameterConfigurationEncoding.Views {
30  /// <summary>
31  /// The visual representation of a <see cref="ConstrainedValue"/>.
32  /// </summary>
33  [View("Value View")]
34  [Content(typeof(ConstrainedValue), true)]
35  public partial class ValueView : ItemView {
36    /// <summary>
37    /// Gets or sets the variable to represent visually.
38    /// </summary>
39    public new ConstrainedValue Content {
40      get { return (ConstrainedValue)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public ValueView() {
45      InitializeComponent();
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content != null) {
51        valueViewHost.Content = Content.Value;
52      } else {
53        valueViewHost.Content = null;
54      }
55      SetEnabledStateOfControls();
56    }
57
58    protected override void SetEnabledStateOfControls() {
59      base.SetEnabledStateOfControls();
60      if (Content != null) {
61        clearValueButton.Enabled = Content.Value != null && Content.IsNullable; // in this case IsNullable is not set correctly
62        setValueButton.Enabled = Content.ValueDataType != null;
63      }
64    }
65
66    private void setValueButton_Click(object sender, EventArgs e) {
67      // TODO: valid values get lost if the value was set to be optimized before
68      var withoutNullValue = Content.ValidValues.Where(x => x != null && !(x is NullValue));
69      var typeSelectorDialog = new TypeSelectorDialog();
70      typeSelectorDialog.Caption = "Select Item";
71      typeSelectorDialog.TypeSelector.Caption = "Available Items";
72      typeSelectorDialog.TypeSelector.Configure(withoutNullValue.Select(x => x.GetType()), false, false, false);
73      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
74        try {
75          Content.Value = withoutNullValue.Single(x => x.GetType() == typeSelectorDialog.TypeSelector.SelectedType);
76          valueViewHost.Content = Content.Value;
77        }
78        catch (Exception ex) {
79          ErrorHandling.ShowErrorDialog(this, ex);
80        }
81      }
82      SetEnabledStateOfControls();
83    }
84
85    private void clearValueButton_Click(object sender, EventArgs e) {
86      Content.Value = null;
87      valueViewHost.Content = null;
88      SetEnabledStateOfControls();
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.