Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2817 was 2790, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

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