Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/VariableView.cs @ 2282

Last change on this file since 2282 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 5.1 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;
29
30namespace HeuristicLab.Core {
31  /// <summary>
32  /// The visual representation of an <see cref="IVariable"/>.
33  /// </summary>
34  public partial class VariableView : ViewBase {
35    private ChooseItemDialog chooseItemDialog;
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 IVariable Variable {
43      get { return (IVariable)Item; }
44      set { base.Item = value; }
45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
49    /// </summary>
50    public VariableView() {
51      InitializeComponent();
52      Caption = "Variable";
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 VariableView(IVariable variable)
60      : this() {
61      Variable = variable;
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 RemoveItemEvents() {
69      Variable.NameChanged -= new EventHandler(Variable_NameChanged);
70      Variable.ValueChanged -= new EventHandler(Variable_ValueChanged);
71      base.RemoveItemEvents();
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 AddItemEvents() {
78      base.AddItemEvents();
79      Variable.NameChanged += new EventHandler(Variable_NameChanged);
80      Variable.ValueChanged += new EventHandler(Variable_ValueChanged);
81    }
82
83    /// <summary>
84    /// Updates all controls with the latest data of the model.
85    /// </summary>
86    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
87    protected override void UpdateControls() {
88      base.UpdateControls();
89      valueTextBox.Text = "-";
90      valueTextBox.Enabled = false;
91      editorGroupBox.Controls.Clear();
92      editorGroupBox.Enabled = false;
93      if (Variable == null) {
94        Caption = "Variable";
95        nameTextBox.Enabled = false;
96      } else {
97        Caption = Variable.Name + " (" + Variable.GetType().Name + ")";
98        nameTextBox.Text = Variable.Name;
99        nameTextBox.Enabled = true;
100        if (Variable.Value != null) {
101          valueTextBox.Text = Variable.Value.GetType().FullName;
102          valueTextBox.Enabled = true;
103          Control editor = (Control)Variable.Value.CreateView();
104          if (editor != null) {
105            editorGroupBox.Controls.Add(editor);
106            editor.Dock = DockStyle.Fill;
107            editorGroupBox.Enabled = true;
108          }
109        }
110      }
111    }
112
113    private void Variable_NameChanged(object sender, EventArgs e) {
114      UpdateText();
115    }
116    private void UpdateText() {
117      if (InvokeRequired)
118        Invoke(new MethodInvoker(UpdateText));
119      else
120        nameTextBox.Text = Variable.Name;
121    }
122    private void Variable_ValueChanged(object sender, EventArgs e) {
123      Refresh();
124    }
125
126    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
127      Variable.Name = nameTextBox.Text;
128
129      // check if variable name was set successfully
130      e.Cancel = Variable.Name != nameTextBox.Text;
131    }
132
133    private void setVariableValueButton_Click(object sender, EventArgs e) {
134      if (chooseItemDialog == null) {
135        chooseItemDialog = new ChooseItemDialog();
136        chooseItemDialog.Caption = "Set Value";
137      }
138      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK)
139        Variable.Value = chooseItemDialog.Item;
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.