Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters.Views/3.3/ValueParameterView.cs @ 2890

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

Operator architecture refactoring (#95)

  • worked on algorithms, problems and parameters
File size: 5.8 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.Core;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Parameters.Views {
29  /// <summary>
30  /// The visual representation of a <see cref="Parameter"/>.
31  /// </summary>
32  [Content(typeof(OptionalValueParameter<>), true)]
33  [Content(typeof(IValueParameter<>), false)]
34  public partial class ValueParameterView<T> : ParameterView where T : class, IItem {
35    protected TypeSelectorDialog typeSelectorDialog;
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>
42    public new IValueParameter<T> Content {
43      get { return (IValueParameter<T>)base.Content; }
44      set { base.Content = value; }
45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
49    /// </summary>
50    public ValueParameterView() {
51      InitializeComponent();
52      Caption = "ValueParameter";
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>
59    public ValueParameterView(IValueParameter<T> content)
60      : this() {
61      Content = content;
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>
68    protected override void DeregisterContentEvents() {
69      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
70      base.DeregisterContentEvents();
71    }
72
73    /// <summary>
74    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
75    /// </summary>
76    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
77    protected override void RegisterContentEvents() {
78      base.RegisterContentEvents();
79      Content.ValueChanged += new EventHandler(Content_ValueChanged);
80    }
81
82    protected override void OnContentChanged() {
83      base.OnContentChanged();
84      if (Content == null) {
85        Caption = "ValueParameter";
86        setValueButton.Enabled = false;
87        clearValueButton.Enabled = false;
88        viewHost.Content = null;
89        valueGroupBox.Enabled = false;
90      } else {
91        Caption = Content.Name + " (" + Content.GetType().Name + ")";
92        setValueButton.Enabled = Content.Value == null;
93        clearValueButton.Enabled = Content.Value != null;
94        valueGroupBox.Enabled = true;
95        viewHost.Content = Content.Value;
96      }
97    }
98
99    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
100      if (InvokeRequired)
101        Invoke(new EventHandler(Content_ValueChanged), sender, e);
102      else {
103        setValueButton.Enabled = Content.Value == null;
104        clearValueButton.Enabled = Content.Value != null;
105        viewHost.Content = Content.Value;
106      }
107    }
108
109    protected virtual void setValueButton_Click(object sender, EventArgs e) {
110      if (typeSelectorDialog == null) {
111        typeSelectorDialog = new TypeSelectorDialog();
112        typeSelectorDialog.Caption = "Select Value";
113        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, false);
114      }
115      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK)
116        Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
117    }
118    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
119      Content.Value = null;
120    }
121    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
122      e.Effect = DragDropEffects.None;
123      Type type = e.Data.GetData("Type") as Type;
124      if ((type != null) && (Content.DataType.IsAssignableFrom(type))) {
125        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
126        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
127        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
128        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
129        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
130      }
131    }
132    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
133      if (e.Effect != DragDropEffects.None) {
134        T value = e.Data.GetData("Value") as T;
135        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
136        Content.Value = value;
137      }
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.