Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/VariableView.cs @ 2530

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

Implemented first draft of MainForm support in HeuristicLab.Core/HeuristicLab.Core.Views and all other depending plugins (#770)

File size: 5.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 an <see cref="IVariable"/>.
34  /// </summary>
35  [Content(typeof(Variable), true)]
36  public partial class VariableView : ViewBase {
37    private ChooseItemDialog chooseItemDialog;
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 IVariable Variable {
45      get { return (IVariable)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 VariableView() {
53      InitializeComponent();
54      Caption = "Variable";
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 VariableView(IVariable variable)
62      : this() {
63      Variable = variable;
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 RemoveItemEvents() {
71      Variable.NameChanged -= new EventHandler(Variable_NameChanged);
72      Variable.ValueChanged -= new EventHandler(Variable_ValueChanged);
73      base.RemoveItemEvents();
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>
79    protected override void AddItemEvents() {
80      base.AddItemEvents();
81      Variable.NameChanged += new EventHandler(Variable_NameChanged);
82      Variable.ValueChanged += new EventHandler(Variable_ValueChanged);
83    }
84
85    /// <summary>
86    /// Updates all controls with the latest data of the model.
87    /// </summary>
88    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
89    protected override void UpdateControls() {
90      base.UpdateControls();
91      valueTextBox.Text = "-";
92      valueTextBox.Enabled = false;
93      editorGroupBox.Controls.Clear();
94      editorGroupBox.Enabled = false;
95      if (Variable == null) {
96        Caption = "Variable";
97        nameTextBox.Enabled = false;
98      } else {
99        Caption = Variable.Name + " (" + Variable.GetType().Name + ")";
100        nameTextBox.Text = Variable.Name;
101        nameTextBox.Enabled = true;
102        if (Variable.Value != null) {
103          valueTextBox.Text = Variable.Value.GetType().FullName;
104          valueTextBox.Enabled = true;
105          Control editor = (Control)MainFormManager.CreateDefaultView(Variable.Value);
106          if (editor != null) {
107            editorGroupBox.Controls.Add(editor);
108            editor.Dock = DockStyle.Fill;
109            editorGroupBox.Enabled = true;
110          }
111        }
112      }
113    }
114
115    private void Variable_NameChanged(object sender, EventArgs e) {
116      UpdateText();
117    }
118    private void UpdateText() {
119      if (InvokeRequired)
120        Invoke(new MethodInvoker(UpdateText));
121      else
122        nameTextBox.Text = Variable.Name;
123    }
124    private void Variable_ValueChanged(object sender, EventArgs e) {
125      Refresh();
126    }
127
128    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
129      Variable.Name = nameTextBox.Text;
130
131      // check if variable name was set successfully
132      e.Cancel = Variable.Name != nameTextBox.Text;
133    }
134
135    private void setVariableValueButton_Click(object sender, EventArgs e) {
136      if (chooseItemDialog == null) {
137        chooseItemDialog = new ChooseItemDialog();
138        chooseItemDialog.Caption = "Set Value";
139      }
140      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK)
141        Variable.Value = chooseItemDialog.Item;
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.