Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

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