Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/VariablesScopeView.cs @ 3098

Last change on this file since 3098 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.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.Core {
32  /// <summary>
33  /// The visual representation of all variables in a specified scope.
34  /// </summary>
35  public partial class VariablesScopeView : ViewBase {
36    private ChooseItemDialog chooseItemDialog;
37
38    /// <summary>
39    /// Gets or sets the scope whose variables to represent visually.
40    /// </summary>
41    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
42    /// No won data storage present.</remarks>
43    public IScope Scope {
44      get { return (IScope)Item; }
45      set { base.Item = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of <see cref="VariablesScopeView"/> with caption "Variables Scope View".
50    /// </summary>
51    public VariablesScopeView() {
52      InitializeComponent();
53      Caption = "Variables Scope View";
54    }
55    /// <summary>
56    /// Initializes a new instance of <see cref="VariablesScopeView"/> with
57    /// the given <paramref name="scope"/>.
58    /// </summary>
59    /// <remarks>Calls <see cref="VariablesScopeView()"/>.</remarks>
60    /// <param name="scope">The scope whose variables should be represented visually.</param>
61    public VariablesScopeView(IScope scope)
62      : this() {
63      Scope = scope;
64    }
65
66    /// <summary>
67    /// Removes the eventhandlers from the underlying <see cref="IScope"/>.
68    /// </summary>
69    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
70    protected override void RemoveItemEvents() {
71      Scope.VariableAdded -= new EventHandler<EventArgs<IVariable>>(Scope_VariableAdded);
72      Scope.VariableRemoved -= new EventHandler<EventArgs<IVariable>>(Scope_VariableRemoved);
73      base.RemoveItemEvents();
74    }
75    /// <summary>
76    /// Adds eventhandlers to the underlying <see cref="IScope"/>.
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      Scope.VariableAdded += new EventHandler<EventArgs<IVariable>>(Scope_VariableAdded);
82      Scope.VariableRemoved += new EventHandler<EventArgs<IVariable>>(Scope_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      detailsGroupBox.Controls.Clear();
92      detailsGroupBox.Enabled = false;
93      removeButton.Enabled = false;
94      if (Scope == null) {
95        Caption = "Variables Scope View";
96        variablesListView.Enabled = false;
97      } else {
98        Caption = "Variables Scope View of " + Scope.Name + " (" + Scope.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 Scope.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      }
112    }
113
114    private void variablesListView_SelectedIndexChanged(object sender, EventArgs e) {
115      if (detailsGroupBox.Controls.Count > 0)
116        detailsGroupBox.Controls[0].Dispose();
117      detailsGroupBox.Controls.Clear();
118      detailsGroupBox.Enabled = false;
119      removeButton.Enabled = false;
120      if (variablesListView.SelectedItems.Count > 0) {
121        removeButton.Enabled = true;
122      }
123      if (variablesListView.SelectedItems.Count == 1) {
124        IVariable variable = (IVariable)variablesListView.SelectedItems[0].Tag;
125        Control control = (Control)new VariableView(variable);
126        detailsGroupBox.Controls.Add(control);
127        control.Dock = DockStyle.Fill;
128        detailsGroupBox.Enabled = true;
129      }
130    }
131
132    #region Size Changed Events
133    private void variablesListView_SizeChanged(object sender, EventArgs e) {
134      if (variablesListView.Columns.Count > 0)
135        variablesListView.Columns[0].Width = Math.Max(0, variablesListView.Width - 25);
136    }
137    #endregion
138
139    #region Key Events
140    private void variablesListView_KeyDown(object sender, KeyEventArgs e) {
141      if (e.KeyCode == Keys.Delete) {
142        if (variablesListView.SelectedItems.Count > 0) {
143          foreach (ListViewItem item in variablesListView.SelectedItems)
144            Scope.RemoveVariable(((IVariable)item.Tag).Name);
145        }
146      }
147    }
148    #endregion
149
150    #region Button Events
151    private void addButton_Click(object sender, EventArgs e) {
152      if (chooseItemDialog == null) {
153        chooseItemDialog = new ChooseItemDialog();
154        chooseItemDialog.Caption = "Add Variable";
155      }
156      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
157        IVariable newVariable = new Variable(chooseItemDialog.Item.GetType().Name, chooseItemDialog.Item);
158        int index = 1;
159        bool valid = true;
160        string name = null;
161        do {
162          valid = true;
163          name = newVariable.Name + " (" + index.ToString() + ")";
164          foreach (IVariable existingVariable in Scope.Variables) {
165            if (existingVariable.Name == name) {
166              valid = false;
167              index++;
168            }
169          }
170        } while (!valid);
171        newVariable.Name = name;
172        Scope.AddVariable(newVariable);
173      }
174    }
175    private void removeButton_Click(object sender, EventArgs e) {
176      if (variablesListView.SelectedItems.Count > 0) {
177        foreach (ListViewItem item in variablesListView.SelectedItems)
178          Scope.RemoveVariable(((IVariable)item.Tag).Name);
179      }
180    }
181    #endregion
182
183    #region Scope Events
184    private delegate void OnVariableEventDelegate(object sender, EventArgs<IVariable> e);
185    private void Scope_VariableAdded(object sender, EventArgs<IVariable> e) {
186      if (InvokeRequired)
187        Invoke(new OnVariableEventDelegate(Scope_VariableAdded), sender, e);
188      else {
189        ListViewItem item = new ListViewItem();
190        item.Text = e.Value.Name;
191        item.Tag = e.Value;
192        variablesListView.Items.Add(item);
193        e.Value.NameChanged += new EventHandler(Variable_NameChanged);
194      }
195    }
196    private void Scope_VariableRemoved(object sender, EventArgs<IVariable> e) {
197      if (InvokeRequired)
198        Invoke(new OnVariableEventDelegate(Scope_VariableRemoved), sender, e);
199      else {
200        ListViewItem itemToDelete = null;
201        foreach (ListViewItem item in variablesListView.Items) {
202          if (item.Tag == e.Value)
203            itemToDelete = item;
204        }
205        e.Value.NameChanged -= new EventHandler(Variable_NameChanged);
206        variablesListView.Items.Remove(itemToDelete);
207      }
208    }
209    #endregion
210
211    #region Variable Events
212    private delegate void OnEventDelegate(object sender, EventArgs e);
213    private void Variable_NameChanged(object sender, EventArgs e) {
214      if (InvokeRequired)
215        Invoke(new OnEventDelegate(Variable_NameChanged), sender, e);
216      else {
217        IVariable variable = (IVariable)sender;
218        foreach (ListViewItem item in variablesListView.Items) {
219          if (item.Tag == variable)
220            item.Text = variable.Name;
221        }
222      }
223    }
224    #endregion
225  }
226}
227
Note: See TracBrowser for help on using the repository browser.