Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2676 was 2676, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • continued work on adapting and refactoring HeuristicLab.Data according to the changes in HeuristicLab.Core
  • unified visual appearance of views
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Core.Views {
32  /// <summary>
33  /// The visual representation of a <see cref="Variable"/>.
34  /// </summary>
35  [Content(typeof(NamedItem), true)]
36  public partial class NamedItemView : ItemView {
37    public NamedItem NamedItem {
38      get { return (NamedItem)Item; }
39      set { base.Item = value; }
40    }
41
42    public NamedItemView() {
43      InitializeComponent();
44      Caption = "NamedItem";
45      errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
46      errorProvider.SetIconPadding(nameTextBox, 2);
47    }
48    public NamedItemView(NamedItem namedItem)
49      : this() {
50      NamedItem = namedItem;
51    }
52
53    protected override void DeregisterObjectEvents() {
54      NamedItem.NameChanged -= new EventHandler(NamedItem_NameChanged);
55      NamedItem.DescriptionChanged -= new EventHandler(NamedItem_DescriptionChanged);
56      base.DeregisterObjectEvents();
57    }
58    protected override void RegisterObjectEvents() {
59      base.RegisterObjectEvents();
60      NamedItem.NameChanged += new EventHandler(NamedItem_NameChanged);
61      NamedItem.DescriptionChanged += new EventHandler(NamedItem_DescriptionChanged);
62    }
63
64    protected override void OnObjectChanged() {
65      base.OnObjectChanged();
66      if (NamedItem == null) {
67        Caption = "NamedItem";
68        nameTextBox.Text = "-";
69        nameTextBox.ReadOnly = false;
70        nameTextBox.Enabled = false;
71        descriptionTextBox.Text = "";
72        nameTextBox.ReadOnly = false;
73        descriptionTextBox.Enabled = false;
74      } else {
75        Caption = NamedItem.Name + " (" + NamedItem.GetType().Name + ")";
76        nameTextBox.Text = NamedItem.Name;
77        nameTextBox.ReadOnly = !NamedItem.CanChangeName;
78        nameTextBox.Enabled = true;
79        descriptionTextBox.Text = NamedItem.Description;
80        descriptionTextBox.ReadOnly = !NamedItem.CanChangeDescription;
81        descriptionTextBox.Enabled = true;
82      }
83    }
84
85    protected virtual void NamedItem_NameChanged(object sender, EventArgs e) {
86      if (InvokeRequired)
87        Invoke(new EventHandler(NamedItem_NameChanged), sender, e);
88      else
89        nameTextBox.Text = NamedItem.Name;
90    }
91    protected virtual void NamedItem_DescriptionChanged(object sender, EventArgs e) {
92      if (InvokeRequired)
93        Invoke(new EventHandler(NamedItem_DescriptionChanged), sender, e);
94      else
95        descriptionTextBox.Text = NamedItem.Description;
96    }
97
98    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
99      if (NamedItem.CanChangeName) {
100        NamedItem.Name = nameTextBox.Text;
101
102        // check if variable name was set successfully
103        if (!NamedItem.Name.Equals(nameTextBox.Text)) {
104          e.Cancel = true;
105          errorProvider.SetError(nameTextBox, "Invalid Name");
106          nameTextBox.SelectAll();
107        }
108      }
109    }
110    protected virtual void nameTextBox_Validated(object sender, EventArgs e) {
111      errorProvider.SetError(nameTextBox, string.Empty);
112    }
113    protected virtual void nameTextBox_KeyDown(object sender, KeyEventArgs e) {
114      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
115        nameLabel.Focus();  // set focus on label to validate data
116      if (e.KeyCode == Keys.Escape) {
117        nameTextBox.Text = NamedItem.Name;
118        nameLabel.Focus();  // set focus on label to validate data
119      }
120    }
121    protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
122      if (NamedItem.CanChangeDescription)
123        NamedItem.Description = descriptionTextBox.Text;
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.