Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Optimization.Views/3.3/RunView.cs @ 14469

Last change on this file since 14469 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 12.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Optimization.Views {
34  /// <summary>
35  /// The visual representation of a <see cref="Variable"/>.
36  /// </summary>
37  [View("Run View")]
38  [Content(typeof(IRun), true)]
39  public sealed partial class RunView : NamedItemView {
40    private readonly Dictionary<string, ListViewItem> parametersItemToListViewItem;
41    private readonly Dictionary<string, ListViewItem> resultsItemToListViewItem;
42    /// <summary>
43    /// Gets or sets the variable to represent visually.
44    /// </summary>
45    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
46    /// No own data storage present.</remarks>
47    public new IRun Content {
48      get { return (IRun)base.Content; }
49      set { base.Content = value; }
50    }
51
52    /// <summary>
53    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
54    /// </summary>
55    public RunView() {
56      InitializeComponent();
57      parametersItemToListViewItem = new Dictionary<string, ListViewItem>();
58      resultsItemToListViewItem = new Dictionary<string, ListViewItem>();
59    }
60
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      Content.PropertyChanged += Content_PropertyChanged;
64      RegisterContentParametersEvents();
65      RegisterContentResultsEents();
66    }
67    private void RegisterContentParametersEvents() {
68      Content.Parameters.ItemsAdded += ParametersOnItemsChanged;
69      Content.Parameters.ItemsRemoved += ParametersOnItemsRemoved;
70      Content.Parameters.ItemsReplaced += ParametersOnItemsChanged;
71      Content.Parameters.CollectionReset += ParametersOnItemsChanged;
72    }
73    private void RegisterContentResultsEents() {
74      Content.Results.ItemsAdded += ResultsOnItemsChanged;
75      Content.Results.ItemsRemoved += ResultsOnItemsRemoved;
76      Content.Results.ItemsReplaced += ResultsOnItemsChanged;
77      Content.Results.CollectionReset += ResultsOnItemsChanged;
78    }
79    protected override void DeregisterContentEvents() {
80      base.DeregisterContentEvents();
81      Content.PropertyChanged -= Content_PropertyChanged;
82      DeregisterContentParametersEvents();
83      DeregisterContentResultsEvents();
84    }
85    private void DeregisterContentParametersEvents() {
86      Content.Parameters.ItemsAdded -= ParametersOnItemsChanged;
87      Content.Parameters.ItemsRemoved -= ParametersOnItemsRemoved;
88      Content.Parameters.ItemsReplaced -= ParametersOnItemsChanged;
89      Content.Parameters.CollectionReset -= ParametersOnItemsChanged;
90    }
91    private void DeregisterContentResultsEvents() {
92      Content.Results.ItemsAdded -= ResultsOnItemsChanged;
93      Content.Results.ItemsRemoved -= ResultsOnItemsRemoved;
94      Content.Results.ItemsReplaced -= ResultsOnItemsChanged;
95      Content.Results.CollectionReset -= ResultsOnItemsChanged;
96    }
97
98    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
99      if (e.PropertyName == "Color") {
100        if (InvokeRequired) this.Invoke((Action)UpdateColor, null);
101        else UpdateColor();
102      } else if (e.PropertyName == "Parameters") {
103        if (InvokeRequired) this.Invoke((Action<bool>)FillParametersListView, true);
104        else FillParametersListView();
105        RegisterContentParametersEvents();
106      } else if (e.PropertyName == "Results") {
107        if (InvokeRequired) this.Invoke((Action<bool>)FillResultsListView, true);
108        else FillResultsListView();
109        RegisterContentResultsEents();
110      }
111    }
112
113    private void ParametersOnItemsChanged(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
114      foreach (var item in e.OldItems) {
115        listView.Items.Remove(parametersItemToListViewItem[item.Key]);
116        parametersItemToListViewItem.Remove(item.Key);
117      }
118      foreach (var item in e.Items) {
119        var listViewItem = CreateListViewItem(item.Key, Content.Parameters[item.Key], listView.Groups["parametersGroup"]);
120        listView.Items.Add(listViewItem);
121        parametersItemToListViewItem[item.Key] = listViewItem;
122      }
123    }
124
125    private void ParametersOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
126      foreach (var item in e.Items) {
127        listView.Items.Remove(parametersItemToListViewItem[item.Key]);
128        parametersItemToListViewItem.Remove(item.Key);
129      }
130    }
131
132    private void ResultsOnItemsChanged(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
133      foreach (var item in e.OldItems) {
134        listView.Items.Remove(resultsItemToListViewItem[item.Key]);
135        resultsItemToListViewItem.Remove(item.Key);
136      }
137      foreach (var item in e.Items) {
138        var listViewItem = CreateListViewItem(item.Key, Content.Results[item.Key], listView.Groups["resultsGroup"]);
139        listView.Items.Add(listViewItem);
140        resultsItemToListViewItem[item.Key] = listViewItem;
141      }
142    }
143
144    private void ResultsOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
145      foreach (var item in e.Items) {
146        listView.Items.Remove(resultsItemToListViewItem[item.Key]);
147        resultsItemToListViewItem.Remove(item.Key);
148      }
149    }
150
151    protected override void OnContentChanged() {
152      base.OnContentChanged();
153      viewHost.Content = null;
154      if (Content != null)
155        UpdateColor();
156      FillListView();
157    }
158
159    protected override void SetEnabledStateOfControls() {
160      base.SetEnabledStateOfControls();
161      listView.Enabled = Content != null;
162      detailsGroupBox.Enabled = (Content != null) && (listView.SelectedItems.Count == 1);
163      changeColorButton.Enabled = Content != null;
164      showAlgorithmButton.Enabled = Content != null && Content.Algorithm != null && !Locked;
165    }
166
167    protected override void PropagateStateChanges(Control control, Type type, System.Reflection.PropertyInfo propertyInfo) {
168      if (propertyInfo.Name == "ReadOnly") return;
169      base.PropagateStateChanges(control, type, propertyInfo);
170    }
171
172    private void changeColorButton_Click(object sender, EventArgs e) {
173      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
174        this.Content.Color = this.colorDialog.Color;
175      }
176    }
177    private void UpdateColor() {
178      this.colorDialog.Color = this.Content.Color;
179      this.colorArea.BackColor = this.Content.Color;
180    }
181
182    private string selectedName;
183    private void FillListView() {
184      if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
185
186      FillParametersListView(false);
187      FillResultsListView(false);
188      if (listView.Items.Count > 0) {
189        for (int i = 0; i < listView.Columns.Count; i++)
190          listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
191        selectedName = null;
192      }
193    }
194
195    private void FillParametersListView(bool resize = true) {
196      listView.BeginUpdate();
197      foreach (var item in listView.Groups["parametersGroup"].Items.OfType<ListViewItem>().OrderByDescending(x => x.ImageIndex).ToList()) {
198        listView.SmallImageList.Images.RemoveAt(item.ImageIndex);
199        listView.Items.Remove(item);
200      }
201      parametersItemToListViewItem.Clear();
202
203      var counter = 0;
204      foreach (var item in listView.Items.OfType<ListViewItem>().OrderBy(x => x.ImageIndex).ToList())
205        item.ImageIndex = counter++;
206      listView.EndUpdate();
207
208      if (Content != null) {
209        foreach (string key in Content.Parameters.Keys) {
210          var listViewItem = CreateListViewItem(key, Content.Parameters[key], listView.Groups["parametersGroup"]);
211          listView.Items.Add(listViewItem);
212          if ((selectedName != null) && key.Equals(selectedName)) listViewItem.Selected = true;
213          parametersItemToListViewItem[key] = listViewItem;
214        }
215        if (resize && listView.Items.Count > 0) {
216          for (int i = 0; i < listView.Columns.Count; i++)
217            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
218        }
219      }
220    }
221
222    private void FillResultsListView(bool resize = true) {
223      listView.BeginUpdate();
224      foreach (var item in listView.Groups["resultsGroup"].Items.OfType<ListViewItem>().OrderByDescending(x => x.ImageIndex).ToList()) {
225        listView.SmallImageList.Images.RemoveAt(item.ImageIndex);
226        listView.Items.Remove(item);
227      }
228      resultsItemToListViewItem.Clear();
229
230      var counter = 0;
231      foreach (var item in listView.Items.OfType<ListViewItem>().OrderBy(x => x.ImageIndex).ToList())
232        item.ImageIndex = counter++;
233      listView.EndUpdate();
234
235      if (Content != null) {
236        foreach (string key in Content.Results.Keys) {
237          var listViewItem = CreateListViewItem(key, Content.Results[key], listView.Groups["resultsGroup"]);
238          listView.Items.Add(listViewItem);
239          if ((selectedName != null) && key.Equals(selectedName)) listViewItem.Selected = true;
240          resultsItemToListViewItem[key] = listViewItem;
241        }
242        if (resize && listView.Items.Count > 0) {
243          for (int i = 0; i < listView.Columns.Count; i++)
244            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
245        }
246      }
247    }
248
249    private ListViewItem CreateListViewItem(string name, IItem value, ListViewGroup group) {
250      var item = new ListViewItem(new string[] { name, value != null ? value.ToString() : "-" });
251      item.Tag = value;
252      item.Group = group;
253      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : value.ItemImage);
254      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
255      return item;
256    }
257
258    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
259      if (showDetailsCheckBox.Checked) {
260        if (listView.SelectedItems.Count == 1) {
261          detailsGroupBox.Enabled = true;
262          viewHost.Content = listView.SelectedItems[0].Tag as IContent;
263        } else {
264          viewHost.Content = null;
265          detailsGroupBox.Enabled = false;
266        }
267      }
268    }
269    private void listView_DoubleClick(object sender, EventArgs e) {
270      if (listView.SelectedItems.Count == 1) {
271        IItem item = (IItem)listView.SelectedItems[0].Tag;
272        IContentView view = MainFormManager.MainForm.ShowContent(item);
273        if (view != null) {
274          view.ReadOnly = true;
275          view.Locked = Locked;
276        }
277      }
278    }
279    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
280      if (!Locked) {
281        ListViewItem listViewItem = (ListViewItem)e.Item;
282        IItem item = (IItem)listViewItem.Tag;
283        if (item != null) {
284          DataObject data = new DataObject();
285          data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
286          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
287        }
288      }
289    }
290    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
291      if (showDetailsCheckBox.Checked) {
292        splitContainer.Panel2Collapsed = false;
293        detailsGroupBox.Enabled = listView.SelectedItems.Count == 1;
294        viewHost.Content = listView.SelectedItems.Count == 1 ? (IContent)listView.SelectedItems[0].Tag : null;
295      } else {
296        splitContainer.Panel2Collapsed = true;
297        viewHost.Content = null;
298      }
299    }
300    private void showAlgorithmButton_Click(object sender, EventArgs e) {
301      if (!Locked) {
302        MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
303      }
304    }
305  }
306}
Note: See TracBrowser for help on using the repository browser.