Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.2/ItemDictionaryView_T.cs @ 3543

Last change on this file since 3543 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 8.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8using HeuristicLab.Common;
9using HeuristicLab.Core;
10
11namespace HeuristicLab.Data {
12  /// <summary>
13  /// The visual representation of the class <see cref="HeuristicLab.Data.ItemDictionary&lt;K,V&gt;"/>.
14  /// </summary>
15  /// <typeparam name="K">The type of the keys of the dictionary.</typeparam>
16  /// <typeparam name="V">The type of the values of the dictionary.</typeparam>
17  public partial class ItemDictionaryView<K, V> : ViewBase
18    where K : IItem
19    where V : IItem {
20
21    private EditKeyValueDialog editKeyValueDialog;
22
23    /// <summary>
24    /// Gets or sets the dictionary to represent visually.
25    /// </summary>
26    /// <remarks>Uses property <see cref="HeuristicLab.Core.ViewBase.Item"/> of base class <see cref="ViewBase"/>.
27    /// No own data storage present.</remarks>
28    public ItemDictionary<K, V> ItemDictionary {
29      get { return (ItemDictionary<K, V>) Item; }
30      set { base.Item = value; }
31    }
32
33    /// <summary>
34    /// Creates a new list view item with the given <paramref name="key"/> and <paramref name="value"/>.
35    /// </summary>
36    /// <param name="key">The key to show in the list item.</param>
37    /// <param name="value">The value to show in the list item.</param>
38    /// <returns>The created list item as <see cref="ListViewItem"/>.</returns>
39    private ListViewItem CreateListViewItem(K key, V value) {
40      ListViewItem item = new ListViewItem(key.ToString());
41      item.Name = key.ToString();
42      item.SubItems.Add(value.ToString());
43      item.SubItems[0].Tag = key;
44      item.SubItems[1].Tag = value;
45      return item;
46    }
47
48    /// <summary>
49    /// Initializes a new instance of the class <see cref="ItemDictionaryView&lt;K,V&gt;"/>.
50    /// </summary>
51    public ItemDictionaryView() {
52      InitializeComponent();
53      listView.View = View.Details;
54      listView.Columns[0].Text = "Key";
55      listView.Columns[1].Text = "Value";
56      valueTypeTextBox.Text = typeof(V).ToString();
57      keyTypeTextBox.Text = typeof(K).ToString();
58    }
59
60    /// <summary>
61    /// Initializes a new instance of the class <see cref="ItemDictionaryView&lt;K,V&gt;"/> with the given
62    /// <paramref name="dictionary"/>.
63    /// <note type="caution"> No CopyConstructor! <paramref name="dictionary"/> is not copied!</note>
64    /// </summary>
65    /// <param name="dictionary">The dictionary to represent visually.</param>
66    public ItemDictionaryView(ItemDictionary<K, V> dictionary)
67      : this() {
68      ItemDictionary = dictionary;
69    }
70
71    /// <summary>
72    /// Removes the eventhandlers from the underlying
73    /// <see cref="HeuristicLab.Data.ItemDictionary&lt;K,V&gt;"/>.
74    /// </summary>
75    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.
76    /// </remarks>
77    protected override void RemoveItemEvents() {
78      ItemDictionary.ItemAdded -= new EventHandler<EventArgs<IItem, IItem>>(ItemDictionary_ItemInserted);
79      ItemDictionary.ItemRemoved -= new EventHandler<EventArgs<IItem, IItem>>(ItemDictionary_ItemRemoved);
80      ItemDictionary.Cleared -= new EventHandler(ItemDictionary_Cleared);
81      base.RemoveItemEvents();
82    }
83
84    /// <summary>
85    /// Adds eventhandlers to the underlying <see cref="HeuristicLab.Data.ItemDictionary&lt;K,V&gt;"/>.
86    /// </summary>
87    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.
88    /// </remarks>
89    protected override void AddItemEvents() {
90      base.AddItemEvents();
91      ItemDictionary.ItemAdded += new EventHandler<EventArgs<IItem, IItem>>(ItemDictionary_ItemInserted);
92      ItemDictionary.ItemRemoved += new EventHandler<EventArgs<IItem, IItem>>(ItemDictionary_ItemRemoved);
93      ItemDictionary.Cleared += new EventHandler(ItemDictionary_Cleared);
94    }
95
96    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
97      if (detailsPanel.Controls.Count > 0)
98        detailsPanel.Controls[0].Dispose();
99      if (keyPanel.Controls.Count > 0)
100        keyPanel.Controls[0].Dispose();
101      detailsPanel.Controls.Clear();
102      keyPanel.Controls.Clear();
103      detailsPanel.Enabled = false;
104      keyPanel.Enabled = false;
105      removeButton.Enabled = false;
106      if (listView.SelectedItems.Count > 0) {
107        removeButton.Enabled = true;
108      }
109      if (listView.SelectedItems.Count == 1) {
110        K key = (K) listView.SelectedItems[0].SubItems[0].Tag;
111        V data = (V) listView.SelectedItems[0].SubItems[1].Tag;
112        Control keyView = (Control) key.CreateView();
113        Control dataView = (Control) data.CreateView();
114        keyPanel.Controls.Add(keyView);
115        detailsPanel.Controls.Add(dataView);
116        detailsPanel.Enabled = true;
117      }
118      keyPanel.Enabled = false;
119    }
120
121    #region Item and ItemDictionary Events
122    private void ItemDictionary_ItemInserted(object sender, EventArgs<IItem, IItem> e) {
123      if (InvokeRequired)
124        Invoke(new EventHandler<EventArgs<IItem, IItem>>(ItemDictionary_ItemInserted), sender, e);
125      else {
126        ListViewItem item = CreateListViewItem((K) e.Value, (V) e.Value2);
127        listView.Items.Insert(listView.Items.Count, item);
128        item.Name = e.Value.ToString();
129        e.Value2.Changed += new EventHandler(Item_Changed);
130        e.Value.Changed += new EventHandler(Item_Changed);
131      }
132    }
133
134    private void ItemDictionary_ItemRemoved(object sender, EventArgs<IItem, IItem> e) {
135      if (InvokeRequired)
136        Invoke(new EventHandler<EventArgs<IItem, IItem>>(ItemDictionary_ItemRemoved), sender, e);
137      else {
138        int index = listView.Items.IndexOfKey(e.Value.ToString());
139        listView.Items.RemoveAt(index);
140        e.Value.Changed -= new EventHandler(Item_Changed);
141        e.Value2.Changed += new EventHandler(Item_Changed);
142      }
143    }
144
145    private void ItemDictionary_Cleared(object sender, EventArgs e) {
146      Refresh();
147    }
148
149    private void Item_Changed(object sender, EventArgs e) {
150      if (InvokeRequired)
151        Invoke(new EventHandler(Item_Changed), sender, e);
152      else {
153        IItem data = (IItem) sender;
154        foreach (ListViewItem item in listView.Items) {
155          if (item.SubItems[0].Tag == data) {
156            item.SubItems[0].Text = data.ToString();
157            item.Name = data.ToString();
158          } else if (item.SubItems[1].Tag == data) {
159            item.SubItems[1].Text = data.ToString();
160          }
161        }
162      }
163    }
164    #endregion
165
166    #region Update Controls
167    /// <summary>
168    /// Updates the controls with the latest elements of the dictionary.
169    /// </summary>
170    protected override void UpdateControls() {
171      base.UpdateControls();
172      detailsPanel.Controls.Clear();
173      keyPanel.Controls.Clear();
174      detailsPanel.Enabled = false;
175      keyPanel.Enabled = false;
176      removeButton.Enabled = false;
177      if (ItemDictionary != null) {
178        foreach (ListViewItem item in listView.Items) {
179          ((IItem) item.SubItems[0]).Changed -= new EventHandler(Item_Changed);
180          ((IItem) item.SubItems[1]).Changed -= new EventHandler(Item_Changed);
181        }
182        listView.Items.Clear();
183        foreach (KeyValuePair<K, V> data in ItemDictionary) {
184          ListViewItem item = CreateListViewItem(data.Key, data.Value);
185          listView.Items.Add(item);
186          data.Key.Changed += new EventHandler(Item_Changed);
187          data.Value.Changed += new EventHandler(Item_Changed);
188        }
189        addButton.Enabled = true;
190      } else {
191        addButton.Enabled = false;
192      }
193    }
194    #endregion
195
196    #region Button Events
197    private void addButton_Click(object sender, EventArgs e) {
198      editKeyValueDialog = new EditKeyValueDialog(typeof(K), typeof(V));
199      if (editKeyValueDialog.ShowDialog(this) == DialogResult.OK) {
200        try {
201          if (!ItemDictionary.ContainsKey((K) editKeyValueDialog.Key)) {
202            ItemDictionary.Add((K) editKeyValueDialog.Key, (V) editKeyValueDialog.Value);
203          }
204        } catch (Exception ex) {
205          Auxiliary.ShowErrorMessageBox(ex);
206        }
207      }
208    }
209
210    private void removeButton_Click(object sender, EventArgs e) {
211      while (listView.SelectedIndices.Count > 0)
212        ItemDictionary.Remove((K) listView.SelectedItems[0].SubItems[0].Tag);
213    }
214    #endregion
215  }
216}
Note: See TracBrowser for help on using the repository browser.