Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionView.cs @ 12694

Last change on this file since 12694 was 12694, checked in by abeham, 9 years ago

#2208: merged trunk changes

File size: 23.4 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[12694]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;
[12694]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;
[12694]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);
[12694]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);
[12694]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);
[12694]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);
[12694]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;
391      try {
392        RunCollection.Modify();
[12694]393      } finally {
[6693]394        ReadOnly = false;
395      }
396    }
[4096]397    #endregion
398
[3277]399    #region Content Events
[4883]400    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12694]401      if (suppressUpdates) return;
[3277]402      if (InvokeRequired)
[3280]403        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
[3449]404      else {
[4200]405        foreach (IRun item in e.Items)
[3277]406          AddListViewItem(CreateListViewItem(item));
[4200]407
408        AdjustListViewColumnSizes();
[3507]409        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]410        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]411        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3449]412      }
[3277]413    }
[4883]414    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12694]415      if (suppressUpdates) return;
[3277]416      if (InvokeRequired)
[3280]417        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
[3277]418      else {
[3280]419        foreach (IRun item in e.Items) {
[4203]420          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]421          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
422          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]423        }
[5237]424        RebuildImageList();
[3507]425        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]426        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]427        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]428      }
429    }
[4883]430    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12694]431      if (suppressUpdates) return;
[3277]432      if (InvokeRequired)
[3280]433        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
[3277]434      else {
[3280]435        foreach (IRun item in e.OldItems) {
[5237]436          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]437          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
438          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]439        }
[5237]440        RebuildImageList();
[4200]441        foreach (IRun item in e.Items)
[3277]442          AddListViewItem(CreateListViewItem(item));
[4200]443
444        AdjustListViewColumnSizes();
[3507]445        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]446        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]447        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]448      }
449    }
[12694]450    private void RunCollection_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
451      if (InvokeRequired) Invoke((Action<object, EventArgs>)RunCollection_UpdateOfRunsInProgressChanged, sender, e);
452      else {
453        suppressUpdates = RunCollection.UpdateOfRunsInProgress;
454        if (!suppressUpdates) {
455          foreach (IRun item in Content) {
456            //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
457            ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
458            if (listViewItem != null) RemoveListViewItem(listViewItem);
459          }
460          RebuildImageList();
461          foreach (IRun item in Content)
462            AddListViewItem(CreateListViewItem(item));
463
464          AdjustListViewColumnSizes();
465          analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
466          clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
467          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
468        }
469      }
470    }
[3277]471    #endregion
472
473    #region Item Events
[4883]474    private void Item_ItemImageChanged(object sender, EventArgs e) {
[12694]475      if (suppressUpdates) return;
[3341]476      if (InvokeRequired)
477        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
478      else {
479        IRun item = (IRun)sender;
480        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
481          UpdateListViewItemImage(listViewItem);
482      }
483    }
[4883]484    private void Item_ToStringChanged(object sender, EventArgs e) {
[12694]485      if (suppressUpdates) return;
[3277]486      if (InvokeRequired)
487        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
488      else {
[3280]489        IRun item = (IRun)sender;
[3277]490        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3341]491          UpdateListViewItemText(listViewItem);
[3829]492        AdjustListViewColumnSizes();
[3277]493      }
494    }
[12694]495    private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) {
496      if (suppressUpdates) return;
[3632]497      if (InvokeRequired)
[12694]498        Invoke((Action<object, PropertyChangedEventArgs>)Item_PropertyChanged, sender, e);
[3632]499      else {
500        IRun run = (IRun)sender;
[12694]501        if (e.PropertyName == "Color" || e.PropertyName == "Visible")
502          UpdateRun(run);
[3632]503      }
[3614]504    }
505
[4883]506    private void UpdateRun(IRun run) {
[3507]507      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
508        if (run.Visible) {
509          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
510          listViewItem.ForeColor = run.Color;
511        } else {
512          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
513          listViewItem.ForeColor = Color.LightGray;
514        }
515      }
516    }
[3277]517    #endregion
[3716]518
[3829]519    #region Helpers
[4883]520    private void AdjustListViewColumnSizes() {
[3829]521      if (itemsListView.Items.Count > 0) {
522        for (int i = 0; i < itemsListView.Columns.Count; i++) {
523          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
524        }
525      }
526    }
[5237]527    private void RebuildImageList() {
528      itemsListView.SmallImageList.Images.Clear();
[5239]529      foreach (ListViewItem listViewItem in itemsListView.Items) {
530        IRun item = listViewItem.Tag as IRun;
[5287]531        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]532        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5237]533      }
534    }
[3829]535    #endregion
[3260]536  }
537}
Note: See TracBrowser for help on using the repository browser.