Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2898_GeneralizedAdditiveModels/HeuristicLab.Parameters.Views/3.3/ValueLookupParameterView.cs @ 16755

Last change on this file since 16755 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Parameters.Views {
31  /// <summary>
32  /// The visual representation of a <see cref="Parameter"/>.
33  /// </summary>
34  [View("ValueLookupParameter View")]
35  [Content(typeof(ValueLookupParameter<>), true)]
36  [Content(typeof(IValueLookupParameter<>), false)]
37  public partial class ValueLookupParameterView<T> : ParameterView where T : class, IItem {
38    protected TypeSelectorDialog typeSelectorDialog;
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>
45    public new IValueLookupParameter<T> Content {
46      get { return (IValueLookupParameter<T>)base.Content; }
47      set { base.Content = value; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
52    /// </summary>
53    public ValueLookupParameterView() {
54      InitializeComponent();
55    }
56
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
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>
73    protected override void DeregisterContentEvents() {
74      Content.ActualNameChanged -= new EventHandler(Content_ActualNameChanged);
75      Content.GetsCollectedChanged -= new EventHandler(Content_GetsCollectedChanged);
76      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
77      base.DeregisterContentEvents();
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>
84    protected override void RegisterContentEvents() {
85      base.RegisterContentEvents();
86      Content.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
87      Content.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
88      Content.ValueChanged += new EventHandler(Content_ValueChanged);
89    }
90
91    protected override void OnContentChanged() {
92      base.OnContentChanged();
93      if (Content == null) {
94        actualNameTextBox.Text = "-";
95        showInRunCheckBox.Checked = false;
96        valueViewHost.Content = null;
97      } else {
98        SetDataTypeTextBoxText();
99        actualNameTextBox.Text = Content.ActualName;
100        showInRunCheckBox.Checked = Content.GetsCollected;
101        valueViewHost.ViewType = null;
102        valueViewHost.Content = Content.Value;
103      }
104    }
105
106    protected override void SetEnabledStateOfControls() {
107      base.SetEnabledStateOfControls();
108      actualNameTextBox.Enabled = Content != null;
109      actualNameTextBox.ReadOnly = ReadOnly;
110      setValueButton.Enabled = Content != null && !ReadOnly;
111      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
112      showInRunCheckBox.Enabled = Content != null && !ReadOnly;
113    }
114
115    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
116      if (InvokeRequired)
117        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
118      else
119        actualNameTextBox.Text = Content.ActualName;
120    }
121    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
122      if (InvokeRequired)
123        Invoke(new EventHandler(Content_ValueChanged), sender, e);
124      else {
125        SetDataTypeTextBoxText();
126        clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
127        valueViewHost.ViewType = null;
128        valueViewHost.Content = Content != null ? Content.Value : null;
129      }
130    }
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    }
137
138    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
139      Content.ActualName = actualNameTextBox.Text;
140    }
141    protected virtual void setValueButton_Click(object sender, EventArgs e) {
142      if (typeSelectorDialog == null) {
143        typeSelectorDialog = new TypeSelectorDialog();
144        typeSelectorDialog.Caption = "Select Value";
145        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
146      }
147      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
148        try {
149          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
150        }
151        catch (Exception ex) {
152          ErrorHandling.ShowErrorDialog(this, ex);
153        }
154      }
155    }
156    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
157      Content.Value = null;
158    }
159    protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
160      if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
161    }
162    protected virtual void valueGroupBox_DragEnterOver(object sender, DragEventArgs e) {
163      e.Effect = DragDropEffects.None;
164      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null) && Content.DataType.IsAssignableFrom(e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat).GetType())) {
165        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
166        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
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;
170      }
171    }
172    protected virtual void valueGroupBox_DragDrop(object sender, DragEventArgs e) {
173      if (e.Effect != DragDropEffects.None) {
174        T value = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as T;
175        if (e.Effect.HasFlag(DragDropEffects.Copy)) value = (T)value.Clone();
176        Content.Value = value;
177      }
178    }
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
192  }
193}
Note: See TracBrowser for help on using the repository browser.