Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5832 was 5832, checked in by swagner, 13 years ago

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