Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core.Views/3.3/ItemListView.cs @ 17148

Last change on this file since 17148 was 17148, checked in by abeham, 5 years ago

#1616: merged to stable (17127)

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