Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7049 was 5833, checked in by swagner, 14 years ago

Removed redundant new line string constant (#1416)

File size: 5.4 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    private const string infoLabelToolTipSuffix = "Double-click to open description editor.";
36
37    public new INamedItem Content {
38      get { return (INamedItem)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public NamedItemView() {
43      InitializeComponent();
44      errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
45      errorProvider.SetIconPadding(nameTextBox, 2);
46    }
47
48    protected override void DeregisterContentEvents() {
49      Content.NameChanged -= new EventHandler(Content_NameChanged);
50      Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
51      base.DeregisterContentEvents();
52    }
53    protected override void RegisterContentEvents() {
54      base.RegisterContentEvents();
55      Content.NameChanged += new EventHandler(Content_NameChanged);
56      Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) {
62        nameTextBox.Text = string.Empty;
63        toolTip.SetToolTip(infoLabel, 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        toolTip.SetToolTip(infoLabel, string.IsNullOrEmpty(Content.Description) ? infoLabelToolTipSuffix : Content.Description + Environment.NewLine + Environment.NewLine + infoLabelToolTipSuffix);
71        Caption = Content.Name;
72      }
73    }
74
75    protected override void SetEnabledStateOfControls() {
76      base.SetEnabledStateOfControls();
77      if (Content == null) {
78        nameTextBox.Enabled = false;
79        infoLabel.Enabled = false;
80      } else {
81        nameTextBox.Enabled = true;
82        nameTextBox.ReadOnly = ReadOnly || !Content.CanChangeName;
83        infoLabel.Enabled = true;
84      }
85    }
86
87    protected virtual void Content_NameChanged(object sender, EventArgs e) {
88      if (InvokeRequired)
89        Invoke(new EventHandler(Content_NameChanged), sender, e);
90      else {
91        nameTextBox.Text = Content.Name;
92        Caption = Content.Name;
93      }
94    }
95    protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
96      if (InvokeRequired)
97        Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
98      else {
99        toolTip.SetToolTip(infoLabel, string.IsNullOrEmpty(Content.Description) ? infoLabelToolTipSuffix : Content.Description + Environment.NewLine + Environment.NewLine + infoLabelToolTipSuffix);
100      }
101    }
102
103    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
104      if ((Content != null) && (Content.CanChangeName)) {
105        if (string.IsNullOrEmpty(nameTextBox.Text)) {
106          e.Cancel = true;
107          errorProvider.SetError(nameTextBox, "Name cannot be empty");
108          nameTextBox.SelectAll();
109          return;
110        }
111        Content.Name = nameTextBox.Text;
112        // check if variable name was set successfully
113        if (!Content.Name.Equals(nameTextBox.Text)) {
114          e.Cancel = true;
115          errorProvider.SetError(nameTextBox, "Invalid Name");
116          nameTextBox.SelectAll();
117        }
118      }
119    }
120    protected virtual void nameTextBox_Validated(object sender, EventArgs e) {
121      errorProvider.SetError(nameTextBox, string.Empty);
122    }
123    protected virtual void nameTextBox_KeyDown(object sender, KeyEventArgs e) {
124      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
125        nameLabel.Focus();  // set focus on label to validate data
126      if (e.KeyCode == Keys.Escape) {
127        nameTextBox.Text = Content.Name;
128        nameLabel.Focus();  // set focus on label to validate data
129      }
130    }
131    protected virtual void infoLabel_DoubleClick(object sender, EventArgs e) {
132      using (TextDialog dialog = new TextDialog("Description of " + Content.Name, Content.Description, ReadOnly || !Content.CanChangeDescription)) {
133        if (dialog.ShowDialog(this) == DialogResult.OK)
134          Content.Description = dialog.Content;
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.