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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Core.Views {
|
---|
33 | /// <summary>
|
---|
34 | /// The visual representation of the variables of an operator.
|
---|
35 | /// </summary>
|
---|
36 | public partial class OperatorBaseVariablesView : ViewBase {
|
---|
37 | private ChooseItemDialog chooseItemDialog;
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Gets or sets the operator whose variables to represent.
|
---|
41 | /// </summary>
|
---|
42 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
|
---|
43 | /// No own data storage present.</remarks>
|
---|
44 | public IOperator Operator {
|
---|
45 | get { return (IOperator)Item; }
|
---|
46 | set { base.Item = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Initializes a new instance of <see cref="OperatorBaseVariablesView"/> with caption "Operator".
|
---|
51 | /// </summary>
|
---|
52 | public OperatorBaseVariablesView() {
|
---|
53 | InitializeComponent();
|
---|
54 | variablesListView.Columns[0].Width = Math.Max(0, variablesListView.Width - 25);
|
---|
55 | Caption = "Operator";
|
---|
56 | }
|
---|
57 | /// <summary>
|
---|
58 | /// Initializes a new instance of <see cref="OperatorBaseVariablesView"/> with the given
|
---|
59 | /// operator <paramref name="op"/>.
|
---|
60 | /// </summary>
|
---|
61 | /// <remarks>Calls <see cref="OperatorBaseVariablesView"/>.</remarks>
|
---|
62 | /// <param name="op">The operator whose variables should be represented visually.</param>
|
---|
63 | public OperatorBaseVariablesView(IOperator op)
|
---|
64 | : this() {
|
---|
65 | Operator = op;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /// <summary>
|
---|
69 | /// Removes the eventhandlers from the unterlying <see cref="IOperator"/>.
|
---|
70 | /// </summary>
|
---|
71 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
72 | protected override void RemoveItemEvents() {
|
---|
73 | Operator.VariableAdded -= new EventHandler<EventArgs<IVariable>>(OperatorBase_VariableAdded);
|
---|
74 | Operator.VariableRemoved -= new EventHandler<EventArgs<IVariable>>(OperatorBase_VariableRemoved);
|
---|
75 | base.RemoveItemEvents();
|
---|
76 | }
|
---|
77 | /// <summary>
|
---|
78 | /// Adds eventhandlers to the underlying <see cref="IOperator"/>.
|
---|
79 | /// </summary>
|
---|
80 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
81 | protected override void AddItemEvents() {
|
---|
82 | base.AddItemEvents();
|
---|
83 | Operator.VariableAdded += new EventHandler<EventArgs<IVariable>>(OperatorBase_VariableAdded);
|
---|
84 | Operator.VariableRemoved += new EventHandler<EventArgs<IVariable>>(OperatorBase_VariableRemoved);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /// <summary>
|
---|
88 | /// Updates all controls with the latest data of the model.
|
---|
89 | /// </summary>
|
---|
90 | /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
91 | protected override void UpdateControls() {
|
---|
92 | base.UpdateControls();
|
---|
93 | variableDetailsGroupBox.Controls.Clear();
|
---|
94 | if (Operator == null) {
|
---|
95 | Caption = "Operator";
|
---|
96 | variablesListView.Enabled = false;
|
---|
97 | variableDetailsGroupBox.Enabled = false;
|
---|
98 | removeVariableButton.Enabled = false;
|
---|
99 | } else {
|
---|
100 | Caption = Operator.Name + " (" + Operator.GetType().Name + ")";
|
---|
101 | variablesListView.Enabled = true;
|
---|
102 | foreach (ListViewItem item in variablesListView.Items) {
|
---|
103 | ((IVariable)item.Tag).NameChanged -= new EventHandler(Variable_NameChanged);
|
---|
104 | }
|
---|
105 | variablesListView.Items.Clear();
|
---|
106 | foreach (IVariable variable in Operator.Variables) {
|
---|
107 | ListViewItem item = new ListViewItem();
|
---|
108 | item.Text = variable.Name;
|
---|
109 | item.Tag = variable;
|
---|
110 | variablesListView.Items.Add(item);
|
---|
111 | variable.NameChanged += new EventHandler(Variable_NameChanged);
|
---|
112 | }
|
---|
113 | if (variablesListView.Items.Count > 0)
|
---|
114 | variablesListView.SelectedIndices.Add(0);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void variablesListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
119 | if (variableDetailsGroupBox.Controls.Count > 0)
|
---|
120 | variableDetailsGroupBox.Controls[0].Dispose();
|
---|
121 | variableDetailsGroupBox.Controls.Clear();
|
---|
122 | variableDetailsGroupBox.Enabled = false;
|
---|
123 | removeVariableButton.Enabled = false;
|
---|
124 | if (variablesListView.SelectedItems.Count > 0) {
|
---|
125 | removeVariableButton.Enabled = true;
|
---|
126 | }
|
---|
127 | if (variablesListView.SelectedItems.Count == 1) {
|
---|
128 | IVariable variable = (IVariable)variablesListView.SelectedItems[0].Tag;
|
---|
129 | Control editor = (Control)MainFormManager.CreateDefaultView(variable);
|
---|
130 | variableDetailsGroupBox.Controls.Add(editor);
|
---|
131 | editor.Dock = DockStyle.Fill;
|
---|
132 | variableDetailsGroupBox.Enabled = true;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | #region Size Changed Events
|
---|
137 | private void variablesListView_SizeChanged(object sender, EventArgs e) {
|
---|
138 | if (variablesListView.Columns.Count > 0)
|
---|
139 | variablesListView.Columns[0].Width = Math.Max(0, variablesListView.Width - 25);
|
---|
140 | }
|
---|
141 | #endregion
|
---|
142 |
|
---|
143 | #region Key Events
|
---|
144 | private void variablesListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
145 | if (e.KeyCode == Keys.Delete) {
|
---|
146 | if (variablesListView.SelectedItems.Count > 0) {
|
---|
147 | foreach (ListViewItem item in variablesListView.SelectedItems)
|
---|
148 | Operator.RemoveVariable(((IVariable)item.Tag).Name);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | #endregion
|
---|
153 |
|
---|
154 | #region Button Events
|
---|
155 | private void addVariableButton_Click(object sender, EventArgs e) {
|
---|
156 | if (chooseItemDialog == null) {
|
---|
157 | chooseItemDialog = new ChooseItemDialog();
|
---|
158 | chooseItemDialog.Caption = "Add Variable";
|
---|
159 | }
|
---|
160 | if (chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
161 | IVariable newVariable = new Variable(chooseItemDialog.Item.GetType().Name, chooseItemDialog.Item);
|
---|
162 | int index = 1;
|
---|
163 | bool valid = true;
|
---|
164 | string name = null;
|
---|
165 | do {
|
---|
166 | valid = true;
|
---|
167 | name = newVariable.Name + " (" + index.ToString() + ")";
|
---|
168 | foreach (IVariable existingVariable in Operator.Variables) {
|
---|
169 | if (existingVariable.Name == name) {
|
---|
170 | valid = false;
|
---|
171 | index++;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | } while (!valid);
|
---|
175 | newVariable.Name = name;
|
---|
176 | Operator.AddVariable(newVariable);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | private void removeVariableButton_Click(object sender, EventArgs e) {
|
---|
180 | if (variablesListView.SelectedItems.Count > 0) {
|
---|
181 | foreach (ListViewItem item in variablesListView.SelectedItems)
|
---|
182 | Operator.RemoveVariable(((IVariable)item.Tag).Name);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | #endregion
|
---|
186 |
|
---|
187 | #region Variable Events
|
---|
188 | private void Variable_NameChanged(object sender, EventArgs e) {
|
---|
189 | IVariable variable = (IVariable)sender;
|
---|
190 | foreach (ListViewItem item in variablesListView.Items) {
|
---|
191 | if (item.Tag == variable)
|
---|
192 | item.Text = variable.Name;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | #endregion
|
---|
196 |
|
---|
197 | #region OperatorBase Events
|
---|
198 | private void OperatorBase_VariableAdded(object sender, EventArgs<IVariable> e) {
|
---|
199 | ListViewItem item = new ListViewItem();
|
---|
200 | item.Text = e.Value.Name;
|
---|
201 | item.Tag = e.Value;
|
---|
202 | variablesListView.Items.Add(item);
|
---|
203 | // select the new item
|
---|
204 | foreach(ListViewItem oldSelected in variablesListView.SelectedItems) {
|
---|
205 | oldSelected.Selected = false;
|
---|
206 | }
|
---|
207 | item.Selected = true;
|
---|
208 | e.Value.NameChanged += new EventHandler(Variable_NameChanged);
|
---|
209 | }
|
---|
210 | private void OperatorBase_VariableRemoved(object sender, EventArgs<IVariable> e) {
|
---|
211 | ListViewItem itemToDelete = null;
|
---|
212 | foreach (ListViewItem item in variablesListView.Items) {
|
---|
213 | if (item.Tag == e.Value)
|
---|
214 | itemToDelete = item;
|
---|
215 | }
|
---|
216 | e.Value.NameChanged -= new EventHandler(Variable_NameChanged);
|
---|
217 | variablesListView.Items.Remove(itemToDelete);
|
---|
218 | }
|
---|
219 | #endregion
|
---|
220 | }
|
---|
221 | }
|
---|