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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using HeuristicLab.MainForm;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Core.Views {
|
---|
28 | /// <summary>
|
---|
29 | /// The visual representation of a <see cref="Variable"/>.
|
---|
30 | /// </summary>
|
---|
31 | [Content(typeof(NamedItem), false)]
|
---|
32 | [Content(typeof(INamedItem), false)]
|
---|
33 | public partial class NamedItemView : ItemView {
|
---|
34 | public new INamedItem Content {
|
---|
35 | get { return (INamedItem)base.Content; }
|
---|
36 | set { base.Content = value; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public NamedItemView() {
|
---|
40 | InitializeComponent();
|
---|
41 | Caption = "NamedItem";
|
---|
42 | errorProvider.SetIconAlignment(nameTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
43 | errorProvider.SetIconPadding(nameTextBox, 2);
|
---|
44 | }
|
---|
45 | public NamedItemView(INamedItem content)
|
---|
46 | : this() {
|
---|
47 | Content = content;
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected override void DeregisterContentEvents() {
|
---|
51 | Content.NameChanged -= new EventHandler(Content_NameChanged);
|
---|
52 | Content.DescriptionChanged -= new EventHandler(Content_DescriptionChanged);
|
---|
53 | base.DeregisterContentEvents();
|
---|
54 | }
|
---|
55 | protected override void RegisterContentEvents() {
|
---|
56 | base.RegisterContentEvents();
|
---|
57 | Content.NameChanged += new EventHandler(Content_NameChanged);
|
---|
58 | Content.DescriptionChanged += new EventHandler(Content_DescriptionChanged);
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected override void OnContentChanged() {
|
---|
62 | base.OnContentChanged();
|
---|
63 | if (Content == null) {
|
---|
64 | Caption = "NamedItem";
|
---|
65 | nameTextBox.Text = "-";
|
---|
66 | nameTextBox.ReadOnly = false;
|
---|
67 | nameTextBox.Enabled = false;
|
---|
68 | descriptionTextBox.Text = "";
|
---|
69 | nameTextBox.ReadOnly = false;
|
---|
70 | descriptionTextBox.Enabled = false;
|
---|
71 | } else {
|
---|
72 | Caption = Content.Name + " (" + Content.GetType().Name + ")";
|
---|
73 | nameTextBox.Text = Content.Name;
|
---|
74 | nameTextBox.ReadOnly = !Content.CanChangeName;
|
---|
75 | nameTextBox.Enabled = true;
|
---|
76 | descriptionTextBox.Text = Content.Description;
|
---|
77 | descriptionTextBox.ReadOnly = !Content.CanChangeDescription;
|
---|
78 | descriptionTextBox.Enabled = true;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected virtual void Content_NameChanged(object sender, EventArgs e) {
|
---|
83 | if (InvokeRequired)
|
---|
84 | Invoke(new EventHandler(Content_NameChanged), sender, e);
|
---|
85 | else
|
---|
86 | nameTextBox.Text = Content.Name;
|
---|
87 | }
|
---|
88 | protected virtual void Content_DescriptionChanged(object sender, EventArgs e) {
|
---|
89 | if (InvokeRequired)
|
---|
90 | Invoke(new EventHandler(Content_DescriptionChanged), sender, e);
|
---|
91 | else
|
---|
92 | descriptionTextBox.Text = Content.Description;
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected virtual void nameTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
96 | if (Content.CanChangeName) {
|
---|
97 | Content.Name = nameTextBox.Text;
|
---|
98 |
|
---|
99 | // check if variable name was set successfully
|
---|
100 | if (!Content.Name.Equals(nameTextBox.Text)) {
|
---|
101 | e.Cancel = true;
|
---|
102 | errorProvider.SetError(nameTextBox, "Invalid Name");
|
---|
103 | nameTextBox.SelectAll();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | protected virtual void nameTextBox_Validated(object sender, EventArgs e) {
|
---|
108 | errorProvider.SetError(nameTextBox, string.Empty);
|
---|
109 | }
|
---|
110 | protected virtual void nameTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
111 | if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
|
---|
112 | nameLabel.Focus(); // set focus on label to validate data
|
---|
113 | if (e.KeyCode == Keys.Escape) {
|
---|
114 | nameTextBox.Text = Content.Name;
|
---|
115 | nameLabel.Focus(); // set focus on label to validate data
|
---|
116 | }
|
---|
117 | }
|
---|
118 | protected virtual void descriptionTextBox_Validated(object sender, EventArgs e) {
|
---|
119 | if (Content.CanChangeDescription)
|
---|
120 | Content.Description = descriptionTextBox.Text;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|