Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionView.cs @ 16749

Last change on this file since 16749 was 15717, checked in by abeham, 7 years ago

#2885: merged to stable

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