[2655] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Windows.Forms;
|
---|
| 24 | using HeuristicLab.Collections;
|
---|
| 25 | using HeuristicLab.MainForm;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Core.Views {
|
---|
[2727] | 28 | [Content(typeof(NamedItemCollection<>), true)]
|
---|
[2655] | 29 | public partial class NamedItemCollectionView<T> : ItemCollectionView<T> where T : class, INamedItem {
|
---|
[2713] | 30 | public new IObservableKeyedCollection<string, T> Content {
|
---|
| 31 | get { return (IObservableKeyedCollection<string, T>)base.Content; }
|
---|
| 32 | set { base.Content = value; }
|
---|
[2655] | 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 | }
|
---|
[2727] | 45 | public NamedItemCollectionView(IObservableKeyedCollection<string, T> content)
|
---|
| 46 | : this() {
|
---|
| 47 | Content = content;
|
---|
| 48 | }
|
---|
[2655] | 49 |
|
---|
[2713] | 50 | protected override void DeregisterContentEvents() {
|
---|
| 51 | Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced);
|
---|
| 52 | base.DeregisterContentEvents();
|
---|
[2655] | 53 | }
|
---|
[2713] | 54 | protected override void RegisterContentEvents() {
|
---|
| 55 | base.RegisterContentEvents();
|
---|
| 56 | Content.ItemsReplaced += new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced);
|
---|
[2655] | 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;
|
---|
[2713] | 83 | if ((item == null) || (Content.ContainsKey(item.Name)))
|
---|
[2655] | 84 | e.Effect = DragDropEffects.None;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | #endregion
|
---|
| 88 |
|
---|
[2713] | 89 | #region Content Events
|
---|
| 90 | protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 91 | if (InvokeRequired)
|
---|
[2713] | 92 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced), sender, e);
|
---|
[2655] | 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
|
---|
[2676] | 103 | protected virtual string GetUniqueName(string originalName) {
|
---|
[2713] | 104 | if (!Content.ContainsKey(originalName))
|
---|
[2655] | 105 | return originalName;
|
---|
| 106 | else {
|
---|
| 107 | string name = null;
|
---|
| 108 | int index = 0;
|
---|
| 109 | do {
|
---|
| 110 | index++;
|
---|
| 111 | name = originalName + index.ToString();
|
---|
[2713] | 112 | } while (Content.ContainsKey(name));
|
---|
[2655] | 113 | return name;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | #endregion
|
---|
| 117 | }
|
---|
| 118 | }
|
---|