Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionView.cs @ 13181

Last change on this file since 13181 was 13181, checked in by abeham, 8 years ago

#2441: Fixed bug with aggregation view open while runs are being added

This also fixes a bug I noticed in that after every add to a RunCollection the batch update event was triggered. I had put that in FilterRuns which was called from multiple places.

File size: 23.5 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
[4068]22using System;
[5744]23using System.Collections;
[4068]24using System.Collections.Generic;
[11344]25using System.ComponentModel;
[4068]26using System.Drawing;
27using System.Linq;
28using System.Windows.Forms;
[3274]29using HeuristicLab.Collections;
[6527]30using HeuristicLab.Common;
[3393]31using HeuristicLab.Core;
[3260]32using HeuristicLab.Core.Views;
33using HeuristicLab.MainForm;
[3277]34using HeuristicLab.MainForm.WindowsForms;
[3260]35
36namespace HeuristicLab.Optimization.Views {
37  [View("RunCollection View")]
38  [Content(typeof(RunCollection), true)]
[3393]39  [Content(typeof(IItemCollection<IRun>), false)]
[4883]40  public sealed partial class RunCollectionView : ItemView {
[5237]41    private Dictionary<IRun, List<ListViewItem>> itemListViewItemMapping;
[5744]42    private bool validDragOperation;
[12692]43    private bool suppressUpdates;
[4883]44
[3393]45    public new IItemCollection<IRun> Content {
46      get { return (IItemCollection<IRun>)base.Content; }
[3277]47      set { base.Content = value; }
48    }
49
[3614]50    public RunCollection RunCollection {
51      get { return Content as RunCollection; }
52    }
53
[3277]54    public ListView ItemsListView {
55      get { return itemsListView; }
56    }
57
[3260]58    public RunCollectionView() {
59      InitializeComponent();
[3505]60      itemsGroupBox.Text = "Runs";
[5237]61      itemListViewItemMapping = new Dictionary<IRun, List<ListViewItem>>();
[6693]62      runCollectionModifiersListView.Evaluator = EvaluateModifications;
[3260]63    }
[3277]64
65    protected override void DeregisterContentEvents() {
[3280]66      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
67      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
68      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[12692]69      if (RunCollection != null)
70        RunCollection.UpdateOfRunsInProgressChanged -= new EventHandler(RunCollection_UpdateOfRunsInProgressChanged);
[5237]71      foreach (IRun run in itemListViewItemMapping.Keys) {
72        DeregisterItemEvents(run);
73      }
[3277]74      base.DeregisterContentEvents();
[3260]75    }
[3277]76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
[3280]78      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
79      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
80      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[12692]81      if (RunCollection != null)
82        RunCollection.UpdateOfRunsInProgressChanged += new EventHandler(RunCollection_UpdateOfRunsInProgressChanged);
[3277]83    }
[5237]84    private void DeregisterItemEvents(IRun item) {
85      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
86      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
[11344]87      item.PropertyChanged -= Item_PropertyChanged;
[3449]88    }
[5237]89    private void RegisterItemEvents(IRun item) {
90      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
91      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
[11344]92      item.PropertyChanged += Item_PropertyChanged;
[3449]93    }
[3277]94
[3507]95    protected override void OnInitialized(EventArgs e) {
96      base.OnInitialized(e);
97      var viewTypes = MainFormManager.GetViewTypes(typeof(RunCollection), true);
98      foreach (Type viewType in viewTypes.OrderBy(x => ViewAttribute.GetViewName(x))) {
99        if ((viewType != typeof(ItemCollectionView<IRun>)) && (viewType != typeof(ViewHost))) {
100          ToolStripMenuItem menuItem = new ToolStripMenuItem();
101          menuItem.Text = ViewAttribute.GetViewName(viewType);
102          menuItem.Tag = viewType;
103          menuItem.Click += new EventHandler(menuItem_Click);
104          analyzeRunsToolStripDropDownButton.DropDownItems.Add(menuItem);
105        }
106      }
107    }
108
[3277]109    protected override void OnContentChanged() {
110      base.OnContentChanged();
[3775]111
112      string selectedName = null;
113      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null))
114        selectedName = ((IRun)itemsListView.SelectedItems[0].Tag).Name;
115
[5237]116      itemsListView.Items.Clear();
117      itemListViewItemMapping.Clear();
118      RebuildImageList();
[3277]119      viewHost.Content = null;
120
121      if (Content != null) {
[3614]122        if (RunCollection != null) {
[3709]123          if (!tabControl.TabPages.Contains(constraintPage))
124            tabControl.TabPages.Add(constraintPage);
[3614]125          runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
126          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[6693]127          if (!tabControl.TabPages.Contains(modifiersPage))
128            tabControl.TabPages.Add(modifiersPage);
129          runCollectionModifiersListView.Content = RunCollection.Modifiers;
[3614]130        }
131        foreach (IRun item in Content) {
[3775]132          ListViewItem listViewItem = CreateListViewItem(item);
133          AddListViewItem(listViewItem);
134          if ((selectedName != null) && item.Name.Equals(selectedName))
135            listViewItem.Selected = true;
[3614]136        }
[4200]137        AdjustListViewColumnSizes();
[3709]138      } else {
139        runCollectionConstraintCollectionView.Content = null;
140        if (tabControl.TabPages.Contains(constraintPage))
141          tabControl.TabPages.Remove(constraintPage);
[6693]142        if (tabControl.TabPages.Contains(modifiersPage))
143          tabControl.TabPages.Remove(modifiersPage);
[3277]144      }
145    }
146
[3904]147    protected override void SetEnabledStateOfControls() {
148      base.SetEnabledStateOfControls();
[3350]149      if (Content == null) {
[3507]150        analyzeRunsToolStripDropDownButton.Enabled = false;
[3614]151        runCollectionConstraintCollectionView.ReadOnly = true;
[3350]152        itemsListView.Enabled = false;
153        detailsGroupBox.Enabled = false;
154        viewHost.Enabled = false;
155        removeButton.Enabled = false;
[3716]156        clearButton.Enabled = false;
[3350]157      } else {
[3507]158        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3614]159        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3350]160        itemsListView.Enabled = true;
[4099]161        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[3435]162        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3716]163        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3350]164        viewHost.Enabled = true;
165      }
166    }
167
[4883]168    private ListViewItem CreateListViewItem(IRun item) {
[3277]169      ListViewItem listViewItem = new ListViewItem();
[5237]170      if (item == null) {
171        listViewItem.Text = "null";
[5287]172        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
[5237]173        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
174      } else {
175        listViewItem.Text = item.ToString();
176        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
177        itemsListView.SmallImageList.Images.Add(item.ItemImage);
178        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
179        listViewItem.Tag = item;
[4200]180
[5237]181        if (item.Visible) {
182          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
183          listViewItem.ForeColor = item.Color;
184        } else {
185          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
186          listViewItem.ForeColor = Color.LightGray;
187        }
[4200]188      }
[3277]189      return listViewItem;
190    }
[4883]191    private void AddListViewItem(ListViewItem listViewItem) {
[5371]192      if (listViewItem == null) throw new ArgumentNullException();
[3277]193      itemsListView.Items.Add(listViewItem);
[4883]194      IRun run = listViewItem.Tag as IRun;
195      if (run != null) {
[5237]196        if (!itemListViewItemMapping.ContainsKey(run)) {
197          itemListViewItemMapping.Add(run, new List<ListViewItem>());
198          RegisterItemEvents(run);
199        }
200        itemListViewItemMapping[run].Add(listViewItem);
[4883]201      }
[3277]202    }
[4883]203    private void RemoveListViewItem(ListViewItem listViewItem) {
[5371]204      if (listViewItem == null) throw new ArgumentNullException();
[4883]205      IRun run = listViewItem.Tag as IRun;
206      if (run != null) {
[5237]207        itemListViewItemMapping[run].Remove(listViewItem);
208        if (itemListViewItemMapping[run].Count == 0) {
209          DeregisterItemEvents(run);
210          itemListViewItemMapping.Remove(run);
[4883]211        }
212      }
[3277]213      listViewItem.Remove();
214    }
[4883]215    private void UpdateListViewItemImage(ListViewItem listViewItem) {
[5371]216      if (listViewItem == null) throw new ArgumentNullException();
[5237]217      IRun item = listViewItem.Tag as IRun;
[3341]218      int i = listViewItem.ImageIndex;
[5839]219      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
[3341]220      listViewItem.ImageIndex = -1;
221      listViewItem.ImageIndex = i;
222    }
[4883]223    private void UpdateListViewItemText(ListViewItem listViewItem) {
[5371]224      if (listViewItem == null) throw new ArgumentNullException();
[5237]225      IRun item = listViewItem.Tag as IRun;
226      listViewItem.Text = item == null ? "null" : item.ToString();
227      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
[3277]228    }
[5237]229    private IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
230      if (item == null) {
231        List<ListViewItem> listViewItems = new List<ListViewItem>();
232        foreach (ListViewItem listViewItem in itemsListView.Items) {
233          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
234        }
235        return listViewItems;
236      } else {
[5302]237        List<ListViewItem> listViewItems = null;
238        itemListViewItemMapping.TryGetValue(item, out listViewItems);
239        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
[5237]240      }
[3277]241    }
242
243    #region ListView Events
[4883]244    private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[3456]245      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
[7065]246      // for performance reason (multiple selection fires this handler for every selected item)
247      if (itemsListView.SelectedIndices.Count <= 1)
248        AdjustListViewColumnSizes();
[4096]249      if (showDetailsCheckBox.Checked) {
250        if (itemsListView.SelectedItems.Count == 1) {
251          IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
252          detailsGroupBox.Enabled = true;
253          viewHost.Content = item;
254        } else {
255          viewHost.Content = null;
256          detailsGroupBox.Enabled = false;
257        }
[3277]258      }
259    }
[4883]260    private void itemsListView_KeyDown(object sender, KeyEventArgs e) {
[3277]261      if (e.KeyCode == Keys.Delete) {
[3435]262        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[9613]263          if (RunCollection != null) {
264            RunCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (IRun)i.Tag));
265          } else {
266            foreach (ListViewItem item in itemsListView.SelectedItems)
267              Content.Remove((IRun)item.Tag);
268          }
[3277]269        }
270      }
271    }
[4883]272    private void itemsListView_DoubleClick(object sender, EventArgs e) {
[3277]273      if (itemsListView.SelectedItems.Count == 1) {
[5237]274        IRun item = itemsListView.SelectedItems[0].Tag as IRun;
275        if (item != null) {
276          IContentView view = MainFormManager.MainForm.ShowContent(item);
277          if (view != null) {
278            view.ReadOnly = ReadOnly;
279            view.Locked = Locked;
280          }
[3416]281        }
[3277]282      }
283    }
[4883]284    private void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]285      if (!Locked) {
[5702]286        List<IRun> items = new List<IRun>();
287        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
288          IRun item = listViewItem.Tag as IRun;
289          if (item != null) items.Add(item);
290        }
291
292        if (items.Count > 0) {
[5237]293          DataObject data = new DataObject();
[5837]294          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
295          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
[5237]296          if (Content.IsReadOnly || ReadOnly) {
297            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
298          } else {
299            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
[5744]300            if (result.HasFlag(DragDropEffects.Move)) {
[5702]301              foreach (IRun item in items) Content.Remove(item);
302            }
[5237]303          }
[3432]304        }
[3277]305      }
306    }
[5744]307    private void itemsListView_DragEnter(object sender, DragEventArgs e) {
308      validDragOperation = false;
[5837]309      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun)) {
[5744]310        validDragOperation = true;
[5837]311      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
[5744]312        validDragOperation = true;
[5837]313        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]314        foreach (object item in items)
315          validDragOperation = validDragOperation && (item is IRun);
316      }
317    }
318    private void itemsListView_DragOver(object sender, DragEventArgs e) {
[3277]319      e.Effect = DragDropEffects.None;
[5744]320      if (validDragOperation) {
[3694]321        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3277]322        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]323        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
324        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
325        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[3277]326      }
327    }
[4883]328    private void itemsListView_DragDrop(object sender, DragEventArgs e) {
[3277]329      if (e.Effect != DragDropEffects.None) {
[5837]330        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun) {
331          IRun item = (IRun)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]332          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item);
[5837]333        } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
334          IEnumerable<IRun> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<IRun>();
[6527]335          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
336            Cloner cloner = new Cloner();
337            items = items.Select(x => cloner.Clone(x));
338          }
[7065]339          if (RunCollection != null) {
340            RunCollection.AddRange(items);
341          } else { // the content is an IItemCollection<IRun>
342            foreach (IRun item in items)
343              Content.Add(item);
344          }
[5702]345        }
[3277]346      }
347    }
348    #endregion
349
350    #region Button Events
[4883]351    private void menuItem_Click(object sender, EventArgs e) {
[3507]352      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
[3796]353      Type viewType = (Type)menuItem.Tag;
354      IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
[3507]355      if (view != null) {
356        view.Locked = Locked;
357        view.ReadOnly = ReadOnly;
358      }
359    }
[4883]360    private void removeButton_Click(object sender, EventArgs e) {
[3277]361      if (itemsListView.SelectedItems.Count > 0) {
[9613]362        if (RunCollection != null) {
363          RunCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (IRun)i.Tag));
364        } else {
365          foreach (ListViewItem item in itemsListView.SelectedItems)
366            Content.Remove((IRun)item.Tag);
367        }
[3277]368        itemsListView.SelectedItems.Clear();
369      }
370    }
[4883]371    private void clearButton_Click(object sender, EventArgs e) {
[3716]372      Content.Clear();
373    }
[3277]374    #endregion
375
[6693]376    #region Control Events
[4883]377    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
[4096]378      if (showDetailsCheckBox.Checked) {
379        splitContainer.Panel2Collapsed = false;
380        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
381        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (IRun)itemsListView.SelectedItems[0].Tag : null;
382      } else {
383        splitContainer.Panel2Collapsed = true;
384        viewHost.Content = null;
385      }
386    }
[6693]387    private void EvaluateModifications() {
388      if (RunCollection == null)
389        return;
390      ReadOnly = true;
[13181]391
[6693]392      try {
[13181]393        RunCollection.UpdateOfRunsInProgress = true;
[6693]394        RunCollection.Modify();
[11344]395      } finally {
[6693]396        ReadOnly = false;
[13181]397        RunCollection.UpdateOfRunsInProgress = false;
[6693]398      }
399    }
[4096]400    #endregion
401
[3277]402    #region Content Events
[4883]403    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12692]404      if (suppressUpdates) return;
[3277]405      if (InvokeRequired)
[3280]406        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
[3449]407      else {
[4200]408        foreach (IRun item in e.Items)
[3277]409          AddListViewItem(CreateListViewItem(item));
[4200]410
411        AdjustListViewColumnSizes();
[3507]412        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]413        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]414        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3449]415      }
[3277]416    }
[4883]417    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12692]418      if (suppressUpdates) return;
[3277]419      if (InvokeRequired)
[3280]420        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
[3277]421      else {
[3280]422        foreach (IRun item in e.Items) {
[4203]423          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]424          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
425          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]426        }
[5237]427        RebuildImageList();
[3507]428        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]429        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]430        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]431      }
432    }
[4883]433    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12692]434      if (suppressUpdates) return;
[3277]435      if (InvokeRequired)
[3280]436        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
[3277]437      else {
[3280]438        foreach (IRun item in e.OldItems) {
[5237]439          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]440          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
441          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]442        }
[5237]443        RebuildImageList();
[4200]444        foreach (IRun item in e.Items)
[3277]445          AddListViewItem(CreateListViewItem(item));
[4200]446
447        AdjustListViewColumnSizes();
[3507]448        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]449        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]450        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]451      }
452    }
[12692]453    private void RunCollection_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
454      if (InvokeRequired) Invoke((Action<object, EventArgs>)RunCollection_UpdateOfRunsInProgressChanged, sender, e);
455      else {
456        suppressUpdates = RunCollection.UpdateOfRunsInProgress;
457        if (!suppressUpdates) {
458          foreach (IRun item in Content) {
459            //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
460            ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
461            if (listViewItem != null) RemoveListViewItem(listViewItem);
462          }
463          RebuildImageList();
464          foreach (IRun item in Content)
465            AddListViewItem(CreateListViewItem(item));
466
467          AdjustListViewColumnSizes();
468          analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
469          clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
470          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
471        }
472      }
473    }
[3277]474    #endregion
475
476    #region Item Events
[4883]477    private void Item_ItemImageChanged(object sender, EventArgs e) {
[12692]478      if (suppressUpdates) return;
[3341]479      if (InvokeRequired)
480        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
481      else {
482        IRun item = (IRun)sender;
483        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
484          UpdateListViewItemImage(listViewItem);
485      }
486    }
[4883]487    private void Item_ToStringChanged(object sender, EventArgs e) {
[12692]488      if (suppressUpdates) return;
[3277]489      if (InvokeRequired)
490        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
491      else {
[3280]492        IRun item = (IRun)sender;
[3277]493        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3341]494          UpdateListViewItemText(listViewItem);
[3829]495        AdjustListViewColumnSizes();
[3277]496      }
497    }
[11344]498    private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) {
[12692]499      if (suppressUpdates) return;
[3632]500      if (InvokeRequired)
[12692]501        Invoke((Action<object, PropertyChangedEventArgs>)Item_PropertyChanged, sender, e);
[3632]502      else {
503        IRun run = (IRun)sender;
[11344]504        if (e.PropertyName == "Color" || e.PropertyName == "Visible")
505          UpdateRun(run);
[3632]506      }
[3614]507    }
508
[4883]509    private void UpdateRun(IRun run) {
[3507]510      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
511        if (run.Visible) {
512          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
513          listViewItem.ForeColor = run.Color;
514        } else {
515          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
516          listViewItem.ForeColor = Color.LightGray;
517        }
518      }
519    }
[3277]520    #endregion
[3716]521
[3829]522    #region Helpers
[4883]523    private void AdjustListViewColumnSizes() {
[3829]524      if (itemsListView.Items.Count > 0) {
525        for (int i = 0; i < itemsListView.Columns.Count; i++) {
526          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
527        }
528      }
529    }
[5237]530    private void RebuildImageList() {
531      itemsListView.SmallImageList.Images.Clear();
[5239]532      foreach (ListViewItem listViewItem in itemsListView.Items) {
533        IRun item = listViewItem.Tag as IRun;
[5287]534        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]535        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5237]536      }
537    }
[3829]538    #endregion
[3260]539  }
540}
Note: See TracBrowser for help on using the repository browser.