[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 {
|
---|
[2917] | 28 | [View("NamedItemCollection View")]
|
---|
[2727] | 29 | [Content(typeof(NamedItemCollection<>), true)]
|
---|
[2655] | 30 | public partial class NamedItemCollectionView<T> : ItemCollectionView<T> where T : class, INamedItem {
|
---|
[3393] | 31 | public new IKeyedItemCollection<string, T> Content {
|
---|
| 32 | get { return (IKeyedItemCollection<string, T>)base.Content; }
|
---|
[2713] | 33 | set { base.Content = value; }
|
---|
[2655] | 34 | }
|
---|
| 35 |
|
---|
| 36 | private Dictionary<T, ListViewItem> listViewItemDictionary;
|
---|
| 37 | protected Dictionary<T, ListViewItem> ListViewItemDictionary {
|
---|
| 38 | get { return listViewItemDictionary; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public NamedItemCollectionView() {
|
---|
| 42 | listViewItemDictionary = new Dictionary<T, ListViewItem>();
|
---|
| 43 | InitializeComponent();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[2713] | 46 | protected override void DeregisterContentEvents() {
|
---|
| 47 | Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced);
|
---|
| 48 | base.DeregisterContentEvents();
|
---|
[2655] | 49 | }
|
---|
[2713] | 50 | protected override void RegisterContentEvents() {
|
---|
| 51 | base.RegisterContentEvents();
|
---|
| 52 | Content.ItemsReplaced += new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced);
|
---|
[2655] | 53 | }
|
---|
| 54 |
|
---|
[3775] | 55 | protected override void OnContentChanged() {
|
---|
| 56 | string selectedName = null;
|
---|
| 57 | if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null))
|
---|
| 58 | selectedName = ((T)itemsListView.SelectedItems[0].Tag).Name;
|
---|
| 59 | base.OnContentChanged();
|
---|
| 60 | if (selectedName != null) {
|
---|
| 61 | foreach (ListViewItem item in itemsListView.Items) {
|
---|
| 62 | if ((item.Tag != null) && (((T)item.Tag).Name.Equals(selectedName)))
|
---|
| 63 | item.Selected = true;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[2655] | 68 | protected override T CreateItem() {
|
---|
| 69 | T item = base.CreateItem();
|
---|
| 70 | if (item != null) {
|
---|
| 71 | item.Name = GetUniqueName(item.Name);
|
---|
| 72 | }
|
---|
| 73 | return item;
|
---|
| 74 | }
|
---|
[2947] | 75 | protected override ListViewItem CreateListViewItem(T item) {
|
---|
| 76 | ListViewItem listViewItem = base.CreateListViewItem(item);
|
---|
[4715] | 77 | listViewItem.ToolTipText = string.IsNullOrEmpty(item.Description) ? item.ItemName : item.ItemName + ": " + item.Description;
|
---|
[2947] | 78 | return listViewItem;
|
---|
| 79 | }
|
---|
[2655] | 80 | protected override void AddListViewItem(ListViewItem listViewItem) {
|
---|
| 81 | ListViewItemDictionary.Add((T)listViewItem.Tag, listViewItem);
|
---|
| 82 | base.AddListViewItem(listViewItem);
|
---|
| 83 | }
|
---|
| 84 | protected override void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
| 85 | base.RemoveListViewItem(listViewItem);
|
---|
| 86 | ListViewItemDictionary.Remove((T)listViewItem.Tag);
|
---|
| 87 | }
|
---|
| 88 | protected override IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
|
---|
[3561] | 89 | ListViewItem listViewItem = null;
|
---|
| 90 | listViewItemDictionary.TryGetValue(item, out listViewItem);
|
---|
| 91 | if (listViewItem != null) return new ListViewItem[] { listViewItem };
|
---|
| 92 | else return new ListViewItem[0];
|
---|
[2655] | 93 | }
|
---|
[3362] | 94 |
|
---|
[2655] | 95 | #region ListView Events
|
---|
| 96 | protected override void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
| 97 | base.itemsListView_DragEnterOver(sender, e);
|
---|
| 98 | if (e.Effect != DragDropEffects.None) {
|
---|
| 99 | T item = e.Data.GetData("Value") as T;
|
---|
[2713] | 100 | if ((item == null) || (Content.ContainsKey(item.Name)))
|
---|
[2655] | 101 | e.Effect = DragDropEffects.None;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | #endregion
|
---|
| 105 |
|
---|
[2713] | 106 | #region Content Events
|
---|
| 107 | protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 108 | if (InvokeRequired)
|
---|
[2713] | 109 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced), sender, e);
|
---|
[2655] | 110 | else {
|
---|
| 111 | foreach (T item in e.Items) {
|
---|
| 112 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
[3327] | 113 | UpdateListViewItemText(listViewItem);
|
---|
[2655] | 114 | }
|
---|
[3362] | 115 | AdjustListViewColumnSizes();
|
---|
[2655] | 116 | }
|
---|
| 117 | }
|
---|
| 118 | #endregion
|
---|
| 119 |
|
---|
| 120 | #region Helpers
|
---|
[2676] | 121 | protected virtual string GetUniqueName(string originalName) {
|
---|
[2713] | 122 | if (!Content.ContainsKey(originalName))
|
---|
[2655] | 123 | return originalName;
|
---|
| 124 | else {
|
---|
| 125 | string name = null;
|
---|
| 126 | int index = 0;
|
---|
| 127 | do {
|
---|
| 128 | index++;
|
---|
| 129 | name = originalName + index.ToString();
|
---|
[2713] | 130 | } while (Content.ContainsKey(name));
|
---|
[2655] | 131 | return name;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | #endregion
|
---|
| 135 | }
|
---|
| 136 | }
|
---|