Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Core/VariablesScopeView.cs @ 771

Last change on this file since 771 was 638, checked in by gkronber, 16 years ago

created a branch for changes needed to run HL3 on Mono 2.0
(ticket #298)

File size: 7.3 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          if (!variable.Name.StartsWith("##")) {
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        }
83      }
84    }
85
86    private void variablesListView_SelectedIndexChanged(object sender, EventArgs e) {
87      if (detailsGroupBox.Controls.Count > 0)
88        detailsGroupBox.Controls[0].Dispose();
89      detailsGroupBox.Controls.Clear();
90      detailsGroupBox.Enabled = false;
91      removeButton.Enabled = false;
92      if (variablesListView.SelectedItems.Count > 0) {
93        removeButton.Enabled = true;
94      }
95      if (variablesListView.SelectedItems.Count == 1) {
96        IVariable variable = (IVariable)variablesListView.SelectedItems[0].Tag;
97        Control control = (Control)new VariableView(variable);
98        detailsGroupBox.Controls.Add(control);
99        control.Dock = DockStyle.Fill;
100        detailsGroupBox.Enabled = true;
101      }
102    }
103
104    #region Size Changed Events
105    private void variablesListView_SizeChanged(object sender, EventArgs e) {
106      if (variablesListView.Columns.Count > 0)
107        variablesListView.Columns[0].Width = Math.Max(0, variablesListView.Width - 25);
108    }
109    #endregion
110
111    #region Key Events
112    private void variablesListView_KeyDown(object sender, KeyEventArgs e) {
113      if (e.KeyCode == Keys.Delete) {
114        if (variablesListView.SelectedItems.Count > 0) {
115          foreach (ListViewItem item in variablesListView.SelectedItems)
116            Scope.RemoveVariable(((IVariable)item.Tag).Name);
117        }
118      }
119    }
120    #endregion
121
122    #region Button Events
123    private void addButton_Click(object sender, EventArgs e) {
124      if (chooseItemDialog == null) {
125        chooseItemDialog = new ChooseItemDialog();
126        chooseItemDialog.Caption = "Add Variable";
127      }
128      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
129        IVariable newVariable = new Variable(chooseItemDialog.Item.GetType().Name, chooseItemDialog.Item);
130        int index = 1;
131        bool valid = true;
132        string name = null;
133        do {
134          valid = true;
135          name = newVariable.Name + " (" + index.ToString() + ")";
136          foreach (IVariable existingVariable in Scope.Variables) {
137            if (existingVariable.Name == name) {
138              valid = false;
139              index++;
140            }
141          }
142        } while (!valid);
143        newVariable.Name = name;
144        Scope.AddVariable(newVariable);
145      }
146    }
147    private void removeButton_Click(object sender, EventArgs e) {
148      if (variablesListView.SelectedItems.Count > 0) {
149        foreach (ListViewItem item in variablesListView.SelectedItems)
150          Scope.RemoveVariable(((IVariable)item.Tag).Name);
151      }
152    }
153    #endregion
154
155    #region Scope Events
156    private delegate void OnVariableEventDelegate(object sender, VariableEventArgs e);
157    private void Scope_VariableAdded(object sender, VariableEventArgs e) {
158      if (InvokeRequired)
159        Invoke(new OnVariableEventDelegate(Scope_VariableAdded), sender, e);
160      else {
161        if (!e.Variable.Name.StartsWith("##")) {
162          ListViewItem item = new ListViewItem();
163          item.Text = e.Variable.Name;
164          item.Tag = e.Variable;
165          variablesListView.Items.Add(item);
166          e.Variable.NameChanged += new EventHandler(Variable_NameChanged);
167        }
168      }
169    }
170    private void Scope_VariableRemoved(object sender, VariableEventArgs e) {
171      if (InvokeRequired)
172        Invoke(new OnVariableEventDelegate(Scope_VariableRemoved), sender, e);
173      else {
174        if (!e.Variable.Name.StartsWith("##")) {
175          ListViewItem itemToDelete = null;
176          foreach (ListViewItem item in variablesListView.Items) {
177            if (item.Tag == e.Variable)
178              itemToDelete = item;
179          }
180          e.Variable.NameChanged -= new EventHandler(Variable_NameChanged);
181          variablesListView.Items.Remove(itemToDelete);
182        }
183      }
184    }
185    #endregion
186
187    #region Variable Events
188    private delegate void OnEventDelegate(object sender, EventArgs e);
189    private void Variable_NameChanged(object sender, EventArgs e) {
190      if (InvokeRequired)
191        Invoke(new OnEventDelegate(Variable_NameChanged), sender, e);
192      else {
193        IVariable variable = (IVariable)sender;
194        foreach (ListViewItem item in variablesListView.Items) {
195          if (item.Tag == variable)
196            item.Text = variable.Name;
197        }
198      }
199    }
200    #endregion
201  }
202}
203
Note: See TracBrowser for help on using the repository browser.