Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemParameterView.cs @ 2710

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

Operator architecture refactoring (#95)

  • finished implemented ideas which came up during yesterday's presentation of HeuristicLab.Core and related plugins
File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Core.Views {
32  /// <summary>
33  /// The visual representation of a <see cref="Parameter"/>.
34  /// </summary>
35  [Content(typeof(ItemParameter), true)]
36  public partial class ItemParameterView : ParameterView {
37    protected TypeSelectorDialog typeSelectorDialog;
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>
44    public new ItemParameter Parameter {
45      get { return (ItemParameter)Item; }
46      set { base.Item = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
51    /// </summary>
52    public ItemParameterView() {
53      InitializeComponent();
54      Caption = "Parameter";
55    }
56    /// <summary>
57    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
58    /// </summary>
59    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
60    /// <param name="variable">The variable to represent visually.</param>
61    public ItemParameterView(ItemParameter parameter)
62      : this() {
63      Parameter = parameter;
64    }
65
66    /// <summary>
67    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
68    /// </summary>
69    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
70    protected override void DeregisterObjectEvents() {
71      Parameter.ActualNameChanged -= new EventHandler(Parameter_ActualNameChanged);
72      Parameter.ValueChanged -= new EventHandler(Parameter_ValueChanged);
73      base.DeregisterObjectEvents();
74    }
75
76    /// <summary>
77    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
78    /// </summary>
79    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
80    protected override void RegisterObjectEvents() {
81      base.RegisterObjectEvents();
82      Parameter.ActualNameChanged += new EventHandler(Parameter_ActualNameChanged);
83      Parameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
84    }
85
86    protected override void OnObjectChanged() {
87      base.OnObjectChanged();
88      if (Parameter == null) {
89        Caption = "Parameter";
90        actualNameTextBox.Text = "-";
91        actualNameTextBox.Enabled = false;
92        setValueButton.Enabled = false;
93        clearValueButton.Enabled = false;
94        valueGroupBox.Enabled = false;
95        viewHost.Object = null;
96      } else {
97        Caption = Parameter.Name + " (" + Parameter.GetType().Name + ")";
98        actualNameTextBox.Text = Parameter.ActualName;
99        actualNameTextBox.Enabled = Parameter.Value == null;
100        setValueButton.Enabled = Parameter.Value == null;
101        clearValueButton.Enabled = Parameter.Value != null;
102        valueGroupBox.Enabled = true;
103        viewHost.Object = Parameter.Value;
104      }
105    }
106
107    protected virtual void Parameter_ActualNameChanged(object sender, EventArgs e) {
108      if (InvokeRequired)
109        Invoke(new EventHandler(Parameter_ActualNameChanged), sender, e);
110      else
111        actualNameTextBox.Text = Parameter.ActualName;
112    }
113    protected virtual void Parameter_ValueChanged(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Parameter_ValueChanged), sender, e);
116      else {
117        actualNameTextBox.Enabled = Parameter.Value == null;
118        setValueButton.Enabled = Parameter.Value == null;
119        clearValueButton.Enabled = Parameter.Value != null;
120        viewHost.Object = Parameter.Value;
121      }
122    }
123
124    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
125      Parameter.ActualName = actualNameTextBox.Text;
126    }
127    protected virtual void setValueButton_Click(object sender, EventArgs e) {
128      if (typeSelectorDialog == null) {
129        typeSelectorDialog = new TypeSelectorDialog();
130        typeSelectorDialog.Caption = "Select Value Type";
131      }
132      typeSelectorDialog.TypeSelector.Configure(Parameter.DataType, false, false);
133      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK)
134        Parameter.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
135    }
136    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
137      Parameter.Value = null;
138    }
139    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
140      e.Effect = DragDropEffects.None;
141      Type type = e.Data.GetData("Type") as Type;
142      if ((type != null) && (Parameter.DataType.IsAssignableFrom(type))) {
143        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
144        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
145        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
146        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
147        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
148      }
149    }
150    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
151      if (e.Effect != DragDropEffects.None) {
152        IItem item = e.Data.GetData("Value") as IItem;
153        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
154        Parameter.Value = item;
155      }
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.