Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4332 was 4332, checked in by swagner, 14 years ago

Enabled users to choose whether a parameter should be collected in each run or not (#1113)

File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
59    /// </summary>
60    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
61    protected override void DeregisterContentEvents() {
62      Content.ActualNameChanged -= new EventHandler(Content_ActualNameChanged);
63      Content.GetsCollectedChanged -= new EventHandler(Content_GetsCollectedChanged);
64      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
65      base.DeregisterContentEvents();
66    }
67
68    /// <summary>
69    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
70    /// </summary>
71    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
72    protected override void RegisterContentEvents() {
73      base.RegisterContentEvents();
74      Content.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
75      Content.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
76      Content.ValueChanged += new EventHandler(Content_ValueChanged);
77    }
78
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      if (Content == null) {
82        actualNameTextBox.Text = "-";
83        showInRunCheckBox.Checked = false;
84        valueViewHost.Content = null;
85      } else {
86        SetDataTypeTextBoxText();
87        actualNameTextBox.Text = Content.ActualName;
88        showInRunCheckBox.Checked = Content.GetsCollected;
89        valueViewHost.ViewType = null;
90        valueViewHost.Content = Content.Value;
91      }
92    }
93
94    protected override void SetEnabledStateOfControls() {
95      base.SetEnabledStateOfControls();
96      actualNameTextBox.Enabled = Content != null;
97      actualNameTextBox.ReadOnly = ReadOnly;
98      setValueButton.Enabled = Content != null && !ReadOnly;
99      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
100      showInRunCheckBox.Enabled = Content != null && !ReadOnly;
101      valueGroupBox.Enabled = Content != null;
102    }
103
104    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
105      if (InvokeRequired)
106        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
107      else
108        actualNameTextBox.Text = Content.ActualName;
109    }
110    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
111      if (InvokeRequired)
112        Invoke(new EventHandler(Content_ValueChanged), sender, e);
113      else {
114        SetDataTypeTextBoxText();
115        clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
116        valueViewHost.ViewType = null;
117        valueViewHost.Content = Content != null ? Content.Value : null;
118      }
119    }
120    protected virtual void Content_GetsCollectedChanged(object sender, EventArgs e) {
121      if (InvokeRequired)
122        Invoke(new EventHandler(Content_GetsCollectedChanged), sender, e);
123      else
124        showInRunCheckBox.Checked = Content != null && Content.GetsCollected;
125    }
126
127    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
128      Content.ActualName = actualNameTextBox.Text;
129    }
130    protected virtual void setValueButton_Click(object sender, EventArgs e) {
131      if (typeSelectorDialog == null) {
132        typeSelectorDialog = new TypeSelectorDialog();
133        typeSelectorDialog.Caption = "Select Value";
134        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
135      }
136      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
137        try {
138          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
139        }
140        catch (Exception ex) {
141          ErrorHandling.ShowErrorDialog(this, ex);
142        }
143      }
144    }
145    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
146      Content.Value = null;
147    }
148    protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
149      if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
150    }
151    protected virtual void valueViewHostPanel_DragEnterOver(object sender, DragEventArgs e) {
152      e.Effect = DragDropEffects.None;
153      Type type = e.Data.GetData("Type") as Type;
154      if (!ReadOnly && (type != null) && (Content.DataType.IsAssignableFrom(type))) {
155        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
156        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
157        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
158        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
159        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
160      }
161    }
162    protected virtual void valueViewHost_DragDrop(object sender, DragEventArgs e) {
163      if (e.Effect != DragDropEffects.None) {
164        T value = e.Data.GetData("Value") as T;
165        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
166        Content.Value = value;
167      }
168    }
169
170    #region Helpers
171    protected void SetDataTypeTextBoxText() {
172      if (Content == null) {
173        dataTypeTextBox.Text = "-";
174      } else {
175        if ((Content.Value != null) && (Content.Value.GetType() != Content.DataType))
176          dataTypeTextBox.Text = Content.DataType.GetPrettyName() + " (" + Content.Value.GetType().GetPrettyName() + ")";
177        else
178          dataTypeTextBox.Text = Content.DataType.GetPrettyName();
179      }
180    }
181    #endregion
182  }
183}
Note: See TracBrowser for help on using the repository browser.