#region License Information /* HeuristicLab * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Windows.Forms; using HeuristicLab.Collections; using HeuristicLab.Core; using HeuristicLab.MainForm; namespace HeuristicLab.ItemTreeViews.Views { [View("NamedItemCollection Tree View")] [Content(typeof(NamedItemCollection<>), false)] public partial class NamedItemCollectionTreeView : ItemCollectionTreeView where T : class, INamedItem { public new IKeyedItemCollection Content { get { return (IKeyedItemCollection)base.Content; } set { base.Content = value; } } public NamedItemCollectionTreeView() { InitializeComponent(); } protected override void DeregisterContentEvents() { Content.ItemsReplaced -= Content_ItemsReplaced; base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ItemsReplaced += Content_ItemsReplaced; } protected override void OnContentChanged() { string selectedName = null; if ((ItemsTreeView.SelectedNode != null ? ItemsTreeView.SelectedNode.Tag as T : null) is T) selectedName = ((T)ItemsTreeView.SelectedNode.Tag).Name; base.OnContentChanged(); if (selectedName == null) return; foreach (TreeNode item in ItemsTreeView.Nodes) if ((item.Tag != null) && ((T)item.Tag).Name.Equals(selectedName)) ItemsTreeView.SelectedNode = item; } protected override T CreateItem() { T item = base.CreateItem(); if (item != null) { item.Name = GetUniqueName(item.Name); } return item; } protected override TreeNode CreateTreeViewNode(T item) { var listViewItem = base.CreateTreeViewNode(item); if (!string.IsNullOrEmpty(item != null ? item.Description : null)) { listViewItem.ToolTipText = item.ItemName + ": " + item.Description; } return listViewItem; } protected override void UpdateTreeViewItemText(TreeNode treeViewNode) { base.UpdateTreeViewItemText(treeViewNode); T item = treeViewNode.Tag as T; if (!string.IsNullOrEmpty(item != null ? item.Description : null)) { treeViewNode.ToolTipText = item.ItemName + ": " + item.Description; } } #region Content Events protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler(Content_ItemsReplaced), sender, e); else { ItemsTreeView.BeginUpdate(); foreach (T item in e.Items) UpdateTreeViewItemText(GetTreeNodeForItem(item)); ItemsTreeView.EndUpdate(); } } #endregion #region Helpers protected virtual string GetUniqueName(string originalName) { if (!Content.ContainsKey(originalName)) return originalName; string name; int index = 0; do { index++; name = originalName + index; } while (Content.ContainsKey(name)); return name; } #endregion } }