Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Parameters.Views/3.3/ValueLookupParameterView.cs @ 17149

Last change on this file since 17149 was 17149, checked in by abeham, 5 years ago

#3005: merged to stable (16872, 16873, 16875, 16890)

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