Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Core.Views/3.3/NamedItemView.cs @ 14570

Last change on this file since 14570 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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