Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Core/3.2/OperatorBaseVariableInfosView.cs @ 2605

Last change on this file since 2605 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 7.9 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.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.Core {
32  /// <summary>
33  /// The visual representation of the information of the variables of an operator.
34  /// </summary>
35  public partial class OperatorBaseVariableInfosView : ViewBase {
36    /// <summary>
37    /// Gets or sets the operator whose variable infos should be represented visually.
38    /// </summary>
39    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
40    /// No own data storage present.</remarks>
41    public IOperator Operator {
42      get { return (IOperator)Item; }
43      set { base.Item = value; }
44    }
45    /// <summary>
46    /// Gets all selected variable infos.
47    /// <note type="caution"> Variable infos are returned read-only!</note>
48    /// </summary>
49    public ICollection<IVariableInfo> SelectedVariableInfos {
50      get {
51        List<IVariableInfo> selected = new List<IVariableInfo>();
52        foreach (ListViewItem item in variableInfosListView.SelectedItems)
53          selected.Add((IVariableInfo)item.Tag);
54        return selected.AsReadOnly();
55      }
56    }
57
58    /// <summary>
59    /// Initializes a new instance of <see cref="OperatorBaseVariableInfosView"/> with caption "Operator".
60    /// </summary>
61    public OperatorBaseVariableInfosView() {
62      InitializeComponent();
63      variableInfosListView.Columns[0].Width = Math.Max(0, variableInfosListView.Width - 25);
64      Caption = "Operator";
65    }
66    /// <summary>
67    /// Initializes a new instance of <see cref="OperatorBaseVariableInfosView"/> with the given operator
68    /// <paramref name="op"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="OperatorBaseVariableInfosView()"/>.</remarks>
71    /// <param name="op">The operator whose variable infos should be displayed.</param>
72    public OperatorBaseVariableInfosView(IOperator op)
73      : this() {
74      Operator = op;
75    }
76
77    /// <summary>
78    /// Removes the eventhandlers from the underlying <see cref="IOperator"/>.
79    /// </summary>
80    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
81    protected override void RemoveItemEvents() {
82      Operator.VariableInfoAdded -= new EventHandler<EventArgs<IVariableInfo>>(OperatorBase_VariableInfoAdded);
83      Operator.VariableInfoRemoved -= new EventHandler<EventArgs<IVariableInfo>>(OperatorBase_VariableInfoRemoved);
84      base.RemoveItemEvents();
85    }
86    /// <summary>
87    /// Adds eventhandlers to the underlying <see cref="IOperator"/>.
88    /// </summary>
89    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
90    protected override void AddItemEvents() {
91      base.AddItemEvents();
92      Operator.VariableInfoAdded += new EventHandler<EventArgs<IVariableInfo>>(OperatorBase_VariableInfoAdded);
93      Operator.VariableInfoRemoved += new EventHandler<EventArgs<IVariableInfo>>(OperatorBase_VariableInfoRemoved);
94    }
95
96    /// <summary>
97    /// Updates all controls with the latest data of the model.
98    /// </summary>
99    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
100    protected override void UpdateControls() {
101      base.UpdateControls();
102      variableInfoDetailsGroupBox.Controls.Clear();
103      if (Operator == null) {
104        Caption = "Operator";
105        variableInfosListView.Enabled = false;
106        variableInfoDetailsGroupBox.Enabled = false;
107      } else {
108        Caption = Operator.Name + " (" + Operator.GetType().Name + ")";
109        variableInfosListView.Enabled = true;
110        foreach (ListViewItem item in variableInfosListView.Items) {
111          ((IVariableInfo)item.Tag).ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);
112        }
113        variableInfosListView.Items.Clear();
114        foreach (IVariableInfo variableInfo in Operator.VariableInfos) {
115          ListViewItem item = new ListViewItem();
116          item.Text = variableInfo.ActualName;
117          item.Tag = variableInfo;
118          variableInfosListView.Items.Add(item);
119          variableInfo.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
120        }
121        if (variableInfosListView.Items.Count > 0)
122          variableInfosListView.SelectedIndices.Add(0);
123      }
124    }
125
126    private void variableInfosListView_SelectedIndexChanged(object sender, EventArgs e) {
127      if (variableInfoDetailsGroupBox.Controls.Count > 0)
128        variableInfoDetailsGroupBox.Controls[0].Dispose();
129      variableInfoDetailsGroupBox.Controls.Clear();
130      variableInfoDetailsGroupBox.Enabled = false;
131      if (variableInfosListView.SelectedItems.Count == 1) {
132        IVariableInfo variableInfo = (IVariableInfo)variableInfosListView.SelectedItems[0].Tag;
133        Control control = (Control)variableInfo.CreateView();
134        variableInfoDetailsGroupBox.Controls.Add(control);
135        control.Dock = DockStyle.Fill;
136        variableInfoDetailsGroupBox.Enabled = true;
137      }
138      OnSelectedVariableInfosChanged();
139    }
140
141    /// <summary>
142    /// Occurs when the variables were changed, whose infos should be displayed.
143    /// </summary>
144    public event EventHandler SelectedVariableInfosChanged;
145    /// <summary>
146    /// Fires a new <c>SelectedVariableInfosChanged</c>.
147    /// </summary>
148    protected virtual void OnSelectedVariableInfosChanged() {
149      if (SelectedVariableInfosChanged != null)
150        SelectedVariableInfosChanged(this, new EventArgs());
151    }
152
153    #region Size Changed Events
154    private void variableInfosListView_SizeChanged(object sender, EventArgs e) {
155      if (variableInfosListView.Columns.Count > 0)
156        variableInfosListView.Columns[0].Width = Math.Max(0, variableInfosListView.Width - 25);
157    }
158    #endregion
159
160    #region VariableInfo Events
161    private void VariableInfo_ActualNameChanged(object sender, EventArgs e) {
162      IVariableInfo variableInfo = (IVariableInfo)sender;
163      foreach (ListViewItem item in variableInfosListView.Items) {
164        if (item.Tag == variableInfo)
165          item.Text = variableInfo.ActualName;
166      }
167    }
168    #endregion
169
170    #region OperatorBase Events
171    private void OperatorBase_VariableInfoAdded(object sender, EventArgs<IVariableInfo> e) {
172      ListViewItem item = new ListViewItem();
173      item.Text = e.Value.ActualName;
174      item.Tag = e.Value;
175      variableInfosListView.Items.Add(item);
176      e.Value.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
177    }
178    private void OperatorBase_VariableInfoRemoved(object sender, EventArgs<IVariableInfo> e) {
179      ListViewItem itemToDelete = null;
180      foreach (ListViewItem item in variableInfosListView.Items) {
181        if (item.Tag == e.Value)
182          itemToDelete = item;
183      }
184      e.Value.ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);
185      variableInfosListView.Items.Remove(itemToDelete);
186    }
187    #endregion
188  }
189}
Note: See TracBrowser for help on using the repository browser.