#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
using HeuristicLab.Persistence.Core;
using HeuristicLab.Persistence.Default.Xml;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.HLScript.Views {
[View("ItemCollection View")]
[Content(typeof(VariableStore), true)]
public partial class VariableStoreView : AsynchronousContentView {
protected Dictionary itemListViewItemMapping;
protected TypeSelectorDialog typeSelectorDialog;
protected bool validDragOperation;
public new VariableStore Content {
get { return (VariableStore)base.Content; }
set { base.Content = value; }
}
public ListView ItemsListView {
get { return variableListView; }
}
public VariableStoreView() {
InitializeComponent();
itemListViewItemMapping = new Dictionary();
variableListView.SmallImageList.Images.AddRange(new Image[] {
HeuristicLab.Common.Resources.VSImageLibrary.Error,
HeuristicLab.Common.Resources.VSImageLibrary.Object,
HeuristicLab.Common.Resources.VSImageLibrary.Nothing
});
}
protected override void Dispose(bool disposing) {
if (disposing) {
if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
if (components != null) components.Dispose();
}
base.Dispose(disposing);
}
protected override void DeregisterContentEvents() {
Content.ItemsAdded -= Content_ItemsAdded;
Content.ItemsReplaced -= Content_ItemsReplaced;
Content.ItemsRemoved -= Content_ItemsRemoved;
Content.CollectionReset -= Content_CollectionReset;
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ItemsAdded += Content_ItemsAdded;
Content.ItemsReplaced += Content_ItemsReplaced;
Content.ItemsRemoved += Content_ItemsRemoved;
Content.CollectionReset += Content_CollectionReset;
}
protected override void OnContentChanged() {
base.OnContentChanged();
variableListView.Items.Clear();
itemListViewItemMapping.Clear();
RebuildImageList();
if (Content != null) {
Caption += " (" + Content.GetType().Name + ")";
foreach (var item in Content)
AddVariable(item);
AdjustListViewColumnSizes();
SortItemsListView(SortOrder.Ascending);
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
if (Content == null) {
addButton.Enabled = false;
sortAscendingButton.Enabled = false;
sortDescendingButton.Enabled = false;
removeButton.Enabled = false;
variableListView.Enabled = false;
} else {
addButton.Enabled = !Locked && !ReadOnly;
sortAscendingButton.Enabled = variableListView.Items.Count > 1;
sortDescendingButton.Enabled = variableListView.Items.Count > 1;
removeButton.Enabled = !Locked && !ReadOnly && variableListView.SelectedItems.Count > 0;
variableListView.Enabled = true;
}
}
protected virtual object CreateItem() {
if (typeSelectorDialog == null) {
typeSelectorDialog = new TypeSelectorDialog { Caption = "Select Item" };
typeSelectorDialog.TypeSelector.Caption = "Available Items";
typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, true);
}
if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
try {
return (object)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
} catch (Exception ex) {
ErrorHandling.ShowErrorDialog(this, ex);
}
}
return null;
}
protected virtual void AddVariable(KeyValuePair variable) {
if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
string value = (variable.Value ?? "null").ToString();
string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
bool serializable = IsSerializable(variable);
var listViewItem = new ListViewItem(new[] { variable.Key, value, type }) { ToolTipText = GetToolTipText(variable, serializable), Tag = variable };
if (serializable) {
listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
} else listViewItem.ImageIndex = 0;
variableListView.Items.Add(listViewItem);
itemListViewItemMapping[variable.Key] = listViewItem;
sortAscendingButton.Enabled = variableListView.Items.Count > 1;
sortDescendingButton.Enabled = variableListView.Items.Count > 1;
var item = variable.Value as IItem;
if (item != null) item.ToStringChanged += item_ToStringChanged;
}
protected virtual void RemoveVariable(KeyValuePair variable) {
if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
ListViewItem listViewItem;
if (itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) {
itemListViewItemMapping.Remove(variable.Key);
variableListView.Items.Remove(listViewItem);
sortAscendingButton.Enabled = variableListView.Items.Count > 1;
sortDescendingButton.Enabled = variableListView.Items.Count > 1;
var item = variable.Value as IItem;
if (item != null) item.ToStringChanged -= item_ToStringChanged;
}
}
protected virtual void UpdateVariable(KeyValuePair variable) {
if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
ListViewItem listViewItem;
if (itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) {
string value = (variable.Value ?? "null").ToString();
string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
bool serializable = IsSerializable(variable);
if (serializable) {
listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
} else listViewItem.ImageIndex = 0;
listViewItem.SubItems[1].Text = value;
listViewItem.SubItems[2].Text = type;
listViewItem.ToolTipText = GetToolTipText(variable, serializable);
listViewItem.Tag = variable;
} else throw new ArgumentException("A variable with the specified name does not exist.", "variable");
}
#region ListView Events
protected virtual void variableListView_SelectedIndexChanged(object sender, EventArgs e) {
removeButton.Enabled = (Content != null) && !Locked && !ReadOnly && variableListView.SelectedItems.Count > 0;
AdjustListViewColumnSizes();
}
protected virtual void variableListView_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Delete) {
if ((variableListView.SelectedItems.Count > 0) && !Locked && !ReadOnly) {
foreach (ListViewItem item in variableListView.SelectedItems)
Content.Remove(item.Text);
}
}
}
protected virtual void variableListView_DoubleClick(object sender, EventArgs e) {
if (variableListView.SelectedItems.Count == 1) {
var item = variableListView.SelectedItems[0].Tag as KeyValuePair?;
if (item != null) {
var value = item.Value.Value as IContent;
if (value != null) {
IContentView view = MainFormManager.MainForm.ShowContent(value);
if (view != null) {
view.ReadOnly = ReadOnly;
view.Locked = Locked;
}
}
}
}
}
protected virtual void variableListView_ItemDrag(object sender, ItemDragEventArgs e) {
if (!Locked) {
var items = new List