Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Core.Views/3.3/NamedItemView.cs @ 16961

Last change on this file since 16961 was 3764, checked in by mkommend, 14 years ago

adapted view captions (ticket #893)

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