Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/VariablesScopeView.cs @ 2555

Last change on this file since 2555 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

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