Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 4.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.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), false)]
36  [Content(typeof(INamedItem), false)]
37  public partial class NamedItemView : ItemView {
38    public new INamedItem Content {
39      get { return (INamedItem)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public NamedItemView() {
44      InitializeComponent();
45      Caption = "NamedItem";
46      errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
47      errorProvider.SetIconPadding(nameTextBox, 2);
48    }
49    public NamedItemView(INamedItem content)
50      : this() {
51      Content = content;
52    }
53
54    protected override void DeregisterContentEvents() {
55      Content.NameChanged -= new EventHandler(Content_NameChanged);
56      Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
57      base.DeregisterContentEvents();
58    }
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.NameChanged += new EventHandler(Content_NameChanged);
62      Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (Content == null) {
68        Caption = "NamedItem";
69        nameTextBox.Text = "-";
70        nameTextBox.ReadOnly = false;
71        nameTextBox.Enabled = false;
72        descriptionTextBox.Text = "";
73        nameTextBox.ReadOnly = false;
74        descriptionTextBox.Enabled = false;
75      } else {
76        Caption = Content.Name + " (" + Content.GetType().Name + ")";
77        nameTextBox.Text = Content.Name;
78        nameTextBox.ReadOnly = !Content.CanChangeName;
79        nameTextBox.Enabled = true;
80        descriptionTextBox.Text = Content.Description;
81        descriptionTextBox.ReadOnly = !Content.CanChangeDescription;
82        descriptionTextBox.Enabled = true;
83      }
84    }
85
86    protected virtual void Content_NameChanged(object sender, EventArgs e) {
87      if (InvokeRequired)
88        Invoke(new EventHandler(Content_NameChanged), sender, e);
89      else
90        nameTextBox.Text = Content.Name;
91    }
92    protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
93      if (InvokeRequired)
94        Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
95      else
96        descriptionTextBox.Text = Content.Description;
97    }
98
99    protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
100      if (Content.CanChangeName) {
101        Content.Name = nameTextBox.Text;
102
103        // check if variable name was set successfully
104        if (!Content.Name.Equals(nameTextBox.Text)) {
105          e.Cancel = true;
106          errorProvider.SetError(nameTextBox, "Invalid Name");
107          nameTextBox.SelectAll();
108        }
109      }
110    }
111    protected virtual void nameTextBox_Validated(object sender, EventArgs e) {
112      errorProvider.SetError(nameTextBox, string.Empty);
113    }
114    protected virtual void nameTextBox_KeyDown(object sender, KeyEventArgs e) {
115      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
116        nameLabel.Focus();  // set focus on label to validate data
117      if (e.KeyCode == Keys.Escape) {
118        nameTextBox.Text = Content.Name;
119        nameLabel.Focus();  // set focus on label to validate data
120      }
121    }
122    protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
123      if (Content.CanChangeDescription)
124        Content.Description = descriptionTextBox.Text;
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.