Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1721 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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