Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Parameters.Views/3.3/ValueParameterView.cs @ 17842

Last change on this file since 17842 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 8.2 KB
RevLine 
[2740]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2740]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.Windows.Forms;
[4257]24using HeuristicLab.Common;
[2740]25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
[3758]28using HeuristicLab.PluginInfrastructure;
[2740]29
30namespace HeuristicLab.Parameters.Views {
31  /// <summary>
32  /// The visual representation of a <see cref="Parameter"/>.
33  /// </summary>
[2917]34  [View("ValueParameter View")]
[2948]35  [Content(typeof(OptionalValueParameter<>), true)]
[2891]36  [Content(typeof(ValueParameter<>), true)]
[2948]37  [Content(typeof(IValueParameter<>), false)]
[2756]38  public partial class ValueParameterView<T> : ParameterView where T : class, IItem {
[2948]39    protected TypeSelectorDialog typeSelectorDialog;
40
[2740]41    /// <summary>
42    /// Gets or sets the variable to represent visually.
43    /// </summary>
44    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
45    /// No own data storage present.</remarks>
[2948]46    public new IValueParameter<T> Content {
47      get { return (IValueParameter<T>)base.Content; }
[2740]48      set { base.Content = value; }
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
53    /// </summary>
[2756]54    public ValueParameterView() {
[2740]55      InitializeComponent();
56    }
57
[5237]58    /// <summary>
59    /// Clean up any resources being used.
60    /// </summary>
61    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
62    protected override void Dispose(bool disposing) {
63      if (disposing) {
64        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
65        if (components != null) components.Dispose();
66      }
67      base.Dispose(disposing);
68    }
69
[2740]70    /// <summary>
71    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
72    /// </summary>
73    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
74    protected override void DeregisterContentEvents() {
[4332]75      Content.GetsCollectedChanged -= new EventHandler(Content_GetsCollectedChanged);
[16872]76      Content.ReadOnlyChanged -= new EventHandler(Content_ReadOnlyChanged);
[2740]77      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
78      base.DeregisterContentEvents();
79    }
80
81    /// <summary>
82    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
83    /// </summary>
84    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
85    protected override void RegisterContentEvents() {
86      base.RegisterContentEvents();
[4332]87      Content.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
[16872]88      Content.ReadOnlyChanged += new EventHandler(Content_ReadOnlyChanged);
[2740]89      Content.ValueChanged += new EventHandler(Content_ValueChanged);
90    }
91
92    protected override void OnContentChanged() {
93      base.OnContentChanged();
94      if (Content == null) {
[4332]95        showInRunCheckBox.Checked = false;
[4011]96        valueViewHost.Content = null;
[2740]97      } else {
[4257]98        SetDataTypeTextBoxText();
[4332]99        showInRunCheckBox.Checked = Content.GetsCollected;
[4011]100        valueViewHost.ViewType = null;
101        valueViewHost.Content = Content.Value;
[2740]102      }
103    }
104
[3904]105    protected override void SetEnabledStateOfControls() {
106      base.SetEnabledStateOfControls();
[16872]107      setValueButton.Enabled = Content != null && !Content.ReadOnly && !(Content is IFixedValueParameter) && !ReadOnly;
108      clearValueButton.Enabled = Content != null && !Content.ReadOnly && Content.Value != null && !(Content is IFixedValueParameter) && !(Content is ValueParameter<T>) && !ReadOnly;
[4332]109      showInRunCheckBox.Enabled = Content != null && !ReadOnly;
[3365]110    }
111
[2740]112    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
113      if (InvokeRequired)
114        Invoke(new EventHandler(Content_ValueChanged), sender, e);
115      else {
[4257]116        SetDataTypeTextBoxText();
[16872]117        setValueButton.Enabled = Content != null && !Content.ReadOnly && !(Content is IFixedValueParameter) && !ReadOnly;
118        clearValueButton.Enabled = Content != null && !Content.ReadOnly && Content.Value != null && !(Content is IFixedValueParameter<T>) && !(Content is ValueParameter<T>) && !ReadOnly;
[4011]119        valueViewHost.ViewType = null;
[4257]120        valueViewHost.Content = Content != null ? Content.Value : null;
[2740]121      }
122    }
[16872]123
124    protected virtual void Content_ReadOnlyChanged(object sender, EventArgs e) {
125      if (InvokeRequired)
126        Invoke(new EventHandler(Content_ReadOnlyChanged), sender, e);
127      else {
128        SetEnabledStateOfControls();
129      }
130    }
[4332]131    protected virtual void Content_GetsCollectedChanged(object sender, EventArgs e) {
132      if (InvokeRequired)
133        Invoke(new EventHandler(Content_GetsCollectedChanged), sender, e);
134      else
135        showInRunCheckBox.Checked = Content != null && Content.GetsCollected;
136    }
[2740]137
[4332]138    protected virtual void setValueButton_Click(object sender, EventArgs e) {
[2948]139      if (typeSelectorDialog == null) {
140        typeSelectorDialog = new TypeSelectorDialog();
141        typeSelectorDialog.Caption = "Select Value";
[3588]142        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
[2948]143      }
[3407]144      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
145        try {
146          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[16872]147        } catch (Exception ex) {
[3758]148          ErrorHandling.ShowErrorDialog(this, ex);
[3407]149        }
150      }
[2948]151    }
[4332]152    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
[2948]153      Content.Value = null;
154    }
[4332]155    protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
156      if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
157    }
[4522]158    protected virtual void valueGroupBox_DragEnterOver(object sender, DragEventArgs e) {
[2740]159      e.Effect = DragDropEffects.None;
[5809]160      if (Content is IFixedValueParameter) return;
[5837]161      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null) && Content.DataType.IsAssignableFrom(e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat).GetType())) {
[3694]162        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2740]163        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]164        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
165        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
166        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[2740]167      }
168    }
[4522]169    protected virtual void valueGroupBox_DragDrop(object sender, DragEventArgs e) {
[2740]170      if (e.Effect != DragDropEffects.None) {
[5837]171        T value = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as T;
[5744]172        if (e.Effect.HasFlag(DragDropEffects.Copy)) value = (T)value.Clone();
[2740]173        Content.Value = value;
174      }
175    }
[4257]176
177    #region Helpers
178    protected void SetDataTypeTextBoxText() {
179      if (Content == null) {
180        dataTypeTextBox.Text = "-";
181      } else {
182        if ((Content.Value != null) && (Content.Value.GetType() != Content.DataType))
183          dataTypeTextBox.Text = Content.DataType.GetPrettyName() + " (" + Content.Value.GetType().GetPrettyName() + ")";
184        else
185          dataTypeTextBox.Text = Content.DataType.GetPrettyName();
186      }
187    }
188    #endregion
[2740]189  }
190}
Note: See TracBrowser for help on using the repository browser.