using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HeuristicLab.MainForm;
using HeuristicLab.Core.Views;
using HeuristicLab.Core;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Problems.MetaOptimization.Views {
///
/// The visual representation of a .
///
[View("ParameterConfigurationList View")]
[Content(typeof(ParameterConfigurationList), true)]
public partial class ParameterConfigurationListView : ItemCollectionView {
///
/// Gets or sets the variable to represent visually.
///
/// Uses property of base class .
/// No own data storage present.
public new ParameterConfigurationList Content {
get { return (ParameterConfigurationList)base.Content; }
set { base.Content = value; }
}
public ParameterConfigurationListView() {
InitializeComponent();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.CheckedItemsChanged += new Collections.CollectionItemsChangedEventHandler(Content_CheckedItemsChanged);
}
protected override void DeregisterContentEvents() {
Content.CheckedItemsChanged -= new Collections.CollectionItemsChangedEventHandler(Content_CheckedItemsChanged);
base.DeregisterContentEvents();
}
protected override void OnContentChanged() {
base.OnContentChanged();
while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
viewHost.Content = null;
if (Content != null) {
Caption += " (" + Content.GetType().Name + ")";
foreach (IParameterConfiguration item in Content.CheckedItems)
AddListViewItem(CreateListViewItem(item));
AdjustListViewColumnSizes();
SortItemsListView(SortOrder.Ascending);
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
}
protected override void addButton_Click(object sender, EventArgs e) {
var selectorDialog = new ObjectSelectorDialog(from c in Content
where !Content.ItemChecked(c)
group c by c.Category);
if (selectorDialog.ShowDialog() == DialogResult.OK) {
Content.SetItemCheckedState(selectorDialog.Item, true);
}
}
protected override void removeButton_Click(object sender, EventArgs e) {
if (itemsListView.SelectedItems.Count > 0) {
foreach (ListViewItem item in itemsListView.SelectedItems)
Content.SetItemCheckedState((IParameterConfiguration)item.Tag, false);
itemsListView.SelectedItems.Clear();
}
}
private void Content_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs e) {
foreach (IParameterConfiguration item in e.Items) {
if (!Content.ItemChecked(item)) {
ListViewItem found = FindListViewItem(item);
if(found != null)
RemoveListViewItem(found);
} else {
ListViewItem found = FindListViewItem(item);
if (found == null)
AddListViewItem(CreateListViewItem(item));
}
}
}
#region Helpers
private ListViewItem FindListViewItem(IParameterConfiguration tag) {
foreach (ListViewItem item in itemsListView.Items) {
if (item.Tag == tag)
return item;
}
return null;
}
#endregion
}
}