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