Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Core.Views/3.3/ItemListView.cs @ 10103

Last change on this file since 10103 was 10103, checked in by jkarder, 11 years ago

#2116:

  • fixed issues within the outermost control detection
  • fixed visible property assignment of the breadcrumb control in ViewHost
File size: 22.9 KB
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2655]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;
[5744]23using System.Collections;
[2655]24using System.Collections.Generic;
25using System.Drawing;
[5302]26using System.Linq;
[2655]27using System.Windows.Forms;
28using HeuristicLab.Collections;
[6472]29using HeuristicLab.Common;
[2655]30using HeuristicLab.MainForm;
[2713]31using HeuristicLab.MainForm.WindowsForms;
[3758]32using HeuristicLab.PluginInfrastructure;
[2655]33
34namespace HeuristicLab.Core.Views {
[2917]35  [View("ItemList View")]
[2727]36  [Content(typeof(ItemList<>), true)]
[3393]37  [Content(typeof(IItemList<>), false)]
[3483]38  public partial class ItemListView<T> : ItemView where T : class, IItem {
[5237]39    protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
[3407]40    protected TypeSelectorDialog typeSelectorDialog;
[5744]41    protected bool validDragOperation;
[3407]42
[3393]43    public new IItemList<T> Content {
44      get { return (IItemList<T>)base.Content; }
[2713]45      set { base.Content = value; }
[2655]46    }
47
48    public ListView ItemsListView {
49      get { return itemsListView; }
50    }
51
52    public ItemListView() {
53      InitializeComponent();
[5237]54      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
[2655]55    }
56
[5237]57    protected override void Dispose(bool disposing) {
58      if (disposing) {
59        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
60        if (components != null) components.Dispose();
61      }
62      base.Dispose(disposing);
63    }
64
[2713]65    protected override void DeregisterContentEvents() {
66      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
67      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
68      Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
69      Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
70      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
[5237]71      foreach (T item in itemListViewItemMapping.Keys) {
72        DeregisterItemEvents(item);
[5070]73      }
[2713]74      base.DeregisterContentEvents();
[2655]75    }
[2713]76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
79      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
80      Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
81      Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
82      Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
[2655]83    }
[5237]84    protected virtual void DeregisterItemEvents(T item) {
85      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
86      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
87    }
88    protected virtual void RegisterItemEvents(T item) {
89      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
90      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
91    }
[2655]92
[2713]93    protected override void OnContentChanged() {
94      base.OnContentChanged();
[3775]95
96      int selectedIndex = -1;
97      if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
98
[5237]99      itemsListView.Items.Clear();
100      itemListViewItemMapping.Clear();
101      RebuildImageList();
[2713]102      viewHost.Content = null;
103      if (Content != null) {
104        foreach (T item in Content)
[2655]105          AddListViewItem(CreateListViewItem(item));
[4240]106        AdjustListViewColumnSizes();
[3775]107        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
108          itemsListView.Items[selectedIndex].Selected = true;
[2655]109      }
[3362]110      SetEnabledStateOfControls();
[2655]111    }
112
[3904]113    protected override void SetEnabledStateOfControls() {
114      base.SetEnabledStateOfControls();
[3362]115      if (Content == null) {
116        addButton.Enabled = false;
117        moveUpButton.Enabled = false;
118        moveDownButton.Enabled = false;
119        removeButton.Enabled = false;
120        itemsListView.Enabled = false;
121        detailsGroupBox.Enabled = false;
122      } else {
[3435]123        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
[3362]124        moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
125                               itemsListView.SelectedIndices[0] != 0 &&
[3435]126                               !Content.IsReadOnly && !ReadOnly;
[3362]127        moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
128                                 itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
[3435]129                                 !Content.IsReadOnly && !ReadOnly;
[3362]130        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
[3435]131                               !Content.IsReadOnly && !ReadOnly;
[3362]132        itemsListView.Enabled = true;
[4099]133        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[3362]134      }
135    }
136
[2655]137    protected virtual T CreateItem() {
[3407]138      if (typeSelectorDialog == null) {
139        typeSelectorDialog = new TypeSelectorDialog();
140        typeSelectorDialog.Caption = "Select Item";
141        typeSelectorDialog.TypeSelector.Caption = "Available Items";
[3588]142        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
[2655]143      }
[3407]144
145      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
146        try {
147          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[10042]148        } catch (Exception ex) {
[3758]149          ErrorHandling.ShowErrorDialog(this, ex);
[3407]150        }
[2655]151      }
[3407]152      return null;
[2655]153    }
154    protected virtual ListViewItem CreateListViewItem(T item) {
155      ListViewItem listViewItem = new ListViewItem();
[5237]156      if (item == null) {
157        listViewItem.Text = "null";
[5287]158        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
[5237]159        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
160      } else {
161        listViewItem.Text = item.ToString();
162        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
163        itemsListView.SmallImageList.Images.Add(item.ItemImage);
164        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
165        listViewItem.Tag = item;
166      }
[2655]167      return listViewItem;
168    }
169    protected virtual void AddListViewItem(ListViewItem listViewItem) {
[5371]170      if (listViewItem == null) throw new ArgumentNullException();
[5237]171      T item = (listViewItem.Tag as T);
[2655]172      itemsListView.Items.Add(listViewItem);
[5237]173      if (item != null) {
174        if (!itemListViewItemMapping.ContainsKey(item)) {
175          RegisterItemEvents(item);
176          itemListViewItemMapping.Add(item, new List<ListViewItem>());
177        }
178        itemListViewItemMapping[item].Add(listViewItem);
179      }
[2655]180    }
181    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
[5371]182      if (listViewItem == null) throw new ArgumentNullException();
[5237]183      T item = (listViewItem.Tag as T);
[2655]184      itemsListView.Items.Insert(index, listViewItem);
[5237]185      if (item != null) {
186        if (!itemListViewItemMapping.ContainsKey(item)) {
187          RegisterItemEvents(item);
188          itemListViewItemMapping.Add(item, new List<ListViewItem>());
189        }
190        itemListViewItemMapping[item].Add(listViewItem);
191      }
[2655]192    }
193    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
[5371]194      if (listViewItem == null) throw new ArgumentNullException();
[5237]195      T item = (listViewItem.Tag as T);
196      if (item != null) {
197        itemListViewItemMapping[item].Remove(listViewItem);
198        if (itemListViewItemMapping[item].Count == 0) {
199          itemListViewItemMapping.Remove(item);
200          DeregisterItemEvents(item);
201        }
202      }
[2655]203      listViewItem.Remove();
204    }
[3327]205    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
[5371]206      if (listViewItem == null) throw new ArgumentNullException();
[5237]207      T item = listViewItem.Tag as T;
[3327]208      int i = listViewItem.ImageIndex;
[5839]209      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
[3327]210      listViewItem.ImageIndex = -1;
211      listViewItem.ImageIndex = i;
[2655]212    }
[3327]213    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
[5371]214      if (listViewItem == null) throw new ArgumentNullException();
[5237]215      T item = listViewItem.Tag as T;
216      listViewItem.Text = item == null ? "null" : item.ToString();
217      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
[3327]218    }
[5237]219    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
[5238]220      if (item == null) {
221        List<ListViewItem> listViewItems = new List<ListViewItem>();
222        foreach (ListViewItem listViewItem in itemsListView.Items) {
223          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
224        }
225        return listViewItems;
226      } else {
[5302]227        List<ListViewItem> listViewItems = null;
228        itemListViewItemMapping.TryGetValue(item, out listViewItems);
229        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
[5238]230      }
[5237]231    }
[2655]232
[3362]233    #region ListView Events
[2655]234    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
235      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
236                             itemsListView.SelectedIndices[0] != 0 &&
[3456]237                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
[2655]238      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
239                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
[3456]240                               (Content != null) && !Content.IsReadOnly && !ReadOnly;
[2655]241      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
[3456]242                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
[3829]243      AdjustListViewColumnSizes();
[10089]244      if (itemsListView.SelectedItems.Count == 1) {
245        var item = (T)itemsListView.SelectedItems[0].Tag;
246        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
[10093]247        var outermostViewHost = mainForm.ShowContentInOutermostViewHost(item, this, true);
[10103]248        if (outermostViewHost != null && outermostViewHost.HotlinkingEnabled) {
[10089]249          outermostViewHost.ActiveView.ReadOnly = ReadOnly;
250          outermostViewHost.ActiveView.Locked = Locked;
[4069]251        } else {
[10089]252          if (showDetailsCheckBox.Checked) {
253            detailsGroupBox.Enabled = true;
254            viewHost.Content = item;
255          } else {
256            viewHost.Content = null;
257            detailsGroupBox.Enabled = false;
258          }
[4069]259        }
[2655]260      }
261    }
262
263    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
264      if (e.KeyCode == Keys.Delete) {
[3435]265        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[2655]266          foreach (ListViewItem item in itemsListView.SelectedItems)
[2713]267            Content.RemoveAt(item.Index);
[2655]268        }
269      }
270    }
271
272    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
273      if (itemsListView.SelectedItems.Count == 1) {
[10089]274        var item = (T)itemsListView.SelectedItems[0].Tag;
275        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
[10093]276        var outermostViewHost = mainForm.ShowContentInOutermostViewHost(item, this);
[10103]277        if (outermostViewHost != null) {
278          outermostViewHost.ActiveView.ReadOnly = ReadOnly;
279          outermostViewHost.ActiveView.Locked = Locked;
280        }
[2655]281      }
282    }
283
284    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]285      if (!Locked) {
[5744]286        List<T> items = new List<T>();
287        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
288          T item = listViewItem.Tag as T;
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 & DragDropEffects.Move) == DragDropEffects.Move) {
301              foreach (ListViewItem listViewItem in itemsListView.SelectedItems.Cast<ListViewItem>().ToArray()) {
302                if (listViewItem.Tag != null) Content.RemoveAt(listViewItem.Index);
303              }
304            }
[5237]305          }
[3432]306        }
[2655]307      }
308    }
[5744]309    protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
310      validDragOperation = false;
[5837]311      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
[5744]312        validDragOperation = true;
[5837]313      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
[5744]314        validDragOperation = true;
[5837]315        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]316        foreach (object item in items)
317          validDragOperation = validDragOperation && (item is T);
318      }
319    }
320    protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
[2655]321      e.Effect = DragDropEffects.None;
[5744]322      if (validDragOperation) {
[3694]323        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2694]324        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]325        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
326        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
327        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[2655]328      }
329    }
330    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
331      if (e.Effect != DragDropEffects.None) {
332        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
333        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
[5744]334
[5837]335        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
336          T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]337          if (listViewItem != null) Content.Insert(listViewItem.Index, e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
338          else Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
[5837]339        } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
340          IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
[6472]341          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
342            Cloner cloner = new Cloner();
[6527]343            items = items.Select(x => cloner.Clone(x));
[6472]344          }
[5744]345          foreach (T item in items) {
[6472]346            if (listViewItem != null) Content.Insert(listViewItem.Index, item);
347            else Content.Add(item);
[5744]348          }
349        }
[2655]350      }
351    }
352    #endregion
353
354    #region Button Events
355    protected virtual void addButton_Click(object sender, EventArgs e) {
356      T item = CreateItem();
357      if (item != null)
[2713]358        Content.Add(item);
[2655]359    }
360    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
361      if (itemsListView.SelectedItems.Count == 1) {
362        int index = itemsListView.SelectedIndices[0];
[5928]363        Content.Reverse(index - 1, 2);
[2655]364        itemsListView.Items[index].Selected = false;
365        itemsListView.Items[index - 1].Selected = true;
366      }
367    }
368    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
369      if (itemsListView.SelectedItems.Count == 1) {
370        int index = itemsListView.SelectedIndices[0];
[5928]371        Content.Reverse(index, 2);
[2655]372        itemsListView.Items[index].Selected = false;
373        itemsListView.Items[index + 1].Selected = true;
374      }
375    }
376    protected virtual void removeButton_Click(object sender, EventArgs e) {
377      if (itemsListView.SelectedItems.Count > 0) {
378        foreach (ListViewItem item in itemsListView.SelectedItems)
[2713]379          Content.RemoveAt(item.Index);
[2655]380      }
381    }
382    #endregion
383
[4069]384    #region CheckBox Events
[4096]385    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
[4069]386      if (showDetailsCheckBox.Checked) {
387        splitContainer.Panel2Collapsed = false;
388        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
389        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
390      } else {
391        splitContainer.Panel2Collapsed = true;
[4096]392        viewHost.Content = null;
[4069]393      }
394    }
395    #endregion
396
[2713]397    #region Content Events
398    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]399      if (InvokeRequired)
[2713]400        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
[4240]401      else {
[2655]402        foreach (IndexedItem<T> item in e.Items)
403          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
[4240]404        AdjustListViewColumnSizes();
405      }
[2655]406    }
[2713]407    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]408      if (InvokeRequired)
[2713]409        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
[2655]410      else {
411        List<ListViewItem> listViewItems = new List<ListViewItem>();
412        foreach (IndexedItem<T> item in e.Items)
413          listViewItems.Add(itemsListView.Items[item.Index]);
414        foreach (ListViewItem listViewItem in listViewItems)
415          RemoveListViewItem(listViewItem);
[5237]416        RebuildImageList();
[2655]417      }
418    }
[2713]419    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]420      if (InvokeRequired)
[2713]421        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
[2655]422      else {
423        int[] selected = new int[itemsListView.SelectedIndices.Count];
424        itemsListView.SelectedIndices.CopyTo(selected, 0);
425
426        List<ListViewItem> listViewItems = new List<ListViewItem>();
427        foreach (IndexedItem<T> item in e.OldItems)
428          listViewItems.Add(itemsListView.Items[item.Index]);
429        foreach (ListViewItem listViewItem in listViewItems)
430          RemoveListViewItem(listViewItem);
[5237]431        RebuildImageList();
[2655]432
433        foreach (IndexedItem<T> item in e.Items)
434          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
[4240]435        AdjustListViewColumnSizes();
[2655]436
437        for (int i = 0; i < selected.Length; i++)
438          itemsListView.Items[selected[i]].Selected = true;
439      }
440    }
[2713]441    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]442      if (InvokeRequired)
[2713]443        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
[2655]444      else {
445        foreach (IndexedItem<T> item in e.Items) {
446          ListViewItem listViewItem = itemsListView.Items[item.Index];
[7161]447          if (listViewItem.Tag != null)
448            itemListViewItemMapping[(T)listViewItem.Tag].Remove(listViewItem);
[2655]449          listViewItem.Tag = item.Value;
[7161]450          if (listViewItem.Tag != null)
451            itemListViewItemMapping[item.Value].Add(listViewItem);
[3327]452          UpdateListViewItemImage(listViewItem);
453          UpdateListViewItemText(listViewItem);
[2655]454        }
455      }
456    }
[2713]457    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2655]458      if (InvokeRequired)
[2713]459        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
[2655]460      else {
461        List<ListViewItem> listViewItems = new List<ListViewItem>();
462        foreach (IndexedItem<T> item in e.OldItems)
463          listViewItems.Add(itemsListView.Items[item.Index]);
464        foreach (ListViewItem listViewItem in listViewItems)
465          RemoveListViewItem(listViewItem);
[5237]466        RebuildImageList();
[2655]467
468        foreach (IndexedItem<T> item in e.Items)
469          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
[4240]470        AdjustListViewColumnSizes();
[2655]471      }
472    }
473    #endregion
474
475    #region Item Events
[3306]476    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
477      if (InvokeRequired)
478        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
479      else {
480        T item = (T)sender;
[5237]481        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
482          UpdateListViewItemImage(listViewItem);
[3306]483      }
484    }
[2932]485    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
[2655]486      if (InvokeRequired)
[2932]487        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
[2655]488      else {
489        T item = (T)sender;
[5237]490        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
491          UpdateListViewItemText(listViewItem);
[9755]492        AdjustListViewColumnSizes();
[2655]493      }
494    }
495    #endregion
[3362]496
497    #region Helpers
498    protected virtual void AdjustListViewColumnSizes() {
499      if (itemsListView.Items.Count > 0) {
500        for (int i = 0; i < itemsListView.Columns.Count; i++)
501          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
502      }
503    }
[5239]504    protected virtual void RebuildImageList() {
[5237]505      itemsListView.SmallImageList.Images.Clear();
[5239]506      foreach (ListViewItem listViewItem in itemsListView.Items) {
507        T item = listViewItem.Tag as T;
[5287]508        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]509        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5237]510      }
511    }
[3362]512    #endregion
[2655]513  }
514}
Note: See TracBrowser for help on using the repository browser.