Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2830 was 2818, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • corrected several bugs in order to get SGA working
File size: 6.5 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;
[2714]24using HeuristicLab.Core;
25using HeuristicLab.Core.Views;
[2655]26using HeuristicLab.MainForm;
27
[2714]28namespace HeuristicLab.Parameters.Views {
[2655]29  /// <summary>
30  /// The visual representation of a <see cref="Parameter"/>.
31  /// </summary>
[2756]32  [Content(typeof(ValueLookupParameter<>), true)]
33  [Content(typeof(IValueLookupParameter<>), false)]
34  public partial class ValueLookupParameterView<T> : ParameterView where T : class, IItem {
[2676]35    protected TypeSelectorDialog typeSelectorDialog;
[2655]36
37    /// <summary>
38    /// Gets or sets the variable to represent visually.
39    /// </summary>
40    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
41    /// No own data storage present.</remarks>
[2756]42    public new IValueLookupParameter<T> Content {
43      get { return (IValueLookupParameter<T>)base.Content; }
[2713]44      set { base.Content = value; }
[2655]45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
49    /// </summary>
[2756]50    public ValueLookupParameterView() {
[2655]51      InitializeComponent();
[2756]52      Caption = "ValueLookupParameter";
[2655]53    }
54    /// <summary>
55    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
56    /// </summary>
57    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
58    /// <param name="variable">The variable to represent visually.</param>
[2756]59    public ValueLookupParameterView(IValueLookupParameter<T> content)
[2655]60      : this() {
[2727]61      Content = content;
[2655]62    }
63
64    /// <summary>
65    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]68    protected override void DeregisterContentEvents() {
69      Content.ActualNameChanged -= new EventHandler(Content_ActualNameChanged);
[2756]70      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
[2713]71      base.DeregisterContentEvents();
[2655]72    }
73
74    /// <summary>
75    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
76    /// </summary>
77    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
[2756]81      Content.ValueChanged += new EventHandler(Content_ValueChanged);
[2655]82    }
83
[2713]84    protected override void OnContentChanged() {
85      base.OnContentChanged();
86      if (Content == null) {
[2756]87        Caption = "ValueLookupParameter";
[2655]88        actualNameTextBox.Text = "-";
89        actualNameTextBox.Enabled = false;
[2756]90        setValueButton.Enabled = false;
91        clearValueButton.Enabled = false;
92        valueGroupBox.Enabled = false;
[2713]93        viewHost.Content = null;
[2655]94      } else {
[2713]95        Caption = Content.Name + " (" + Content.GetType().Name + ")";
96        actualNameTextBox.Text = Content.ActualName;
[2756]97        actualNameTextBox.Enabled = true;
98        setValueButton.Enabled = Content.Value == null;
99        clearValueButton.Enabled = Content.Value != null;
100        valueGroupBox.Enabled = true;
101        viewHost.Content = Content.Value;
[2655]102      }
103    }
104
[2713]105    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
[2655]106      if (InvokeRequired)
[2713]107        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
[2655]108      else
[2713]109        actualNameTextBox.Text = Content.ActualName;
[2655]110    }
[2756]111    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
[2655]112      if (InvokeRequired)
[2756]113        Invoke(new EventHandler(Content_ValueChanged), sender, e);
[2655]114      else {
[2756]115        setValueButton.Enabled = Content.Value == null;
116        clearValueButton.Enabled = Content.Value != null;
117        viewHost.Content = Content.Value;
[2655]118      }
119    }
120
[2676]121    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
[2713]122      Content.ActualName = actualNameTextBox.Text;
[2655]123    }
[2756]124    protected virtual void setValueButton_Click(object sender, EventArgs e) {
[2655]125      if (typeSelectorDialog == null) {
126        typeSelectorDialog = new TypeSelectorDialog();
[2756]127        typeSelectorDialog.Caption = "Select Value";
128        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, false);
[2655]129      }
130      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK)
[2756]131        Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[2655]132    }
[2756]133    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
134      Content.Value = null;
[2655]135    }
[2756]136    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
[2655]137      e.Effect = DragDropEffects.None;
138      Type type = e.Data.GetData("Type") as Type;
[2713]139      if ((type != null) && (Content.DataType.IsAssignableFrom(type))) {
[2655]140        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
[2694]141        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
142        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
143        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
144        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[2655]145      }
146    }
[2756]147    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
[2655]148      if (e.Effect != DragDropEffects.None) {
[2756]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;
[2655]152      }
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.