Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Data/ItemDictionaryView_T.cs @ 2498

Last change on this file since 2498 was 194, checked in by abeham, 17 years ago

moved type identification to default constructor

File size: 6.3 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  public partial class ItemDictionaryView<K, V> : ViewBase
12    where K : IItem
13    where V : IItem {
14
15    private EditKeyValueDialog editKeyValueDialog;
16
17    public ItemDictionary<K, V> ItemDictionary {
18      get { return (ItemDictionary<K, V>) Item; }
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
31    public ItemDictionaryView() {
32      InitializeComponent();
33      listView.View = View.Details;
34      listView.Columns[0].Text = "Key";
35      listView.Columns[1].Text = "Value";
36      valueTypeTextBox.Text = typeof(V).ToString();
37      keyTypeTextBox.Text = typeof(K).ToString();
38    }
39
40    public ItemDictionaryView(ItemDictionary<K, V> dictionary)
41      : this() {
42      ItemDictionary = dictionary;
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;
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;
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}
Note: See TracBrowser for help on using the repository browser.