Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ParameterView.cs @ 2655

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

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

File size: 6.2 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(Parameter), true)]
36  public partial class ParameterView : ParameterBaseView {
37    private 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 Parameter Parameter {
45      get { return (Parameter)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 ParameterView() {
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 ParameterView(Parameter 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    private 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    private 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    private void actualNameTextBox_Validated(object sender, EventArgs e) {
125      Parameter.ActualName = actualNameTextBox.Text;
126    }
127    private 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    private void clearValueButton_Click(object sender, EventArgs e) {
137      Parameter.Value = null;
138    }
139    private 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 & 32) == 32) e.Effect = DragDropEffects.Move;  // ALT key
145        else e.Effect = DragDropEffects.Link;
146      }
147    }
148    private void valuePanel_DragDrop(object sender, DragEventArgs e) {
149      if (e.Effect != DragDropEffects.None) {
150        IItem item = e.Data.GetData("Value") as IItem;
151        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
152        Parameter.Value = item;
153      }
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.