Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Parameters.Views/3.3/ValueParameterView.cs @ 10429

Last change on this file since 10429 was 4522, checked in by mkommend, 14 years ago

Enabled dropping of values on various views (ticket #1155).

File size: 7.0 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("ValueParameter View")]
35  [Content(typeof(OptionalValueParameter<>), true)]
36  [Content(typeof(ValueParameter<>), true)]
37  [Content(typeof(IValueParameter<>), false)]
38  public partial class ValueParameterView<T> : ParameterView where T : class, IItem {
39    protected TypeSelectorDialog typeSelectorDialog;
40
41    /// <summary>
42    /// Gets or sets the variable to represent visually.
43    /// </summary>
44    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
45    /// No own data storage present.</remarks>
46    public new IValueParameter<T> Content {
47      get { return (IValueParameter<T>)base.Content; }
48      set { base.Content = value; }
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
53    /// </summary>
54    public ValueParameterView() {
55      InitializeComponent();
56    }
57
58    /// <summary>
59    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
60    /// </summary>
61    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
62    protected override void DeregisterContentEvents() {
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.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
75      Content.ValueChanged += new EventHandler(Content_ValueChanged);
76    }
77
78    protected override void OnContentChanged() {
79      base.OnContentChanged();
80      if (Content == null) {
81        showInRunCheckBox.Checked = false;
82        valueViewHost.Content = null;
83      } else {
84        SetDataTypeTextBoxText();
85        showInRunCheckBox.Checked = Content.GetsCollected;
86        valueViewHost.ViewType = null;
87        valueViewHost.Content = Content.Value;
88      }
89    }
90
91    protected override void SetEnabledStateOfControls() {
92      base.SetEnabledStateOfControls();
93      setValueButton.Enabled = Content != null && !ReadOnly;
94      clearValueButton.Enabled = Content != null && Content.Value != null && !(Content is ValueParameter<T>) && !ReadOnly;
95      showInRunCheckBox.Enabled = Content != null && !ReadOnly;
96    }
97
98    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
99      if (InvokeRequired)
100        Invoke(new EventHandler(Content_ValueChanged), sender, e);
101      else {
102        SetDataTypeTextBoxText();
103        clearValueButton.Enabled = Content != null && Content.Value != null && !(Content is ValueParameter<T>) && !ReadOnly;
104        valueViewHost.ViewType = null;
105        valueViewHost.Content = Content != null ? Content.Value : null;
106      }
107    }
108    protected virtual void Content_GetsCollectedChanged(object sender, EventArgs e) {
109      if (InvokeRequired)
110        Invoke(new EventHandler(Content_GetsCollectedChanged), sender, e);
111      else
112        showInRunCheckBox.Checked = Content != null && Content.GetsCollected;
113    }
114
115    protected virtual void setValueButton_Click(object sender, EventArgs e) {
116      if (typeSelectorDialog == null) {
117        typeSelectorDialog = new TypeSelectorDialog();
118        typeSelectorDialog.Caption = "Select Value";
119        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
120      }
121      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
122        try {
123          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
124        }
125        catch (Exception ex) {
126          ErrorHandling.ShowErrorDialog(this, ex);
127        }
128      }
129    }
130    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
131      Content.Value = null;
132    }
133    protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
134      if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
135    }
136    protected virtual void valueGroupBox_DragEnterOver(object sender, DragEventArgs e) {
137      e.Effect = DragDropEffects.None;
138      Type type = e.Data.GetData("Type") as Type;
139      if (!ReadOnly && (type != null) && (Content.DataType.IsAssignableFrom(type))) {
140        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
141        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
142        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
143        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
144        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
145      }
146    }
147    protected virtual void valueGroupBox_DragDrop(object sender, DragEventArgs e) {
148      if (e.Effect != DragDropEffects.None) {
149        T value = e.Data.GetData("Value") as T;
150        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
151        Content.Value = value;
152      }
153    }
154
155    #region Helpers
156    protected void SetDataTypeTextBoxText() {
157      if (Content == null) {
158        dataTypeTextBox.Text = "-";
159      } else {
160        if ((Content.Value != null) && (Content.Value.GetType() != Content.DataType))
161          dataTypeTextBox.Text = Content.DataType.GetPrettyName() + " (" + Content.Value.GetType().GetPrettyName() + ")";
162        else
163          dataTypeTextBox.Text = Content.DataType.GetPrettyName();
164      }
165    }
166    #endregion
167  }
168}
Note: See TracBrowser for help on using the repository browser.