Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters.Views/3.3/ValueLookupParameterView.cs @ 15377

Last change on this file since 15377 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 8.1 KB
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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);
[2756]76      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
[2713]77      base.DeregisterContentEvents();
[2655]78    }
79
80    /// <summary>
81    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
82    /// </summary>
83    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]84    protected override void RegisterContentEvents() {
85      base.RegisterContentEvents();
86      Content.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
[4332]87      Content.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
[2756]88      Content.ValueChanged += new EventHandler(Content_ValueChanged);
[2655]89    }
90
[2713]91    protected override void OnContentChanged() {
92      base.OnContentChanged();
93      if (Content == null) {
[2655]94        actualNameTextBox.Text = "-";
[4332]95        showInRunCheckBox.Checked = false;
[4011]96        valueViewHost.Content = null;
[2655]97      } else {
[4257]98        SetDataTypeTextBoxText();
[2713]99        actualNameTextBox.Text = Content.ActualName;
[4332]100        showInRunCheckBox.Checked = Content.GetsCollected;
[4011]101        valueViewHost.ViewType = null;
102        valueViewHost.Content = Content.Value;
[2655]103      }
104    }
105
[3904]106    protected override void SetEnabledStateOfControls() {
107      base.SetEnabledStateOfControls();
[3365]108      actualNameTextBox.Enabled = Content != null;
109      actualNameTextBox.ReadOnly = ReadOnly;
110      setValueButton.Enabled = Content != null && !ReadOnly;
111      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
[4332]112      showInRunCheckBox.Enabled = Content != null && !ReadOnly;
[3365]113    }
114
[2713]115    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
[2655]116      if (InvokeRequired)
[2713]117        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
[2655]118      else
[2713]119        actualNameTextBox.Text = Content.ActualName;
[2655]120    }
[2756]121    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
[2655]122      if (InvokeRequired)
[2756]123        Invoke(new EventHandler(Content_ValueChanged), sender, e);
[2655]124      else {
[4257]125        SetDataTypeTextBoxText();
126        clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
[4011]127        valueViewHost.ViewType = null;
[4257]128        valueViewHost.Content = Content != null ? Content.Value : null;
[2655]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    }
[2655]137
[2676]138    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
[2713]139      Content.ActualName = actualNameTextBox.Text;
[2655]140    }
[2756]141    protected virtual void setValueButton_Click(object sender, EventArgs e) {
[2655]142      if (typeSelectorDialog == null) {
143        typeSelectorDialog = new TypeSelectorDialog();
[2756]144        typeSelectorDialog.Caption = "Select Value";
[3588]145        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
[2655]146      }
[3407]147      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
148        try {
149          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
150        }
151        catch (Exception ex) {
[3758]152          ErrorHandling.ShowErrorDialog(this, ex);
[3407]153        }
154      }
[2655]155    }
[2756]156    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
157      Content.Value = null;
[2655]158    }
[4332]159    protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
160      if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
161    }
[4522]162    protected virtual void valueGroupBox_DragEnterOver(object sender, DragEventArgs e) {
[2655]163      e.Effect = DragDropEffects.None;
[5837]164      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null) && Content.DataType.IsAssignableFrom(e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat).GetType())) {
[3694]165        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2694]166        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]167        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
168        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
169        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[2655]170      }
171    }
[4522]172    protected virtual void valueGroupBox_DragDrop(object sender, DragEventArgs e) {
[2655]173      if (e.Effect != DragDropEffects.None) {
[5837]174        T value = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as T;
[5744]175        if (e.Effect.HasFlag(DragDropEffects.Copy)) value = (T)value.Clone();
[2756]176        Content.Value = value;
[2655]177      }
178    }
[4257]179
180    #region Helpers
181    protected void SetDataTypeTextBoxText() {
182      if (Content == null) {
183        dataTypeTextBox.Text = "-";
184      } else {
185        if ((Content.Value != null) && (Content.Value.GetType() != Content.DataType))
186          dataTypeTextBox.Text = Content.DataType.GetPrettyName() + " (" + Content.Value.GetType().GetPrettyName() + ")";
187        else
188          dataTypeTextBox.Text = Content.DataType.GetPrettyName();
189      }
190    }
191    #endregion
[2655]192  }
193}
Note: See TracBrowser for help on using the repository browser.