Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Views/3.3/RunView.cs @ 16796

Last change on this file since 16796 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 12.7 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3260]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;
[11522]23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
[3260]26using System.Windows.Forms;
[11522]27using HeuristicLab.Collections;
[3376]28using HeuristicLab.Common;
[3260]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")]
[3280]38  [Content(typeof(IRun), true)]
[3260]39  public sealed partial class RunView : NamedItemView {
[11522]40    private readonly Dictionary<string, ListViewItem> parametersItemToListViewItem;
41    private readonly Dictionary<string, ListViewItem> resultsItemToListViewItem;
[3260]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>
[3280]47    public new IRun Content {
48      get { return (IRun)base.Content; }
[3260]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();
[11522]57      parametersItemToListViewItem = new Dictionary<string, ListViewItem>();
58      resultsItemToListViewItem = new Dictionary<string, ListViewItem>();
[3260]59    }
60
[3638]61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
[11522]63      Content.PropertyChanged += Content_PropertyChanged;
64      RegisterContentParametersEvents();
65      RegisterContentResultsEents();
[3638]66    }
[11522]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    }
[3638]79    protected override void DeregisterContentEvents() {
80      base.DeregisterContentEvents();
[11522]81      Content.PropertyChanged -= Content_PropertyChanged;
82      DeregisterContentParametersEvents();
83      DeregisterContentResultsEvents();
[3638]84    }
[11522]85    private void DeregisterContentParametersEvents() {
86      Content.Parameters.ItemsAdded -= ParametersOnItemsChanged;
87      Content.Parameters.ItemsRemoved -= ParametersOnItemsRemoved;
88      Content.Parameters.ItemsReplaced -= ParametersOnItemsChanged;
89      Content.Parameters.CollectionReset -= ParametersOnItemsChanged;
[3638]90    }
[11522]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    }
[3638]97
[11522]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
[3260]151    protected override void OnContentChanged() {
152      base.OnContentChanged();
153      viewHost.Content = null;
[3764]154      if (Content != null)
[4096]155        UpdateColor();
[3775]156      FillListView();
[3904]157    }
[3764]158
[3904]159    protected override void SetEnabledStateOfControls() {
160      base.SetEnabledStateOfControls();
[3454]161      listView.Enabled = Content != null;
[4099]162      detailsGroupBox.Enabled = (Content != null) && (listView.SelectedItems.Count == 1);
[3638]163      changeColorButton.Enabled = Content != null;
[4102]164      showAlgorithmButton.Enabled = Content != null && Content.Algorithm != null && !Locked;
[3260]165    }
166
[7760]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
[3638]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    }
[4096]177    private void UpdateColor() {
[3638]178      this.colorDialog.Color = this.Content.Color;
[4096]179      this.colorArea.BackColor = this.Content.Color;
[3638]180    }
181
[4011]182    private string selectedName;
[3260]183    private void FillListView() {
[3775]184      if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
185
[11522]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
[3260]208      if (Content != null) {
[11522]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) {
[3260]216          for (int i = 0; i < listView.Columns.Count; i++)
217            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
218        }
219      }
220    }
221
[11522]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() : "-" });
[3341]251      item.Tag = value;
252      item.Group = group;
[5287]253      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : value.ItemImage);
[3341]254      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
[11522]255      return item;
[3341]256    }
257
[3260]258    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
[4096]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      }
[3260]268    }
[3300]269    private void listView_DoubleClick(object sender, EventArgs e) {
270      if (listView.SelectedItems.Count == 1) {
271        IItem item = (IItem)listView.SelectedItems[0].Tag;
[3557]272        IContentView view = MainFormManager.MainForm.ShowContent(item);
[3416]273        if (view != null) {
[3766]274          view.ReadOnly = true;
[3423]275          view.Locked = Locked;
[3416]276        }
[3300]277      }
278    }
279    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]280      if (!Locked) {
281        ListViewItem listViewItem = (ListViewItem)e.Item;
282        IItem item = (IItem)listViewItem.Tag;
[3731]283        if (item != null) {
284          DataObject data = new DataObject();
[5837]285          data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
[3731]286          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
287        }
[3432]288      }
[3300]289    }
[4096]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    }
[3300]300    private void showAlgorithmButton_Click(object sender, EventArgs e) {
[3454]301      if (!Locked) {
[3557]302        MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
[3416]303      }
[3300]304    }
[3260]305  }
306}
Note: See TracBrowser for help on using the repository browser.