Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed redundant new line string constant (#1416)

File size: 5.4 KB
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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>
[2917]31  [View("NamedItem View")]
[2790]32  [Content(typeof(NamedItem), false)]
[2727]33  [Content(typeof(INamedItem), false)]
[2664]34  public partial class NamedItemView : ItemView {
[5832]35    private const string infoLabelToolTipSuffix = "Double-click to open description editor.";
36
[2727]37    public new INamedItem Content {
38      get { return (INamedItem)base.Content; }
[2713]39      set { base.Content = value; }
[2655]40    }
41
[2664]42    public NamedItemView() {
[2655]43      InitializeComponent();
[2676]44      errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
45      errorProvider.SetIconPadding(nameTextBox, 2);
[2655]46    }
47
[2713]48    protected override void DeregisterContentEvents() {
49      Content.NameChanged -= new EventHandler(Content_NameChanged);
50      Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
51      base.DeregisterContentEvents();
[2655]52    }
[2713]53    protected override void RegisterContentEvents() {
54      base.RegisterContentEvents();
55      Content.NameChanged += new EventHandler(Content_NameChanged);
56      Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
[2655]57    }
58
[2713]59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) {
[3454]62        nameTextBox.Text = string.Empty;
[5832]63        toolTip.SetToolTip(infoLabel, string.Empty);
[3764]64        if (ViewAttribute.HasViewAttribute(this.GetType()))
65          this.Caption = ViewAttribute.GetViewName(this.GetType());
66        else
67          this.Caption = "NamedItem View";
[2655]68      } else {
[2713]69        nameTextBox.Text = Content.Name;
[5833]70        toolTip.SetToolTip(infoLabel, string.IsNullOrEmpty(Content.Description) ? infoLabelToolTipSuffix : Content.Description + Environment.NewLine + Environment.NewLine + infoLabelToolTipSuffix);
[3764]71        Caption = Content.Name;
[2655]72      }
73    }
74
[3904]75    protected override void SetEnabledStateOfControls() {
76      base.SetEnabledStateOfControls();
[3350]77      if (Content == null) {
78        nameTextBox.Enabled = false;
[5832]79        infoLabel.Enabled = false;
[3350]80      } else {
81        nameTextBox.Enabled = true;
[5832]82        nameTextBox.ReadOnly = ReadOnly || !Content.CanChangeName;
83        infoLabel.Enabled = true;
[3350]84      }
85    }
86
[2713]87    protected virtual void Content_NameChanged(object sender, EventArgs e) {
[2655]88      if (InvokeRequired)
[2713]89        Invoke(new EventHandler(Content_NameChanged), sender, e);
[3764]90      else {
[2713]91        nameTextBox.Text = Content.Name;
[3764]92        Caption = Content.Name;
93      }
[2655]94    }
[2713]95    protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
[2655]96      if (InvokeRequired)
[2713]97        Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
[2917]98      else {
[5833]99        toolTip.SetToolTip(infoLabel, string.IsNullOrEmpty(Content.Description) ? infoLabelToolTipSuffix : Content.Description + Environment.NewLine + Environment.NewLine + infoLabelToolTipSuffix);
[2917]100      }
[2655]101    }
102
103    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
[2916]104      if ((Content != null) && (Content.CanChangeName)) {
[3764]105        if (string.IsNullOrEmpty(nameTextBox.Text)) {
106          e.Cancel = true;
107          errorProvider.SetError(nameTextBox, "Name cannot be empty");
108          nameTextBox.SelectAll();
109          return;
110        }
[2713]111        Content.Name = nameTextBox.Text;
[2655]112        // check if variable name was set successfully
[2713]113        if (!Content.Name.Equals(nameTextBox.Text)) {
[2676]114          e.Cancel = true;
115          errorProvider.SetError(nameTextBox, "Invalid Name");
[2655]116          nameTextBox.SelectAll();
117        }
118      }
119    }
[2676]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) {
[2713]127        nameTextBox.Text = Content.Name;
[2676]128        nameLabel.Focus();  // set focus on label to validate data
129      }
130    }
[5832]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)) {
[2917]133        if (dialog.ShowDialog(this) == DialogResult.OK)
134          Content.Description = dialog.Content;
135      }
136    }
[2655]137  }
138}
Note: See TracBrowser for help on using the repository browser.