Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ItemDictionaryView_T.cs @ 2299

Last change on this file since 2299 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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