#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;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Collections;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
namespace HeuristicLab.ItemTreeViews.Views {
[View("ParameterCollection Tree View")]
[Content(typeof(ParameterCollection), false)]
[Content(typeof(IKeyedItemCollection), false)]
public partial class ParameterCollectionTreeView : NamedItemCollectionTreeView {
protected CreateParameterDialog createParameterDialog;
protected bool allowEditingOfHiddenParameters;
public virtual bool AllowEditingOfHiddenParameters
{
get { return allowEditingOfHiddenParameters; }
set
{
if (value != allowEditingOfHiddenParameters) {
allowEditingOfHiddenParameters = value;
SetEnabledStateOfControls();
}
}
}
public ParameterCollectionTreeView() {
InitializeComponent();
itemsGroupBox.Text = "Parameters";
allowEditingOfHiddenParameters = true;
}
protected override void Dispose(bool disposing) {
if (disposing) {
if (createParameterDialog != null)
createParameterDialog.Dispose();
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}
protected override void DeregisterItemEvents(IParameter item) {
item.HiddenChanged -= Item_HiddenChanged;
base.DeregisterItemEvents(item);
}
protected override void RegisterItemEvents(IParameter item) {
base.RegisterItemEvents(item);
item.HiddenChanged += Item_HiddenChanged;
}
protected override void OnContentChanged() {
base.OnContentChanged();
if ((Content == null) || !Content.Any(x => x.Hidden))
showHiddenParametersCheckBox.Checked = false;
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
showHiddenParametersCheckBox.Enabled = (Content != null) && Content.Any(x => x.Hidden);
viewHost.ReadOnly = ReadOnly;
}
protected override IParameter CreateItem() {
if (createParameterDialog == null) createParameterDialog = new CreateParameterDialog();
if (createParameterDialog.ShowDialog(this) == DialogResult.OK) {
IParameter param = createParameterDialog.Parameter;
if ((param != null) && Content.ContainsKey(param.Name))
param = (IParameter)Activator.CreateInstance(param.GetType(), GetUniqueName(param.Name), param.Description);
return param;
}
return null;
}
protected override TreeNode CreateTreeViewNode(IParameter item) {
var viewItem = base.CreateTreeViewNode(item);
if ((item != null) && item.Hidden) {
viewItem.NodeFont = new Font(viewItem.NodeFont, FontStyle.Italic);
viewItem.ForeColor = Color.LightGray;
}
return viewItem;
}
protected override void AddTreeNode(IParameter p) {
base.AddTreeNode(p);
if (p != null) UpdateParameterVisibility(p);
}
protected virtual void UpdateParameterVisibility(IParameter parameter) {
var treeNode = GetTreeNodeForItem(parameter);
if (parameter.Hidden) {
treeNode.NodeFont = new Font(treeNode.NodeFont, FontStyle.Italic);
treeNode.ForeColor = Color.LightGray;
} else {
treeNode.NodeFont = new Font(treeNode.NodeFont, FontStyle.Regular);
treeNode.ForeColor = ItemsTreeView.ForeColor;
}
if (!showHiddenParametersCheckBox.Checked && parameter.Hidden) //remove hidden nodes
treeNode.Remove();
else if (treeNode.Parent == null && !ItemsTreeView.Nodes.Contains(treeNode)) {// nonadded Nodes
var p = GetParent(parameter);
(p == null ? ItemsTreeView.Nodes : GetTreeNodeForItem(p).Nodes).Add(treeNode);
}
}
protected override List ExpandTreeViewItem(object parent) {
var p = parent as IParameterizedItem;
if (p == null || p.Parameters == null) return base.ExpandTreeViewItem(parent);
return p.Parameters.ToList();
}
#region Control Events
protected virtual void showHiddenParametersCheckBox_CheckedChanged(object sender, EventArgs e) {
if (Content == null) return;
var items = itemTreeViewItemMapping.Keys.Where(x => x.Hidden).OrderBy(x => x.ToString()).ToArray();
ItemsTreeView.BeginUpdate();
Array.ForEach(items, UpdateParameterVisibility);
ItemsTreeView.EndUpdate();
}
protected override void itemsTreeView_SelectedIndexChanged(object sender, EventArgs e) {
base.itemsTreeView_SelectedIndexChanged(sender, e);
SetEnabledStateOfControls();
}
protected override void itemsTreeView_DoubleClick(object sender, EventArgs e) {
var item = ItemsTreeView.SelectedNode != null ? ItemsTreeView.SelectedNode.Tag as IParameter : null;
if (item == null) return;
var view = MainFormManager.MainForm.ShowContent(item);
if (view == null) return;
view.ReadOnly = ReadOnly || (item.Hidden && !AllowEditingOfHiddenParameters);
view.Locked = Locked;
}
protected virtual void itemsTreeViewContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
showHideParametersToolStripMenuItem.Text = "Show/Hide Parameters";
showHideParametersToolStripMenuItem.Enabled = false;
if (ItemsTreeView.SelectedNode == null) {
e.Cancel = true;
} else if (!ReadOnly && !Locked && AllowEditingOfHiddenParameters) {
List parameters = new List();
IParameter parameter = ItemsTreeView.SelectedNode.Tag as IParameter;
if (parameter != null) parameters.Add(parameter);
showHideParametersToolStripMenuItem.Enabled = parameters.Count > 0;
if (parameters.Count == 1) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameter" : "Hide Parameter";
else if ((parameters.Count > 1) && parameters.All(x => x.Hidden == parameters[0].Hidden)) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameters" : "Hide Parameters";
showHideParametersToolStripMenuItem.Tag = parameters;
}
}
protected virtual void showHideParametersToolStripMenuItem_Click(object sender, EventArgs e) {
foreach (IParameter parameter in (IEnumerable)showHideParametersToolStripMenuItem.Tag)
parameter.Hidden = !parameter.Hidden;
}
protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
base.showDetailsCheckBox_CheckedChanged(sender, e);
SetEnabledStateOfControls();
}
#endregion
#region Content Events
protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) {
if (InvokeRequired)
Invoke(new CollectionItemsChangedEventHandler(Content_ItemsAdded), sender, e);
else {
base.Content_ItemsAdded(sender, e);
showHiddenParametersCheckBox.Enabled = Content.Any(x => x.Hidden);
}
}
protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) {
if (InvokeRequired)
Invoke(new CollectionItemsChangedEventHandler(Content_ItemsRemoved), sender, e);
else {
base.Content_ItemsRemoved(sender, e);
}
}
protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler(Content_CollectionReset), sender, e);
else base.Content_CollectionReset(sender, e);
}
#endregion
#region Item Events
protected virtual void Item_HiddenChanged(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Item_HiddenChanged), sender, e);
else {
ItemsTreeView.BeginUpdate();
UpdateParameterVisibility((IParameter)sender);
ItemsTreeView.EndUpdate();
}
}
#endregion
#region Helpers
protected override void RebuildTreeImageList() {
base.RebuildTreeImageList();
if (!showHiddenParametersCheckBox.Checked) {
// update image of hidden list view items
foreach (IParameter parameter in itemTreeViewItemMapping.Keys.Where(x => x.Hidden)) {
var treeNode = GetTreeNodeForItem(parameter);
try {
ItemsTreeView.ImageList.Images.Add(parameter == null ? Common.Resources.VSImageLibrary.Nothing : parameter.ItemImage);
treeNode.ImageIndex = ItemsTreeView.ImageList.Images.Count - 1;
treeNode.SelectedImageIndex = treeNode.ImageIndex;
}
catch (ArgumentException) {
treeNode.SelectedImageIndex = ItemsTreeView.ImageList.Images.Count + 10000;
}
}
}
}
#endregion
}
}