Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2830 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

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