Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.Parameters.Views/3.3/ValueLookupParameterView.cs @ 17800

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

#2875: Removed years in copyrights

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.ReadOnlyChanged -= new EventHandler(Content_ReadOnlyChanged);
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();
87      Content.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
88      Content.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
89      Content.ReadOnlyChanged += new EventHandler(Content_ReadOnlyChanged);
90      Content.ValueChanged += new EventHandler(Content_ValueChanged);
91    }
92
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
95      if (Content == null) {
96        actualNameTextBox.Text = "-";
97        showInRunCheckBox.Checked = false;
98        valueViewHost.Content = null;
99      } else {
100        SetDataTypeTextBoxText();
101        actualNameTextBox.Text = Content.ActualName;
102        showInRunCheckBox.Checked = Content.GetsCollected;
103        valueViewHost.ViewType = null;
104        valueViewHost.Content = Content.Value;
105      }
106    }
107
108    protected override void SetEnabledStateOfControls() {
109      base.SetEnabledStateOfControls();
110      actualNameTextBox.Enabled = Content != null;
111      actualNameTextBox.ReadOnly = ReadOnly;
112      setValueButton.Enabled = Content != null && !Content.ReadOnly && !ReadOnly;
113      clearValueButton.Enabled = Content != null && !Content.ReadOnly && Content.Value != null && !ReadOnly;
114      showInRunCheckBox.Enabled = Content != null && !ReadOnly;
115    }
116
117    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
118      if (InvokeRequired)
119        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
120      else
121        actualNameTextBox.Text = Content.ActualName;
122    }
123    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_ValueChanged), sender, e);
126      else {
127        SetDataTypeTextBoxText();
128        clearValueButton.Enabled = Content != null && !Content.ReadOnly && Content.Value != null && !ReadOnly;
129        valueViewHost.ViewType = null;
130        valueViewHost.Content = Content != null ? Content.Value : null;
131      }
132    }
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    }
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    }
146
147    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
148      Content.ActualName = actualNameTextBox.Text;
149    }
150    protected virtual void setValueButton_Click(object sender, EventArgs e) {
151      if (typeSelectorDialog == null) {
152        typeSelectorDialog = new TypeSelectorDialog();
153        typeSelectorDialog.Caption = "Select Value";
154        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
155      }
156      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
157        try {
158          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
159        }
160        catch (Exception ex) {
161          ErrorHandling.ShowErrorDialog(this, ex);
162        }
163      }
164    }
165    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
166      Content.Value = null;
167    }
168    protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
169      if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
170    }
171    protected virtual void valueGroupBox_DragEnterOver(object sender, DragEventArgs e) {
172      e.Effect = DragDropEffects.None;
173      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null) && Content.DataType.IsAssignableFrom(e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat).GetType())) {
174        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
175        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
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;
179      }
180    }
181    protected virtual void valueGroupBox_DragDrop(object sender, DragEventArgs e) {
182      if (e.Effect != DragDropEffects.None) {
183        T value = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as T;
184        if (e.Effect.HasFlag(DragDropEffects.Copy)) value = (T)value.Clone();
185        Content.Value = value;
186      }
187    }
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
201  }
202}
Note: See TracBrowser for help on using the repository browser.