Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2903 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
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2655]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>
[2790]31  [Content(typeof(NamedItem), false)]
[2727]32  [Content(typeof(INamedItem), false)]
[2664]33  public partial class NamedItemView : ItemView {
[2727]34    public new INamedItem Content {
35      get { return (INamedItem)base.Content; }
[2713]36      set { base.Content = value; }
[2655]37    }
38
[2664]39    public NamedItemView() {
[2655]40      InitializeComponent();
41      Caption = "NamedItem";
[2676]42      errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
43      errorProvider.SetIconPadding(nameTextBox, 2);
[2655]44    }
[2727]45    public NamedItemView(INamedItem content)
[2655]46      : this() {
[2727]47      Content = content;
[2655]48    }
49
[2713]50    protected override void DeregisterContentEvents() {
51      Content.NameChanged -= new EventHandler(Content_NameChanged);
52      Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
53      base.DeregisterContentEvents();
[2655]54    }
[2713]55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.NameChanged += new EventHandler(Content_NameChanged);
58      Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
[2655]59    }
60
[2713]61    protected override void OnContentChanged() {
62      base.OnContentChanged();
63      if (Content == null) {
[2655]64        Caption = "NamedItem";
65        nameTextBox.Text = "-";
66        nameTextBox.Enabled = false;
67        descriptionTextBox.Text = "";
68        descriptionTextBox.Enabled = false;
69      } else {
[2713]70        Caption = Content.Name + " (" + Content.GetType().Name + ")";
71        nameTextBox.Text = Content.Name;
72        nameTextBox.ReadOnly = !Content.CanChangeName;
[2655]73        nameTextBox.Enabled = true;
[2713]74        descriptionTextBox.Text = Content.Description;
75        descriptionTextBox.ReadOnly = !Content.CanChangeDescription;
[2655]76        descriptionTextBox.Enabled = true;
77      }
78    }
79
[2713]80    protected virtual void Content_NameChanged(object sender, EventArgs e) {
[2655]81      if (InvokeRequired)
[2713]82        Invoke(new EventHandler(Content_NameChanged), sender, e);
[2655]83      else
[2713]84        nameTextBox.Text = Content.Name;
[2655]85    }
[2713]86    protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
[2655]87      if (InvokeRequired)
[2713]88        Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
[2655]89      else
[2713]90        descriptionTextBox.Text = Content.Description;
[2655]91    }
92
93    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
[2713]94      if (Content.CanChangeName) {
95        Content.Name = nameTextBox.Text;
[2655]96
97        // check if variable name was set successfully
[2713]98        if (!Content.Name.Equals(nameTextBox.Text)) {
[2676]99          e.Cancel = true;
100          errorProvider.SetError(nameTextBox, "Invalid Name");
[2655]101          nameTextBox.SelectAll();
102        }
103      }
104    }
[2676]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) {
[2713]112        nameTextBox.Text = Content.Name;
[2676]113        nameLabel.Focus();  // set focus on label to validate data
114      }
115    }
[2655]116    protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
[2713]117      if (Content.CanChangeDescription)
118        Content.Description = descriptionTextBox.Text;
[2655]119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.