Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization.Views/3.3/ResultsProducingItemView.cs

Last change on this file was 17957, checked in by mkommend, 4 years ago

#2521: Fixed bugs in resultsProducingItemView (toString changes of items, removing of parametes / results not working).

File size: 12.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Optimization.Views {
33
34  [View("Results Producing Item View")]
35  [Content(typeof(IResultsProducingItem), true)]
36  public partial class ResultsProducingItemView : AsynchronousContentView {
37    public ResultsProducingItemView() {
38      InitializeComponent();
39
40      parameterGroup = listView.Groups["parametersGroup"];
41      resultsGroup = listView.Groups["resultsGroup"];
42    }
43
44    public new IResultsProducingItem Content {
45      get { return (IResultsProducingItem)base.Content; }
46      set { base.Content = value; }
47    }
48
49    private Dictionary<IItem, ListViewItem> itemToListViewItem = new Dictionary<IItem, ListViewItem>();
50    private ListViewGroup parameterGroup;
51    private ListViewGroup resultsGroup;
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      listView.Items.Clear();
56      itemToListViewItem.Clear();
57      RebuildImageList();
58
59      viewHost.Content = null;
60
61      if (Content != null) {
62        FillListView();
63        AdjustListViewColumnSizes();
64      }
65    }
66
67    protected override void SetEnabledStateOfControls() {
68      base.SetEnabledStateOfControls();
69
70      if (Content == null) {
71        addButton.Enabled = false;
72        sortAscendingButton.Enabled = false;
73        sortDescendingButton.Enabled = false;
74        removeButton.Enabled = false;
75        listView.Enabled = false;
76        detailsGroupBox.Enabled = false;
77        return;
78      }
79
80      addButton.Enabled = !ReadOnly && !Content.Parameters.IsReadOnly && !((IKeyedItemCollection<string, IResult>)Content.Results).IsReadOnly;
81      sortAscendingButton.Enabled = listView.Items.Count > 1;
82      sortDescendingButton.Enabled = listView.Items.Count > 1;
83      removeButton.Enabled = !ReadOnly && !Content.Parameters.IsReadOnly && !((IKeyedItemCollection<string, IResult>)Content.Results).IsReadOnly;
84      listView.Enabled = true;
85      detailsGroupBox.Enabled = listView.SelectedItems.Count == 1;
86    }
87
88    #region Content event registration
89    protected override void RegisterContentEvents() {
90      base.RegisterContentEvents();
91      RegisterContentParametersEvents();
92      RegisterContentResultsEvents();
93    }
94    private void RegisterContentParametersEvents() {
95      Content.Parameters.ItemsAdded += ParametersOnItemsChanged;
96      Content.Parameters.ItemsRemoved += ParametersOnItemsRemoved;
97      Content.Parameters.ItemsReplaced += ParametersOnItemsChanged;
98      Content.Parameters.CollectionReset += ParametersOnItemsChanged;
99
100      foreach (var item in Content.Parameters) {
101        RegisterItemEvents(item);
102      }
103    }
104    private void RegisterContentResultsEvents() {
105      Content.Results.ItemsAdded += ResultsOnItemsChanged;
106      Content.Results.ItemsRemoved += ResultsOnItemsRemoved;
107      Content.Results.ItemsReplaced += ResultsOnItemsChanged;
108      Content.Results.CollectionReset += ResultsOnItemsChanged;
109
110      foreach (var item in Content.Results) {
111        RegisterItemEvents(item);
112      }
113    }
114    protected virtual void RegisterItemEvents(IItem item) {
115      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
116      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
117    }
118
119    protected override void DeregisterContentEvents() {
120      DeregisterContentParametersEvents();
121      DeregisterContentResultsEvents();
122
123      foreach (var item in itemToListViewItem.Keys) {
124        DeregisterItemEvents(item);
125      }
126      base.DeregisterContentEvents();
127    }
128    private void DeregisterContentParametersEvents() {
129      Content.Parameters.ItemsAdded -= ParametersOnItemsChanged;
130      Content.Parameters.ItemsRemoved -= ParametersOnItemsRemoved;
131      Content.Parameters.ItemsReplaced -= ParametersOnItemsChanged;
132      Content.Parameters.CollectionReset -= ParametersOnItemsChanged;
133    }
134    private void DeregisterContentResultsEvents() {
135      Content.Results.ItemsAdded -= ResultsOnItemsChanged;
136      Content.Results.ItemsRemoved -= ResultsOnItemsRemoved;
137      Content.Results.ItemsReplaced -= ResultsOnItemsChanged;
138      Content.Results.CollectionReset -= ResultsOnItemsChanged;
139    }
140    protected virtual void DeregisterItemEvents(IItem item) {
141      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
142      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
143    }
144    #endregion
145
146
147
148    private string selectedName;
149    private void FillListView() {
150      if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
151
152      FillListView(false);
153      if (listView.Items.Count > 0)
154        selectedName = null;
155    }
156
157
158    private void FillListView(bool resize = true) {
159      listView.BeginUpdate();
160      foreach (var item in listView.Items.OfType<ListViewItem>().OrderByDescending(x => x.ImageIndex).ToList()) {
161        listView.SmallImageList.Images.RemoveAt(item.ImageIndex);
162        listView.Items.Remove(item);
163      }
164      itemToListViewItem.Clear();
165      listView.EndUpdate();
166
167      if (Content == null) return;
168
169      //Fill list view with new content
170      foreach (var parameter in Content.Parameters) {
171        var listViewItem = CreateListViewItem(parameter, parameterGroup);
172        listView.Items.Add(listViewItem);
173        if ((selectedName != null) && parameter.Name.Equals(selectedName)) listViewItem.Selected = true;
174        itemToListViewItem[parameter] = listViewItem;
175      }
176
177      foreach (var result in Content.Results) {
178        var listViewItem = CreateListViewItem(result, resultsGroup);
179        listView.Items.Add(listViewItem);
180        if ((selectedName != null) && result.Name.Equals(selectedName)) listViewItem.Selected = true;
181        itemToListViewItem[result] = listViewItem;
182      }
183
184      if (resize && listView.Items.Count > 0) {
185        for (int i = 0; i < listView.Columns.Count; i++)
186          listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
187      }
188    }
189
190    private void ParametersOnItemsChanged(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
191      foreach (var item in e.OldItems) {
192        listView.Items.Remove(itemToListViewItem[item]);
193        itemToListViewItem.Remove(item);
194      }
195      foreach (var item in e.Items) {
196        var listViewItem = CreateListViewItem(item, parameterGroup);
197        RegisterItemEvents(item);
198        listView.Items.Add(listViewItem);
199        itemToListViewItem[item] = listViewItem;
200      }
201      AdjustListViewColumnSizes();
202    }
203    private void ParametersOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
204      var eventArgs = new CollectionItemsChangedEventArgs<IParameter>(new IParameter[0], e.Items);
205      ParametersOnItemsChanged(sender, eventArgs);
206    }
207
208    private void ResultsOnItemsChanged(object sender, CollectionItemsChangedEventArgs<IResult> e) {
209      foreach (var item in e.OldItems) {
210        listView.Items.Remove(itemToListViewItem[item]);
211        itemToListViewItem.Remove(item);
212      }
213      foreach (var item in e.Items) {
214        var listViewItem = CreateListViewItem(item, resultsGroup);
215        RegisterItemEvents(item);
216        listView.Items.Add(listViewItem);
217        itemToListViewItem[item] = listViewItem;
218      }
219      AdjustListViewColumnSizes();
220    }
221    private void ResultsOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IResult> e) {
222      var eventArgs = new CollectionItemsChangedEventArgs<IResult>(new IResult[0], e.Items);
223      ResultsOnItemsChanged(sender, eventArgs);
224    }
225
226
227    private ListViewItem CreateListViewItem(INamedItem value, ListViewGroup group) {
228      var item = new ListViewItem();
229      item.Text = value.ToString();
230      item.ToolTipText = value.Name + ": " + value.Description;
231      item.Tag = value;
232      item.Group = group;
233      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : value.ItemImage);
234      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
235      return item;
236    }
237
238    private void UpdateListViewItemImage(ListViewItem listViewItem) {
239      if (listViewItem == null) throw new ArgumentNullException();
240      var item = listViewItem.Tag as IItem;
241      int i = listViewItem.ImageIndex;
242      listView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
243      listViewItem.ImageIndex = -1;
244      listViewItem.ImageIndex = i;
245    }
246    private void UpdateListViewItemText(ListViewItem listViewItem) {
247      if (listViewItem == null) throw new ArgumentNullException();
248      var item = listViewItem.Tag as INamedItem;
249      listViewItem.Text = item.ToString();
250      listViewItem.ToolTipText = item.Name + ": " + item.Description;
251    }
252
253    #region listView events
254    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
255      if (!showDetailsCheckBox.Checked) return;
256
257      if (listView.SelectedItems.Count == 1) {
258        detailsGroupBox.Enabled = true;
259        viewHost.Content = listView.SelectedItems[0].Tag as IContent;
260      } else {
261        viewHost.Content = null;
262        detailsGroupBox.Enabled = false;
263      }
264    }
265    private void listView_DoubleClick(object sender, EventArgs e) {
266      if (listView.SelectedItems.Count == 1) {
267        IItem item = (IItem)listView.SelectedItems[0].Tag;
268        IContentView view = MainFormManager.MainForm.ShowContent(item);
269        if (view != null) {
270          view.ReadOnly = true;
271          view.Locked = Locked;
272        }
273      }
274    }
275
276    protected virtual void listView_Layout(object sender, LayoutEventArgs e) {
277      AdjustListViewColumnSizes();
278    }
279    protected virtual void listView_Resize(object sender, EventArgs e) {
280      AdjustListViewColumnSizes();
281    }
282    #endregion
283
284    #region Item Events
285    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
286      if (InvokeRequired)
287        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
288      else {
289        IItem item = (IItem)sender;
290        var listViewItem = itemToListViewItem[item];
291        UpdateListViewItemImage(listViewItem);
292      }
293    }
294
295    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
296      if (InvokeRequired)
297        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
298      else {
299        IItem item = (IItem)sender;
300        var listViewItem = itemToListViewItem[item];
301        UpdateListViewItemText(listViewItem);
302        if (listView.Columns.Count > 1)
303          AdjustListViewColumnSizes();
304      }
305    }
306    #endregion
307
308    #region Helper Methods
309    protected virtual void AdjustListViewColumnSizes() {
310      if (listView.Columns.Count == 1)
311        listView.Columns[0].Width = listView.ClientSize.Width;
312      else {
313        if (listView.Items.Count > 0) {
314          for (int i = 0; i < listView.Columns.Count; i++)
315            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
316        }
317      }
318    }
319    protected virtual void RebuildImageList() {
320      listView.SmallImageList.Images.Clear();
321      foreach (ListViewItem listViewItem in listView.Items) {
322        var item = listViewItem.Tag as IItem;
323        listView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
324        listViewItem.ImageIndex = listView.SmallImageList.Images.Count - 1;
325      }
326    }
327    #endregion
328  }
329}
330
Note: See TracBrowser for help on using the repository browser.