Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/VariablesScopeView.cs @ 417

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