#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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;
using System.Windows.Forms;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Clients.Hive.Views {
[View("ItemTree View")]
[Content(typeof(IItemTree), IsDefaultView = false)]
public sealed partial class ItemTreeView : ItemView {
public new IItemTree Content {
get { return (IItemTree)base.Content; }
set { base.Content = value; }
}
public ItemTreeView() {
InitializeComponent();
}
protected override void DeregisterContentEvents() {
foreach (TreeNode node in treeView.Nodes) {
DeregisterContentEvents((IItemTree)node.Tag, node);
}
base.DeregisterContentEvents();
}
private void DeregisterContentEvents(IItemTree item, TreeNode node) {
item.ItemsAdded -= new CollectionItemsChangedEventHandler(item_ItemsAdded);
item.ItemsRemoved -= new CollectionItemsChangedEventHandler(item_ItemsRemoved);
item.CollectionReset -= new CollectionItemsChangedEventHandler(item_CollectionReset);
item.ToStringChanged -= new EventHandler(item_ToStringChanged);
item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
foreach (TreeNode childNode in node.Nodes) {
DeregisterContentEvents((IItemTree)childNode.Tag, childNode);
}
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
foreach (TreeNode node in treeView.Nodes) {
RegisterContentEvents((IItemTree)node.Tag, node);
}
}
private void RegisterContentEvents(IItemTree item, TreeNode node) {
item.ItemsAdded += new CollectionItemsChangedEventHandler(item_ItemsAdded);
item.ItemsRemoved += new CollectionItemsChangedEventHandler(item_ItemsRemoved);
item.CollectionReset += new CollectionItemsChangedEventHandler(item_CollectionReset);
item.ToStringChanged += new EventHandler(item_ToStringChanged);
item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
foreach (TreeNode childNode in node.Nodes) {
RegisterContentEvents((IItemTree)childNode.Tag, childNode);
}
}
#region Event Handlers (Content)
private void item_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) {
if (InvokeRequired) {
Invoke(new CollectionItemsChangedEventHandler(item_ItemsAdded), sender, e);
} else {
TreeNode node = GetNodeByItem(sender as IItemTree);
if (node != null) {
foreach (IItemTree item in e.Items) {
AddChildNodes(item, node.Nodes, true);
node.ExpandAll();
UpdateNodeItemImage(node);
}
}
}
}
private void item_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) {
if (InvokeRequired) {
Invoke(new CollectionItemsChangedEventHandler(item_ItemsRemoved), sender, e);
} else {
TreeNode node = GetNodeByItem(sender as IItemTree);
if (node != null) {
foreach (IItemTree item in e.Items) {
RemoveChildNodes(item, node.Nodes);
}
RebuildImageList();
}
}
}
private void item_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
if (InvokeRequired) {
Invoke(new CollectionItemsChangedEventHandler(item_CollectionReset), sender, e);
} else {
TreeNode node = GetNodeByItem(sender as IItemTree);
if (node != null) {
foreach (IItemTree item in e.OldItems) {
RemoveChildNodes(item, node.Nodes);
}
RebuildImageList();
foreach (IItemTree item in e.Items) {
AddChildNodes(item, node.Nodes, true);
}
}
}
}
private void item_ToStringChanged(object sender, EventArgs e) {
if (InvokeRequired) {
Invoke(new EventHandler(item_ToStringChanged), sender, e);
} else {
var item = sender as IItemTree;
TreeNode node = GetNodeByItem(item);
if(node != null)
node.Text = item.ToString();
}
}
private void item_ItemImageChanged(object sender, EventArgs e) {
if (InvokeRequired) {
Invoke(new EventHandler(item_ItemImageChanged), sender, e);
} else {
var item = sender as IItemTree;
TreeNode node = GetNodeByItem(item);
if(node != null)
UpdateNodeItemImage(node);
}
}
private void treeView_DoubleClick(object sender, EventArgs e) {
if (InvokeRequired) {
Invoke(new EventHandler(treeView_DoubleClick), sender, e);
} else {
if (treeView.SelectedNode != null) {
var item = treeView.SelectedNode.Tag as IItemTree;
if (item != null) {
IContentView view = MainFormManager.MainForm.ShowContent(item);
if (view != null) {
view.ReadOnly = ReadOnly;
view.Locked = Locked;
}
}
}
}
}
#endregion
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
// Add code when content has been changed and is null
ClearNodes();
detailsViewHost.Content = null;
} else {
// Add code when content has been changed and is not null
ClearNodes();
AddChildNodes(Content, treeView.Nodes, false);
foreach (TreeNode node in treeView.Nodes) {
UpdateNodeItemImage(node);
}
}
}
private void ClearNodes() {
treeView.Nodes.Clear();
}
private void AddChildNodes(IItemTree item, TreeNodeCollection nodes, bool registerEvents) {
TreeNode node = CreateTreeViewNode(item);
nodes.Add(node);
RebuildImageList(node);
RegisterContentEvents(item, node);
var childItems = item.GetChildItems();
foreach (var childItem in childItems) {
AddChildNodes(childItem, node.Nodes, registerEvents);
}
node.ExpandAll();
}
private void RemoveChildNodes(IItemTree item, TreeNodeCollection treeNodeCollection) {
int idx = -1;
for (int i = 0; i < treeNodeCollection.Count; i++) {
if (treeNodeCollection[i].Tag == item) {
idx = i; break;
}
}
DeregisterContentEvents(item, treeNodeCollection[idx]);
treeNodeCollection.RemoveAt(idx);
}
private TreeNode GetNodeByItem(IItemTree item) {
TreeNode found = null;
foreach (TreeNode node in treeView.Nodes) {
found = GetNodeByItem(node, item);
if (found != null)
return found;
}
return null;
}
private TreeNode GetNodeByItem(TreeNode node, IItemTree item) {
if (node.Tag == item)
return node;
TreeNode found = null;
foreach (TreeNode childNode in node.Nodes) {
found = GetNodeByItem(childNode, item);
if (found != null)
return found;
}
return null;
}
private TreeNode CreateTreeViewNode(IItemTree item) {
var node = new TreeNode(item.ToString());
node.Tag = item;
return node;
}
private void UpdateNodeItemImage(TreeNode node) {
if (node == null) throw new ArgumentNullException();
var item = node.Tag as IItemTree;
int i = node.ImageIndex;
this.imageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
node.ImageIndex = -1;
node.ImageIndex = i;
node.SelectedImageIndex = node.ImageIndex;
foreach (TreeNode childNode in node.Nodes) {
UpdateNodeItemImage(childNode);
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
// Enable or disable controls based on whether the content is null or the view is set readonly
}
#region Event Handlers (child controls)
// Put event handlers of child controls here.
private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
detailsViewHost.Content = (IContent)treeView.SelectedNode.Tag;
}
#endregion
private void RebuildImageList() {
treeView.ImageList.Images.Clear();
foreach (TreeNode node in treeView.Nodes) {
RebuildImageList(node);
}
}
private void RebuildImageList(TreeNode node) {
var item = node.Tag as IItemTree;
treeView.ImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
node.ImageIndex = treeView.ImageList.Images.Count - 1;
node.SelectedImageIndex = node.ImageIndex;
foreach (TreeNode childNode in node.Nodes) {
RebuildImageList(childNode);
}
}
}
}