using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.Common; using HeuristicLab.MainForm; namespace HeuristicLab.Data { /// /// The visual representation of the class . /// /// The type of the keys of the dictionary. /// The type of the values of the dictionary. [Content(typeof(ItemDictionary), true)] public partial class ItemDictionaryView : ItemViewBase where K : IItem where V : IItem { private EditKeyValueDialog editKeyValueDialog; /// /// Gets or sets the dictionary to represent visually. /// /// Uses property of base class . /// No own data storage present. public ItemDictionary ItemDictionary { get { return (ItemDictionary) Item; } set { base.Item = value; } } /// /// Creates a new list view item with the given and . /// /// The key to show in the list item. /// The value to show in the list item. /// The created list item as . private ListViewItem CreateListViewItem(K key, V value) { ListViewItem item = new ListViewItem(key.ToString()); item.Name = key.ToString(); item.SubItems.Add(value.ToString()); item.SubItems[0].Tag = key; item.SubItems[1].Tag = value; return item; } /// /// Initializes a new instance of the class . /// public ItemDictionaryView() { InitializeComponent(); listView.View = View.Details; listView.Columns[0].Text = "Key"; listView.Columns[1].Text = "Value"; valueTypeTextBox.Text = typeof(V).ToString(); keyTypeTextBox.Text = typeof(K).ToString(); } /// /// Initializes a new instance of the class with the given /// . /// No CopyConstructor! is not copied! /// /// The dictionary to represent visually. public ItemDictionaryView(ItemDictionary dictionary) : this() { ItemDictionary = dictionary; } /// /// Removes the eventhandlers from the underlying /// . /// /// Calls of base class . /// protected override void RemoveItemEvents() { ItemDictionary.ItemAdded -= new EventHandler>(ItemDictionary_ItemInserted); ItemDictionary.ItemRemoved -= new EventHandler>(ItemDictionary_ItemRemoved); ItemDictionary.Cleared -= new EventHandler(ItemDictionary_Cleared); base.RemoveItemEvents(); } /// /// Adds eventhandlers to the underlying . /// /// Calls of base class . /// protected override void AddItemEvents() { base.AddItemEvents(); ItemDictionary.ItemAdded += new EventHandler>(ItemDictionary_ItemInserted); ItemDictionary.ItemRemoved += new EventHandler>(ItemDictionary_ItemRemoved); ItemDictionary.Cleared += new EventHandler(ItemDictionary_Cleared); } private void listView_SelectedIndexChanged(object sender, EventArgs e) { if (detailsPanel.Controls.Count > 0) detailsPanel.Controls[0].Dispose(); if (keyPanel.Controls.Count > 0) keyPanel.Controls[0].Dispose(); detailsPanel.Controls.Clear(); keyPanel.Controls.Clear(); detailsPanel.Enabled = false; keyPanel.Enabled = false; removeButton.Enabled = false; if (listView.SelectedItems.Count > 0) { removeButton.Enabled = true; } if (listView.SelectedItems.Count == 1) { K key = (K) listView.SelectedItems[0].SubItems[0].Tag; V data = (V) listView.SelectedItems[0].SubItems[1].Tag; Control keyView = (Control) MainFormManager.CreateDefaultView(key); Control dataView = (Control) MainFormManager.CreateDefaultView(data); keyPanel.Controls.Add(keyView); detailsPanel.Controls.Add(dataView); detailsPanel.Enabled = true; } keyPanel.Enabled = false; } #region Item and ItemDictionary Events private void ItemDictionary_ItemInserted(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler>(ItemDictionary_ItemInserted), sender, e); else { ListViewItem item = CreateListViewItem((K) e.Value, (V) e.Value2); listView.Items.Insert(listView.Items.Count, item); item.Name = e.Value.ToString(); e.Value2.Changed += new EventHandler(Item_Changed); e.Value.Changed += new EventHandler(Item_Changed); } } private void ItemDictionary_ItemRemoved(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler>(ItemDictionary_ItemRemoved), sender, e); else { int index = listView.Items.IndexOfKey(e.Value.ToString()); listView.Items.RemoveAt(index); e.Value.Changed -= new EventHandler(Item_Changed); e.Value2.Changed += new EventHandler(Item_Changed); } } private void ItemDictionary_Cleared(object sender, EventArgs e) { Refresh(); } private void Item_Changed(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Item_Changed), sender, e); else { IItem data = (IItem) sender; foreach (ListViewItem item in listView.Items) { if (item.SubItems[0].Tag == data) { item.SubItems[0].Text = data.ToString(); item.Name = data.ToString(); } else if (item.SubItems[1].Tag == data) { item.SubItems[1].Text = data.ToString(); } } } } #endregion #region Update Controls /// /// Updates the controls with the latest elements of the dictionary. /// protected override void UpdateControls() { base.UpdateControls(); detailsPanel.Controls.Clear(); keyPanel.Controls.Clear(); detailsPanel.Enabled = false; keyPanel.Enabled = false; removeButton.Enabled = false; if (ItemDictionary != null) { foreach (ListViewItem item in listView.Items) { ((IItem) item.SubItems[0]).Changed -= new EventHandler(Item_Changed); ((IItem) item.SubItems[1]).Changed -= new EventHandler(Item_Changed); } listView.Items.Clear(); foreach (KeyValuePair data in ItemDictionary) { ListViewItem item = CreateListViewItem(data.Key, data.Value); listView.Items.Add(item); data.Key.Changed += new EventHandler(Item_Changed); data.Value.Changed += new EventHandler(Item_Changed); } addButton.Enabled = true; } else { addButton.Enabled = false; } } #endregion #region Button Events private void addButton_Click(object sender, EventArgs e) { editKeyValueDialog = new EditKeyValueDialog(typeof(K), typeof(V)); if (editKeyValueDialog.ShowDialog(this) == DialogResult.OK) { try { if (!ItemDictionary.ContainsKey((K) editKeyValueDialog.Key)) { ItemDictionary.Add((K) editKeyValueDialog.Key, (V) editKeyValueDialog.Value); } } catch (Exception ex) { HeuristicLab.Core.Views.Auxiliary.ShowErrorMessageBox(ex); } } } private void removeButton_Click(object sender, EventArgs e) { while (listView.SelectedIndices.Count > 0) ItemDictionary.Remove((K) listView.SelectedItems[0].SubItems[0].Tag); } #endregion } }