Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

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