Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/NamedItemCollectionView.cs @ 2818

Last change on this file since 2818 was 2818, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • corrected several bugs in order to get SGA working
File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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.Collections.Generic;
23using System.Windows.Forms;
24using HeuristicLab.Collections;
25using HeuristicLab.MainForm;
26
27namespace HeuristicLab.Core.Views {
28  [Content(typeof(NamedItemCollection<>), true)]
29  public partial class NamedItemCollectionView<T> : ItemCollectionView<T> where T : class, INamedItem {
30    public new IObservableKeyedCollection<string, T> Content {
31      get { return (IObservableKeyedCollection<string, T>)base.Content; }
32      set { base.Content = value; }
33    }
34
35    private Dictionary<T, ListViewItem> listViewItemDictionary;
36    protected Dictionary<T, ListViewItem> ListViewItemDictionary {
37      get { return listViewItemDictionary; }
38    }
39
40    public NamedItemCollectionView() {
41      listViewItemDictionary = new Dictionary<T, ListViewItem>();
42      InitializeComponent();
43      Caption = "Named Item Collection";
44    }
45    public NamedItemCollectionView(IObservableKeyedCollection<string, T> content)
46      : this() {
47      Content = content;
48    }
49
50    protected override void DeregisterContentEvents() {
51      Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced);
52      base.DeregisterContentEvents();
53    }
54    protected override void RegisterContentEvents() {
55      base.RegisterContentEvents();
56      Content.ItemsReplaced += new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced);
57    }
58
59    protected override T CreateItem() {
60      T item = base.CreateItem();
61      if (item != null) {
62        item.Name = GetUniqueName(item.Name);
63      }
64      return item;
65    }
66    protected override void AddListViewItem(ListViewItem listViewItem) {
67      ListViewItemDictionary.Add((T)listViewItem.Tag, listViewItem);
68      base.AddListViewItem(listViewItem);
69    }
70    protected override void RemoveListViewItem(ListViewItem listViewItem) {
71      base.RemoveListViewItem(listViewItem);
72      ListViewItemDictionary.Remove((T)listViewItem.Tag);
73    }
74    protected override IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
75      return new ListViewItem[] { listViewItemDictionary[item] };
76    }
77 
78    #region ListView Events
79    protected override void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
80      base.itemsListView_DragEnterOver(sender, e);
81      if (e.Effect != DragDropEffects.None) {
82        T item = e.Data.GetData("Value") as T;
83        if ((item == null) || (Content.ContainsKey(item.Name)))
84          e.Effect = DragDropEffects.None;
85      }
86    }
87    #endregion
88
89    #region Content Events
90    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<T> e) {
91      if (InvokeRequired)
92        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced), sender, e);
93      else {
94        foreach (T item in e.Items) {
95          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
96            UpdateListViewItem(listViewItem);
97        }
98      }
99    }
100    #endregion
101
102    #region Helpers
103    protected virtual string GetUniqueName(string originalName) {
104      if (!Content.ContainsKey(originalName))
105        return originalName;
106      else {
107        string name = null;
108        int index = 0;
109        do {
110          index++;
111          name = originalName + index.ToString();
112        } while (Content.ContainsKey(name));
113        return name;
114      }
115    }
116    #endregion
117  }
118}
Note: See TracBrowser for help on using the repository browser.