Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/OperatorBaseVariablesView.cs @ 1307

Last change on this file since 1307 was 776, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Core namespace (#331)

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