Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented first version of View.ReadOnly and adapted some views to the new mechanism (ticket #973)

File size: 5.4 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        descriptionTextBox.Text = "";
69        toolTip.SetToolTip(descriptionTextBox, string.Empty);
70      } else {
71        Caption = Content.Name + " (" + Content.GetType().Name + ")";
72        nameTextBox.Text = Content.Name;
73        descriptionTextBox.Text = Content.Description;
74        toolTip.SetToolTip(descriptionTextBox, Content.Description);
75      }
76      SetEnableStateOfControls();
77    }
78
79    protected override void OnReadOnlyChanged() {
80      base.OnReadOnlyChanged();
81      SetEnableStateOfControls();
82    }
83    private void SetEnableStateOfControls() {
84      if (Content == null) {
85        nameTextBox.Enabled = false;
86        descriptionTextBox.Enabled = false;
87      } else {
88        nameTextBox.Enabled = true;
89        nameTextBox.ReadOnly = ReadOnly || !Content.CanChangeName; ;
90        descriptionTextBox.Enabled = true;
91        descriptionTextBox.ReadOnly = ReadOnly || !Content.CanChangeDescription;
92      }
93    }
94
95    protected virtual void Content_NameChanged(object sender, EventArgs e) {
96      if (InvokeRequired)
97        Invoke(new EventHandler(Content_NameChanged), sender, e);
98      else
99        nameTextBox.Text = Content.Name;
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        Content.Name = nameTextBox.Text;
113
114        // check if variable name was set successfully
115        if (!Content.Name.Equals(nameTextBox.Text)) {
116          e.Cancel = true;
117          errorProvider.SetError(nameTextBox, "Invalid Name");
118          nameTextBox.SelectAll();
119        }
120      }
121    }
122    protected virtual void nameTextBox_Validated(object sender, EventArgs e) {
123      errorProvider.SetError(nameTextBox, string.Empty);
124    }
125    protected virtual void nameTextBox_KeyDown(object sender, KeyEventArgs e) {
126      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
127        nameLabel.Focus();  // set focus on label to validate data
128      if (e.KeyCode == Keys.Escape) {
129        nameTextBox.Text = Content.Name;
130        nameLabel.Focus();  // set focus on label to validate data
131      }
132    }
133    protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
134      if (Content.CanChangeDescription)
135        Content.Description = descriptionTextBox.Text;
136    }
137
138    protected void descriptionTextBox_DoubleClick(object sender, EventArgs e) {
139      using (TextDialog dialog = new TextDialog("Description of " + Content.Name, Content.Description, !Content.CanChangeDescription)) {
140        if (dialog.ShowDialog(this) == DialogResult.OK)
141          Content.Description = dialog.Content;
142      }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.