Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Core/3.2/OperatorBaseVariablesView.cs @ 3928

Last change on this file since 3928 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

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