Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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