Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3120 was 2917, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
File size: 5.1 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      Caption = "NamedItem";
44      errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
45      errorProvider.SetIconPadding(nameTextBox, 2);
46    }
47    public NamedItemView(INamedItem content)
48      : this() {
49      Content = content;
50    }
51
52    protected override void DeregisterContentEvents() {
53      Content.NameChanged -= new EventHandler(Content_NameChanged);
54      Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
55      base.DeregisterContentEvents();
56    }
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Content.NameChanged += new EventHandler(Content_NameChanged);
60      Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
61    }
62
63    protected override void OnContentChanged() {
64      base.OnContentChanged();
65      if (Content == null) {
66        Caption = "NamedItem";
67        nameTextBox.Text = "-";
68        nameTextBox.Enabled = false;
69        descriptionTextBox.Text = "";
70        descriptionTextBox.Enabled = false;
71        toolTip.SetToolTip(descriptionTextBox, string.Empty);
72      } else {
73        Caption = Content.Name + " (" + Content.GetType().Name + ")";
74        nameTextBox.Text = Content.Name;
75        nameTextBox.ReadOnly = !Content.CanChangeName;
76        nameTextBox.Enabled = true;
77        descriptionTextBox.Text = Content.Description;
78        descriptionTextBox.ReadOnly = !Content.CanChangeDescription;
79        descriptionTextBox.Enabled = true;
80        toolTip.SetToolTip(descriptionTextBox, Content.Description);
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    }
90    protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
91      if (InvokeRequired)
92        Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
93      else {
94        descriptionTextBox.Text = Content.Description;
95        toolTip.SetToolTip(descriptionTextBox, Content.Description);
96      }
97    }
98
99    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
100      if ((Content != null) && (Content.CanChangeName)) {
101        Content.Name = nameTextBox.Text;
102
103        // check if variable name was set successfully
104        if (!Content.Name.Equals(nameTextBox.Text)) {
105          e.Cancel = true;
106          errorProvider.SetError(nameTextBox, "Invalid Name");
107          nameTextBox.SelectAll();
108        }
109      }
110    }
111    protected virtual void nameTextBox_Validated(object sender, EventArgs e) {
112      errorProvider.SetError(nameTextBox, string.Empty);
113    }
114    protected virtual void nameTextBox_KeyDown(object sender, KeyEventArgs e) {
115      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
116        nameLabel.Focus();  // set focus on label to validate data
117      if (e.KeyCode == Keys.Escape) {
118        nameTextBox.Text = Content.Name;
119        nameLabel.Focus();  // set focus on label to validate data
120      }
121    }
122    protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
123      if (Content.CanChangeDescription)
124        Content.Description = descriptionTextBox.Text;
125    }
126
127    protected void descriptionTextBox_DoubleClick(object sender, EventArgs e) {
128      using (TextDialog dialog = new TextDialog("Description of " + Content.Name, Content.Description, !Content.CanChangeDescription)) {
129        if (dialog.ShowDialog(this) == DialogResult.OK)
130          Content.Description = dialog.Content;
131      }
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.