Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2714 was 2713, checked in by swagner, 15 years ago

Adapted HL 3.3 plugins according to changes in MainForm (#857)

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 new NamedItem Content {
38      get { return (NamedItem)base.Content; }
39      set { base.Content = 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      Content = namedItem;
51    }
52
53    protected override void DeregisterContentEvents() {
54      Content.NameChanged -= new EventHandler(Content_NameChanged);
55      Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
56      base.DeregisterContentEvents();
57    }
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      Content.NameChanged += new EventHandler(Content_NameChanged);
61      Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      if (Content == 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 = Content.Name + " (" + Content.GetType().Name + ")";
76        nameTextBox.Text = Content.Name;
77        nameTextBox.ReadOnly = !Content.CanChangeName;
78        nameTextBox.Enabled = true;
79        descriptionTextBox.Text = Content.Description;
80        descriptionTextBox.ReadOnly = !Content.CanChangeDescription;
81        descriptionTextBox.Enabled = true;
82      }
83    }
84
85    protected virtual void Content_NameChanged(object sender, EventArgs e) {
86      if (InvokeRequired)
87        Invoke(new EventHandler(Content_NameChanged), sender, e);
88      else
89        nameTextBox.Text = Content.Name;
90    }
91    protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
92      if (InvokeRequired)
93        Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
94      else
95        descriptionTextBox.Text = Content.Description;
96    }
97
98    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
99      if (Content.CanChangeName) {
100        Content.Name = nameTextBox.Text;
101
102        // check if variable name was set successfully
103        if (!Content.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 = Content.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 (Content.CanChangeDescription)
123        Content.Description = descriptionTextBox.Text;
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.