Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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  [View("NamedItem View")]
32  [Content(typeof(NamedItem), false)]
33  [Content(typeof(INamedItem), false)]
34  public partial class NamedItemView : ItemView {
35    public new INamedItem Content {
36      get { return (INamedItem)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public NamedItemView() {
41      InitializeComponent();
42      errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
43      errorProvider.SetIconPadding(nameTextBox, 2);
44    }
45
46    protected override void DeregisterContentEvents() {
47      Content.NameChanged -= new EventHandler(Content_NameChanged);
48      Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
49      base.DeregisterContentEvents();
50    }
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.NameChanged += new EventHandler(Content_NameChanged);
54      Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
55    }
56
57    protected override void OnContentChanged() {
58      base.OnContentChanged();
59      if (Content == null) {
60        nameTextBox.Text = string.Empty;
61        descriptionTextBox.Text = string.Empty;
62        toolTip.SetToolTip(descriptionTextBox, string.Empty);
63        if (ViewAttribute.HasViewAttribute(this.GetType()))
64          this.Caption = ViewAttribute.GetViewName(this.GetType());
65        else
66          this.Caption = "NamedItem View";
67      } else {
68        nameTextBox.Text = Content.Name;
69        descriptionTextBox.Text = Content.Description;
70        toolTip.SetToolTip(descriptionTextBox, Content.Description);
71        Caption = Content.Name;
72      }
73    }
74
75    protected override void SetEnabledStateOfControls() {
76      base.SetEnabledStateOfControls();
77      if (Content == null) {
78        nameTextBox.Enabled = false;
79        descriptionTextBox.Enabled = false;
80      } else {
81        nameTextBox.Enabled = true;
82        nameTextBox.ReadOnly = ReadOnly || !Content.CanChangeName; ;
83        descriptionTextBox.Enabled = true;
84        descriptionTextBox.ReadOnly = ReadOnly || !Content.CanChangeDescription;
85      }
86    }
87
88    protected virtual void Content_NameChanged(object sender, EventArgs e) {
89      if (InvokeRequired)
90        Invoke(new EventHandler(Content_NameChanged), sender, e);
91      else {
92        nameTextBox.Text = Content.Name;
93        Caption = Content.Name;
94      }
95    }
96    protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
97      if (InvokeRequired)
98        Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
99      else {
100        descriptionTextBox.Text = Content.Description;
101        toolTip.SetToolTip(descriptionTextBox, Content.Description);
102      }
103    }
104
105    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
106      if ((Content != null) && (Content.CanChangeName)) {
107        if (string.IsNullOrEmpty(nameTextBox.Text)) {
108          e.Cancel = true;
109          errorProvider.SetError(nameTextBox, "Name cannot be empty");
110          nameTextBox.SelectAll();
111          return;
112        }
113        Content.Name = nameTextBox.Text;
114        // check if variable name was set successfully
115        if (!Content.Name.Equals(nameTextBox.Text)) {
116          e.Cancel = true;
117          errorProvider.SetError(nameTextBox, "Invalid Name");
118          nameTextBox.SelectAll();
119        }
120      }
121    }
122    protected virtual void nameTextBox_Validated(object sender, EventArgs e) {
123      errorProvider.SetError(nameTextBox, string.Empty);
124    }
125    protected virtual void nameTextBox_KeyDown(object sender, KeyEventArgs e) {
126      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
127        nameLabel.Focus();  // set focus on label to validate data
128      if (e.KeyCode == Keys.Escape) {
129        nameTextBox.Text = Content.Name;
130        nameLabel.Focus();  // set focus on label to validate data
131      }
132    }
133    protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
134      if (Content.CanChangeDescription)
135        Content.Description = descriptionTextBox.Text;
136    }
137
138    protected void descriptionTextBox_DoubleClick(object sender, EventArgs e) {
139      using (TextDialog dialog = new TextDialog("Description of " + Content.Name, descriptionTextBox.Text, ReadOnly || !Content.CanChangeDescription)) {
140        if (dialog.ShowDialog(this) == DialogResult.OK)
141          Content.Description = dialog.Content;
142      }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.