Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

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