Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4047 was 3904, checked in by mkommend, 15 years ago

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

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