[187] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.Drawing;
|
---|
| 5 | using System.Data;
|
---|
| 6 | using System.Text;
|
---|
| 7 | using System.Windows.Forms;
|
---|
| 8 | using HeuristicLab.Core;
|
---|
| 9 |
|
---|
| 10 | namespace HeuristicLab.Data {
|
---|
[190] | 11 | public partial class ItemDictionaryView<K, V> : ViewBase
|
---|
[187] | 12 | where K : IItem
|
---|
| 13 | where V : IItem {
|
---|
| 14 |
|
---|
| 15 | private EditKeyValueDialog editKeyValueDialog;
|
---|
| 16 |
|
---|
[190] | 17 | public ItemDictionary<K, V> ItemDictionary {
|
---|
| 18 | get { return (ItemDictionary<K, V>) Item; }
|
---|
[187] | 19 | set { base.Item = value; }
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | private ListViewItem CreateListViewItem(K key, V value) {
|
---|
| 23 | ListViewItem item = new ListViewItem(key.ToString());
|
---|
| 24 | item.Name = key.ToString();
|
---|
| 25 | item.SubItems.Add(value.ToString());
|
---|
| 26 | item.SubItems[0].Tag = key;
|
---|
| 27 | item.SubItems[1].Tag = value;
|
---|
| 28 | return item;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[190] | 31 | public ItemDictionaryView() {
|
---|
[187] | 32 | InitializeComponent();
|
---|
| 33 | listView.View = View.Details;
|
---|
| 34 | listView.Columns[0].Text = "Key";
|
---|
| 35 | listView.Columns[1].Text = "Value";
|
---|
[194] | 36 | valueTypeTextBox.Text = typeof(V).ToString();
|
---|
| 37 | keyTypeTextBox.Text = typeof(K).ToString();
|
---|
[190] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public ItemDictionaryView(ItemDictionary<K, V> dictionary)
|
---|
| 41 | : this() {
|
---|
| 42 | ItemDictionary = dictionary;
|
---|
[187] | 43 | }
|
---|
| 44 |
|
---|
| 45 | protected override void RemoveItemEvents() {
|
---|
| 46 | ItemDictionary.ItemAdded -= new EventHandler<KeyValueEventArgs>(ItemDictionary_ItemInserted);
|
---|
| 47 | ItemDictionary.ItemRemoved -= new EventHandler<KeyValueEventArgs>(ItemDictionary_ItemRemoved);
|
---|
| 48 | ItemDictionary.Cleared -= new EventHandler(ItemDictionary_Cleared);
|
---|
| 49 | base.RemoveItemEvents();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | protected override void AddItemEvents() {
|
---|
| 53 | base.AddItemEvents();
|
---|
| 54 | ItemDictionary.ItemAdded += new EventHandler<KeyValueEventArgs>(ItemDictionary_ItemInserted);
|
---|
| 55 | ItemDictionary.ItemRemoved += new EventHandler<KeyValueEventArgs>(ItemDictionary_ItemRemoved);
|
---|
| 56 | ItemDictionary.Cleared += new EventHandler(ItemDictionary_Cleared);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | private void listView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 60 | if (detailsPanel.Controls.Count > 0)
|
---|
| 61 | detailsPanel.Controls[0].Dispose();
|
---|
| 62 | if (keyPanel.Controls.Count > 0)
|
---|
| 63 | keyPanel.Controls[0].Dispose();
|
---|
| 64 | detailsPanel.Controls.Clear();
|
---|
| 65 | keyPanel.Controls.Clear();
|
---|
| 66 | detailsPanel.Enabled = false;
|
---|
| 67 | keyPanel.Enabled = false;
|
---|
| 68 | removeButton.Enabled = false;
|
---|
| 69 | if (listView.SelectedItems.Count > 0) {
|
---|
| 70 | removeButton.Enabled = true;
|
---|
| 71 | }
|
---|
| 72 | if (listView.SelectedItems.Count == 1) {
|
---|
| 73 | K key = (K) listView.SelectedItems[0].SubItems[0].Tag;
|
---|
| 74 | V data = (V) listView.SelectedItems[0].SubItems[1].Tag;
|
---|
| 75 | Control keyView = (Control) key.CreateView();
|
---|
| 76 | Control dataView = (Control) data.CreateView();
|
---|
| 77 | keyPanel.Controls.Add(keyView);
|
---|
| 78 | detailsPanel.Controls.Add(dataView);
|
---|
| 79 | detailsPanel.Enabled = true;
|
---|
| 80 | }
|
---|
| 81 | keyPanel.Enabled = false;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | #region Item and ItemDictionary Events
|
---|
| 85 | private void ItemDictionary_ItemInserted(object sender, KeyValueEventArgs e) {
|
---|
| 86 | if (InvokeRequired)
|
---|
| 87 | Invoke(new EventHandler<KeyValueEventArgs>(ItemDictionary_ItemInserted), sender, e);
|
---|
| 88 | else {
|
---|
| 89 | ListViewItem item = CreateListViewItem((K) e.Key, (V) e.Value);
|
---|
| 90 | listView.Items.Insert(listView.Items.Count, item);
|
---|
| 91 | item.Name = e.Key.ToString();
|
---|
| 92 | e.Value.Changed += new EventHandler(Item_Changed);
|
---|
| 93 | e.Key.Changed += new EventHandler(Item_Changed);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | private void ItemDictionary_ItemRemoved(object sender, KeyValueEventArgs e) {
|
---|
| 98 | if (InvokeRequired)
|
---|
| 99 | Invoke(new EventHandler<KeyValueEventArgs>(ItemDictionary_ItemRemoved), sender, e);
|
---|
| 100 | else {
|
---|
| 101 | int index = listView.Items.IndexOfKey(e.Key.ToString());
|
---|
| 102 | listView.Items.RemoveAt(index);
|
---|
| 103 | e.Key.Changed -= new EventHandler(Item_Changed);
|
---|
| 104 | e.Value.Changed += new EventHandler(Item_Changed);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private void ItemDictionary_Cleared(object sender, EventArgs e) {
|
---|
| 109 | Refresh();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | private void Item_Changed(object sender, EventArgs e) {
|
---|
| 113 | if (InvokeRequired)
|
---|
| 114 | Invoke(new EventHandler(Item_Changed), sender, e);
|
---|
| 115 | else {
|
---|
| 116 | IItem data = (IItem) sender;
|
---|
| 117 | foreach (ListViewItem item in listView.Items) {
|
---|
| 118 | if (item.SubItems[0].Tag == data) {
|
---|
| 119 | item.SubItems[0].Text = data.ToString();
|
---|
| 120 | item.Name = data.ToString();
|
---|
| 121 | } else if (item.SubItems[1].Tag == data) {
|
---|
| 122 | item.SubItems[1].Text = data.ToString();
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | #endregion
|
---|
| 128 |
|
---|
| 129 | #region Update Controls
|
---|
| 130 | protected override void UpdateControls() {
|
---|
| 131 | base.UpdateControls();
|
---|
| 132 | detailsPanel.Controls.Clear();
|
---|
| 133 | keyPanel.Controls.Clear();
|
---|
| 134 | detailsPanel.Enabled = false;
|
---|
| 135 | keyPanel.Enabled = false;
|
---|
| 136 | removeButton.Enabled = false;
|
---|
[190] | 137 | if (ItemDictionary != null) {
|
---|
| 138 | foreach (ListViewItem item in listView.Items) {
|
---|
| 139 | ((IItem) item.SubItems[0]).Changed -= new EventHandler(Item_Changed);
|
---|
| 140 | ((IItem) item.SubItems[1]).Changed -= new EventHandler(Item_Changed);
|
---|
| 141 | }
|
---|
| 142 | listView.Items.Clear();
|
---|
| 143 | foreach (KeyValuePair<K, V> data in ItemDictionary) {
|
---|
| 144 | ListViewItem item = CreateListViewItem(data.Key, data.Value);
|
---|
| 145 | listView.Items.Add(item);
|
---|
| 146 | data.Key.Changed += new EventHandler(Item_Changed);
|
---|
| 147 | data.Value.Changed += new EventHandler(Item_Changed);
|
---|
| 148 | }
|
---|
| 149 | addButton.Enabled = true;
|
---|
| 150 | } else {
|
---|
| 151 | addButton.Enabled = false;
|
---|
[187] | 152 | }
|
---|
| 153 | }
|
---|
| 154 | #endregion
|
---|
| 155 |
|
---|
| 156 | #region Button Events
|
---|
| 157 | private void addButton_Click(object sender, EventArgs e) {
|
---|
| 158 | editKeyValueDialog = new EditKeyValueDialog(typeof(K), typeof(V));
|
---|
| 159 | if (editKeyValueDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 160 | try {
|
---|
| 161 | if (!ItemDictionary.ContainsKey((K) editKeyValueDialog.Key)) {
|
---|
| 162 | ItemDictionary.Add((K) editKeyValueDialog.Key, (V) editKeyValueDialog.Value);
|
---|
| 163 | }
|
---|
| 164 | } catch (Exception ex) {
|
---|
| 165 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
| 171 | while (listView.SelectedIndices.Count > 0)
|
---|
| 172 | ItemDictionary.Remove((K) listView.SelectedItems[0].SubItems[0].Tag);
|
---|
| 173 | }
|
---|
| 174 | #endregion
|
---|
| 175 | }
|
---|
| 176 | }
|
---|