[2655] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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.Windows.Forms;
|
---|
| 23 | using HeuristicLab.Collections;
|
---|
| 24 | using HeuristicLab.MainForm;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Core.Views {
|
---|
[2917] | 27 | [View("NamedItemCollection View")]
|
---|
[2727] | 28 | [Content(typeof(NamedItemCollection<>), true)]
|
---|
[2655] | 29 | public partial class NamedItemCollectionView<T> : ItemCollectionView<T> where T : class, INamedItem {
|
---|
[3393] | 30 | public new IKeyedItemCollection<string, T> Content {
|
---|
| 31 | get { return (IKeyedItemCollection<string, T>)base.Content; }
|
---|
[2713] | 32 | set { base.Content = value; }
|
---|
[2655] | 33 | }
|
---|
| 34 |
|
---|
| 35 | public NamedItemCollectionView() {
|
---|
| 36 | InitializeComponent();
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[2713] | 39 | protected override void DeregisterContentEvents() {
|
---|
| 40 | Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced);
|
---|
| 41 | base.DeregisterContentEvents();
|
---|
[2655] | 42 | }
|
---|
[2713] | 43 | protected override void RegisterContentEvents() {
|
---|
| 44 | base.RegisterContentEvents();
|
---|
| 45 | Content.ItemsReplaced += new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced);
|
---|
[2655] | 46 | }
|
---|
| 47 |
|
---|
[3775] | 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 |
|
---|
[2655] | 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 | }
|
---|
[2947] | 68 | protected override ListViewItem CreateListViewItem(T item) {
|
---|
| 69 | ListViewItem listViewItem = base.CreateListViewItem(item);
|
---|
[5237] | 70 | if ((item != null) && !string.IsNullOrEmpty(item.Description)) {
|
---|
| 71 | listViewItem.ToolTipText = item.ItemName + ": " + item.Description;
|
---|
| 72 | }
|
---|
[2947] | 73 | return listViewItem;
|
---|
| 74 | }
|
---|
[3362] | 75 |
|
---|
[2655] | 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;
|
---|
[2713] | 81 | if ((item == null) || (Content.ContainsKey(item.Name)))
|
---|
[2655] | 82 | e.Effect = DragDropEffects.None;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | #endregion
|
---|
| 86 |
|
---|
[2713] | 87 | #region Content Events
|
---|
| 88 | protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 89 | if (InvokeRequired)
|
---|
[2713] | 90 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced), sender, e);
|
---|
[2655] | 91 | else {
|
---|
| 92 | foreach (T item in e.Items) {
|
---|
| 93 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
[3327] | 94 | UpdateListViewItemText(listViewItem);
|
---|
[2655] | 95 | }
|
---|
[3362] | 96 | AdjustListViewColumnSizes();
|
---|
[2655] | 97 | }
|
---|
| 98 | }
|
---|
| 99 | #endregion
|
---|
| 100 |
|
---|
| 101 | #region Helpers
|
---|
[2676] | 102 | protected virtual string GetUniqueName(string originalName) {
|
---|
[2713] | 103 | if (!Content.ContainsKey(originalName))
|
---|
[2655] | 104 | return originalName;
|
---|
| 105 | else {
|
---|
| 106 | string name = null;
|
---|
| 107 | int index = 0;
|
---|
| 108 | do {
|
---|
| 109 | index++;
|
---|
| 110 | name = originalName + index.ToString();
|
---|
[2713] | 111 | } while (Content.ContainsKey(name));
|
---|
[2655] | 112 | return name;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | #endregion
|
---|
| 116 | }
|
---|
| 117 | }
|
---|