Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Core/OperatorBaseVariablesView.cs @ 4949

Last change on this file since 4949 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 7.1 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  public partial class OperatorBaseVariablesView : ViewBase {
32    private ChooseItemDialog chooseItemDialog;
33
34    public IOperator Operator {
35      get { return (IOperator)Item; }
36      set { base.Item = value; }
37    }
38
39    public OperatorBaseVariablesView() {
40      InitializeComponent();
41      variablesListView.Columns[0].Width = Math.Max(0, variablesListView.Width - 25);
42      Caption = "Operator";
43    }
44    public OperatorBaseVariablesView(IOperator op)
45      : this() {
46      Operator = op;
47    }
48
49    protected override void RemoveItemEvents() {
50      Operator.VariableAdded -= new EventHandler<VariableEventArgs>(OperatorBase_VariableAdded);
51      Operator.VariableRemoved -= new EventHandler<VariableEventArgs>(OperatorBase_VariableRemoved);
52      base.RemoveItemEvents();
53    }
54    protected override void AddItemEvents() {
55      base.AddItemEvents();
56      Operator.VariableAdded += new EventHandler<VariableEventArgs>(OperatorBase_VariableAdded);
57      Operator.VariableRemoved += new EventHandler<VariableEventArgs>(OperatorBase_VariableRemoved);
58    }
59
60    protected override void UpdateControls() {
61      base.UpdateControls();
62      variableDetailsGroupBox.Controls.Clear();
63      if (Operator == null) {
64        Caption = "Operator";
65        variablesListView.Enabled = false;
66        variableDetailsGroupBox.Enabled = false;
67        removeVariableButton.Enabled = false;
68      } else {
69        Caption = Operator.Name + " (" + Operator.GetType().Name + ")";
70        variablesListView.Enabled = true;
71        foreach (ListViewItem item in variablesListView.Items) {
72          ((IVariable)item.Tag).NameChanged -= new EventHandler(Variable_NameChanged);
73        }
74        variablesListView.Items.Clear();
75        foreach (IVariable variable in Operator.Variables) {
76          ListViewItem item = new ListViewItem();
77          item.Text = variable.Name;
78          item.Tag = variable;
79          variablesListView.Items.Add(item);
80          variable.NameChanged += new EventHandler(Variable_NameChanged);
81        }
82        if (variablesListView.Items.Count > 0)
83          variablesListView.SelectedIndices.Add(0);
84      }
85    }
86
87    private void variablesListView_SelectedIndexChanged(object sender, EventArgs e) {
88      if (variableDetailsGroupBox.Controls.Count > 0)
89        variableDetailsGroupBox.Controls[0].Dispose();
90      variableDetailsGroupBox.Controls.Clear();
91      variableDetailsGroupBox.Enabled = false;
92      removeVariableButton.Enabled = false;
93      if (variablesListView.SelectedItems.Count > 0) {
94        removeVariableButton.Enabled = true;
95      }
96      if (variablesListView.SelectedItems.Count == 1) {
97        IVariable variable = (IVariable)variablesListView.SelectedItems[0].Tag;
98        Control editor = (Control)variable.CreateView();
99        variableDetailsGroupBox.Controls.Add(editor);
100        editor.Dock = DockStyle.Fill;
101        variableDetailsGroupBox.Enabled = true;
102      }
103    }
104
105    #region Size Changed Events
106    private void variablesListView_SizeChanged(object sender, EventArgs e) {
107      if (variablesListView.Columns.Count > 0)
108        variablesListView.Columns[0].Width = Math.Max(0, variablesListView.Width - 25);
109    }
110    #endregion
111
112    #region Key Events
113    private void variablesListView_KeyDown(object sender, KeyEventArgs e) {
114      if (e.KeyCode == Keys.Delete) {
115        if (variablesListView.SelectedItems.Count > 0) {
116          foreach (ListViewItem item in variablesListView.SelectedItems)
117            Operator.RemoveVariable(((IVariable)item.Tag).Name);
118        }
119      }
120    }
121    #endregion
122
123    #region Button Events
124    private void addVariableButton_Click(object sender, EventArgs e) {
125      if (chooseItemDialog == null) {
126        chooseItemDialog = new ChooseItemDialog();
127        chooseItemDialog.Caption = "Add Variable";
128      }
129      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
130        IVariable newVariable = new Variable(chooseItemDialog.Item.GetType().Name, chooseItemDialog.Item);
131        int index = 1;
132        bool valid = true;
133        string name = null;
134        do {
135          valid = true;
136          name = newVariable.Name + " (" + index.ToString() + ")";
137          foreach (IVariable existingVariable in Operator.Variables) {
138            if (existingVariable.Name == name) {
139              valid = false;
140              index++;
141            }
142          }
143        } while (!valid);
144        newVariable.Name = name;
145        Operator.AddVariable(newVariable);
146      }
147    }
148    private void removeVariableButton_Click(object sender, EventArgs e) {
149      if (variablesListView.SelectedItems.Count > 0) {
150        foreach (ListViewItem item in variablesListView.SelectedItems)
151          Operator.RemoveVariable(((IVariable)item.Tag).Name);
152      }
153    }
154    #endregion
155
156    #region Variable Events
157    private void Variable_NameChanged(object sender, EventArgs e) {
158      IVariable variable = (IVariable)sender;
159      foreach (ListViewItem item in variablesListView.Items) {
160        if (item.Tag == variable)
161          item.Text = variable.Name;
162      }
163    }
164    #endregion
165
166    #region OperatorBase Events
167    private void OperatorBase_VariableAdded(object sender, VariableEventArgs e) {
168      ListViewItem item = new ListViewItem();
169      item.Text = e.Variable.Name;
170      item.Tag = e.Variable;
171      variablesListView.Items.Add(item);
172      // select the new item
173      foreach(ListViewItem oldSelected in variablesListView.SelectedItems) {
174        oldSelected.Selected = false;
175      }
176      item.Selected = true;
177      e.Variable.NameChanged += new EventHandler(Variable_NameChanged);
178    }
179    private void OperatorBase_VariableRemoved(object sender, VariableEventArgs e) {
180      ListViewItem itemToDelete = null;
181      foreach (ListViewItem item in variablesListView.Items) {
182        if (item.Tag == e.Variable)
183          itemToDelete = item;
184      }
185      e.Variable.NameChanged -= new EventHandler(Variable_NameChanged);
186      variablesListView.Items.Remove(itemToDelete);
187    }
188    #endregion
189  }
190}
Note: See TracBrowser for help on using the repository browser.