Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2668 was 2664, checked in by swagner, 15 years ago

Abandoned policy that the names of all abstract base classes have to end in "Base" (#95)

File size: 4.3 KB
RevLine 
[2655]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>
[2664]35  [Content(typeof(NamedItem), true)]
36  public partial class NamedItemView : ItemView {
37    public NamedItem NamedItemBase {
38      get { return (NamedItem)Item; }
[2655]39      set { base.Item = value; }
40    }
41
[2664]42    public NamedItemView() {
[2655]43      InitializeComponent();
44      Caption = "NamedItem";
45    }
[2664]46    public NamedItemView(NamedItem namedItemBase)
[2655]47      : this() {
48      NamedItemBase = namedItemBase;
49    }
50
51    protected override void DeregisterObjectEvents() {
52      NamedItemBase.NameChanged -= new EventHandler(NamedItemBase_NameChanged);
53      NamedItemBase.DescriptionChanged -= new EventHandler(NamedItemBase_DescriptionChanged);
54      base.DeregisterObjectEvents();
55    }
56    protected override void RegisterObjectEvents() {
57      base.RegisterObjectEvents();
58      NamedItemBase.NameChanged += new EventHandler(NamedItemBase_NameChanged);
59      NamedItemBase.DescriptionChanged += new EventHandler(NamedItemBase_DescriptionChanged);
60    }
61
62    protected override void OnObjectChanged() {
63      base.OnObjectChanged();
64      if (NamedItemBase == null) {
65        Caption = "NamedItem";
66        nameTextBox.Text = "-";
67        nameTextBox.ReadOnly = false;
68        nameTextBox.Enabled = false;
69        descriptionTextBox.Text = "";
70        nameTextBox.ReadOnly = false;
71        descriptionTextBox.Enabled = false;
72      } else {
73        Caption = NamedItemBase.Name + " (" + NamedItemBase.GetType().Name + ")";
74        nameTextBox.Text = NamedItemBase.Name;
75        nameTextBox.ReadOnly = !NamedItemBase.CanChangeName;
76        nameTextBox.Enabled = true;
77        descriptionTextBox.Text = NamedItemBase.Description;
78        descriptionTextBox.ReadOnly = !NamedItemBase.CanChangeDescription;
79        descriptionTextBox.Enabled = true;
80      }
81    }
82
83    protected virtual void NamedItemBase_NameChanged(object sender, EventArgs e) {
84      if (InvokeRequired)
85        Invoke(new EventHandler(NamedItemBase_NameChanged), sender, e);
86      else
87        nameTextBox.Text = NamedItemBase.Name;
88    }
89    protected virtual void NamedItemBase_DescriptionChanged(object sender, EventArgs e) {
90      if (InvokeRequired)
91        Invoke(new EventHandler(NamedItemBase_DescriptionChanged), sender, e);
92      else
93        descriptionTextBox.Text = NamedItemBase.Description;
94    }
95
96    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
97      if (NamedItemBase.CanChangeName) {
98        string oldName = NamedItemBase.Name;
99        NamedItemBase.Name = nameTextBox.Text;
100
101        // check if variable name was set successfully
102        e.Cancel = !NamedItemBase.Name.Equals(nameTextBox.Text);
103        if (e.Cancel) {
104          MessageBox.Show(this, "\"" + nameTextBox.Text + "\" is not a valid name.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
105          nameTextBox.Text = oldName;
106          nameTextBox.SelectAll();
107          nameTextBox.Focus();
108        }
109      }
110    }
111    protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
112      if (NamedItemBase.CanChangeDescription)
113        NamedItemBase.Description = descriptionTextBox.Text;
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.