Changeset 5057


Ignore:
Timestamp:
12/07/10 11:49:25 (11 years ago)
Author:
abeham
Message:

#1324

  • Removed update of ImageIndex from RemoveListViewItem()
  • Added method RebuildImageList for synchronizing the SmallImageList after a removal
  • Added a dictionary to provide faster access to items in the collection
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core.Views/3.3/ItemCollectionView.cs

    r4300 r5057  
    3434  [Content(typeof(IItemCollection<>), false)]
    3535  public partial class ItemCollectionView<T> : ItemView where T : class, IItem {
     36    private Dictionary<T, List<ListViewItem>> listViewItemMapping;
    3637    protected TypeSelectorDialog typeSelectorDialog;
    3738
     
    4748    public ItemCollectionView() {
    4849      InitializeComponent();
     50      listViewItemMapping = new Dictionary<T, List<ListViewItem>>();
    4951    }
    5052
     
    6567      base.OnContentChanged();
    6668      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
     69      RebuildImageList();
    6770      viewHost.Content = null;
    6871      if (Content != null) {
     
    105108        try {
    106109          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
    107         }
    108         catch (Exception ex) {
     110        } catch (Exception ex) {
    109111          ErrorHandling.ShowErrorDialog(this, ex);
    110112        }
     
    122124    }
    123125    protected virtual void AddListViewItem(ListViewItem listViewItem) {
     126      T item = (listViewItem.Tag as T);
    124127      itemsListView.Items.Add(listViewItem);
    125       ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
    126       ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
     128      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
     129      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
     130      if (!listViewItemMapping.ContainsKey(item))
     131        listViewItemMapping.Add(item, new List<ListViewItem>());
     132      listViewItemMapping[item].Add(listViewItem);
    127133      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
    128134      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
    129135    }
    130136    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
    131       ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
    132       ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
     137      T item = (listViewItem.Tag as T);
     138      listViewItemMapping[item].Remove(listViewItem);
     139      if (listViewItemMapping[item].Count == 0) {
     140        listViewItemMapping.Remove(item);
     141        item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
     142        item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
     143      }
    133144      listViewItem.Remove();
    134       foreach (ListViewItem other in itemsListView.Items)
    135         if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
    136       itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
    137145      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
    138146      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
     
    149157    }
    150158    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
    151       foreach (ListViewItem listViewItem in itemsListView.Items) {
    152         if (((T)listViewItem.Tag) == item)
    153           yield return listViewItem;
    154       }
     159      return listViewItemMapping[item];
    155160    }
    156161
     
    278283          if (listviewItem != null) RemoveListViewItem(listviewItem);
    279284        }
     285        RebuildImageList();
    280286      }
    281287    }
     
    289295          if (listviewItem != null) RemoveListViewItem(listviewItem);
    290296        }
     297        RebuildImageList();
    291298        foreach (T item in e.Items)
    292299          AddListViewItem(CreateListViewItem(item));
     
    328335      }
    329336    }
     337    private void RebuildImageList() {
     338      itemsListView.SmallImageList.Images.Clear();
     339      foreach (ListViewItem item in itemsListView.Items) {
     340        itemsListView.SmallImageList.Images.Add(((T)item.Tag).ItemImage);
     341        item.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
     342      }
     343    }
    330344    #endregion
    331345  }
Note: See TracChangeset for help on using the changeset viewer.