Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/OperatorBaseVariableInfosView.cs @ 2547

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