Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/NamedItemView.cs @ 2879

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

Added some of the changes suggested by abeham to improve UI reaction times (#887)

File size: 4.5 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.ComponentModel;
24using System.Windows.Forms;
25using HeuristicLab.MainForm;
26
27namespace HeuristicLab.Core.Views {
28  /// <summary>
29  /// The visual representation of a <see cref="Variable"/>.
30  /// </summary>
31  [Content(typeof(NamedItem), false)]
32  [Content(typeof(INamedItem), false)]
33  public partial class NamedItemView : ItemView {
34    public new INamedItem Content {
35      get { return (INamedItem)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public NamedItemView() {
40      InitializeComponent();
41      Caption = "NamedItem";
42      errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
43      errorProvider.SetIconPadding(nameTextBox, 2);
44    }
45    public NamedItemView(INamedItem content)
46      : this() {
47      Content = content;
48    }
49
50    protected override void DeregisterContentEvents() {
51      Content.NameChanged -= new EventHandler(Content_NameChanged);
52      Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
53      base.DeregisterContentEvents();
54    }
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.NameChanged += new EventHandler(Content_NameChanged);
58      Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
59    }
60
61    protected override void OnContentChanged() {
62      base.OnContentChanged();
63      if (Content == null) {
64        Caption = "NamedItem";
65        nameTextBox.Text = "-";
66        nameTextBox.Enabled = false;
67        descriptionTextBox.Text = "";
68        descriptionTextBox.Enabled = false;
69      } else {
70        Caption = Content.Name + " (" + Content.GetType().Name + ")";
71        nameTextBox.Text = Content.Name;
72        nameTextBox.ReadOnly = !Content.CanChangeName;
73        nameTextBox.Enabled = true;
74        descriptionTextBox.Text = Content.Description;
75        descriptionTextBox.ReadOnly = !Content.CanChangeDescription;
76        descriptionTextBox.Enabled = true;
77      }
78    }
79
80    protected virtual void Content_NameChanged(object sender, EventArgs e) {
81      if (InvokeRequired)
82        Invoke(new EventHandler(Content_NameChanged), sender, e);
83      else
84        nameTextBox.Text = Content.Name;
85    }
86    protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
87      if (InvokeRequired)
88        Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
89      else
90        descriptionTextBox.Text = Content.Description;
91    }
92
93    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
94      if (Content.CanChangeName) {
95        Content.Name = nameTextBox.Text;
96
97        // check if variable name was set successfully
98        if (!Content.Name.Equals(nameTextBox.Text)) {
99          e.Cancel = true;
100          errorProvider.SetError(nameTextBox, "Invalid Name");
101          nameTextBox.SelectAll();
102        }
103      }
104    }
105    protected virtual void nameTextBox_Validated(object sender, EventArgs e) {
106      errorProvider.SetError(nameTextBox, string.Empty);
107    }
108    protected virtual void nameTextBox_KeyDown(object sender, KeyEventArgs e) {
109      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
110        nameLabel.Focus();  // set focus on label to validate data
111      if (e.KeyCode == Keys.Escape) {
112        nameTextBox.Text = Content.Name;
113        nameLabel.Focus();  // set focus on label to validate data
114      }
115    }
116    protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
117      if (Content.CanChangeDescription)
118        Content.Description = descriptionTextBox.Text;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.