Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemCollectionView.cs @ 10884

Last change on this file since 10884 was 10495, checked in by gkronber, 11 years ago

#1971: used the more general type ObservableCollection in ItemCollectionView

File size: 18.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Core.Views {
34  [View("ItemCollection View")]
35  [Content(typeof(ItemCollection<>), true)]
36  [Content(typeof(IItemCollection<>), false)]
37  public partial class ItemCollectionView<T> : ItemView where T : class, IItem {
38    protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
39    protected TypeSelectorDialog typeSelectorDialog;
40    protected bool validDragOperation;
41
42    public new IItemCollection<T> Content {
43      get { return (IItemCollection<T>)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public ObservableCollection<T> ItemCollection {
48      get { return Content as ObservableCollection<T>; }
49    }
50
51    public bool ShowDetails {
52      get { return showDetailsCheckBox.Checked; }
53      set { showDetailsCheckBox.Checked = value; }
54    }
55
56    public ListView ItemsListView {
57      get { return itemsListView; }
58    }
59
60    public ItemCollectionView() {
61      InitializeComponent();
62      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
63    }
64
65    protected override void Dispose(bool disposing) {
66      if (disposing) {
67        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
68        if (components != null) components.Dispose();
69      }
70      base.Dispose(disposing);
71    }
72
73    protected override void DeregisterContentEvents() {
74      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
75      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
76      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
77      foreach (T item in itemListViewItemMapping.Keys) {
78        DeregisterItemEvents(item);
79      }
80      base.DeregisterContentEvents();
81    }
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
85      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
86      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
87    }
88    protected virtual void DeregisterItemEvents(T item) {
89      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
90      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
91    }
92    protected virtual void RegisterItemEvents(T item) {
93      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
94      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
95    }
96
97    protected override void OnContentChanged() {
98      base.OnContentChanged();
99      itemsListView.Items.Clear();
100      itemListViewItemMapping.Clear();
101      RebuildImageList();
102      viewHost.Content = null;
103      if (Content != null) {
104        Caption += " (" + Content.GetType().Name + ")";
105        foreach (T item in Content)
106          AddListViewItem(CreateListViewItem(item));
107        AdjustListViewColumnSizes();
108        SortItemsListView(SortOrder.Ascending);
109      }
110    }
111
112    protected override void SetEnabledStateOfControls() {
113      base.SetEnabledStateOfControls();
114      if (Content == null) {
115        addButton.Enabled = false;
116        sortAscendingButton.Enabled = false;
117        sortDescendingButton.Enabled = false;
118        removeButton.Enabled = false;
119        itemsListView.Enabled = false;
120        detailsGroupBox.Enabled = false;
121      } else {
122        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
123        sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
124        sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
125        removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
126        itemsListView.Enabled = true;
127        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
128      }
129    }
130
131    protected virtual T CreateItem() {
132      if (typeSelectorDialog == null) {
133        typeSelectorDialog = new TypeSelectorDialog();
134        typeSelectorDialog.Caption = "Select Item";
135        typeSelectorDialog.TypeSelector.Caption = "Available Items";
136        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
137      }
138
139      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
140        try {
141          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
142        }
143        catch (Exception ex) {
144          ErrorHandling.ShowErrorDialog(this, ex);
145        }
146      }
147      return null;
148    }
149    protected virtual ListViewItem CreateListViewItem(T item) {
150      ListViewItem listViewItem = new ListViewItem();
151      if (item == null) {
152        listViewItem.Text = "null";
153        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
154        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
155      } else {
156        listViewItem.Text = item.ToString();
157        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
158        itemsListView.SmallImageList.Images.Add(item.ItemImage);
159        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
160        listViewItem.Tag = item;
161      }
162      return listViewItem;
163    }
164    protected virtual void AddListViewItem(ListViewItem listViewItem) {
165      if (listViewItem == null) throw new ArgumentNullException();
166      T item = (listViewItem.Tag as T);
167      itemsListView.Items.Add(listViewItem);
168      if (item != null) {
169        if (!itemListViewItemMapping.ContainsKey(item)) {
170          RegisterItemEvents(item);
171          itemListViewItemMapping.Add(item, new List<ListViewItem>());
172        }
173        itemListViewItemMapping[item].Add(listViewItem);
174      }
175      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
176      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
177    }
178    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
179      if (listViewItem == null) throw new ArgumentNullException();
180      T item = (listViewItem.Tag as T);
181      if (item != null) {
182        itemListViewItemMapping[item].Remove(listViewItem);
183        if (itemListViewItemMapping[item].Count == 0) {
184          itemListViewItemMapping.Remove(item);
185          DeregisterItemEvents(item);
186        }
187      }
188      listViewItem.Remove();
189      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
190      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
191    }
192    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
193      if (listViewItem == null) throw new ArgumentNullException();
194      T item = listViewItem.Tag as T;
195      int i = listViewItem.ImageIndex;
196      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
197      listViewItem.ImageIndex = -1;
198      listViewItem.ImageIndex = i;
199    }
200    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
201      if (listViewItem == null) throw new ArgumentNullException();
202      T item = listViewItem.Tag as T;
203      listViewItem.Text = item == null ? "null" : item.ToString();
204      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
205    }
206    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
207      if (item == null) {
208        List<ListViewItem> listViewItems = new List<ListViewItem>();
209        foreach (ListViewItem listViewItem in itemsListView.Items) {
210          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
211        }
212        return listViewItems;
213      } else {
214        List<ListViewItem> listViewItems = null;
215        itemListViewItemMapping.TryGetValue(item, out listViewItems);
216        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
217      }
218    }
219
220    #region ListView Events
221    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
222      removeButton.Enabled = (Content != null) && !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
223      AdjustListViewColumnSizes();
224      if (showDetailsCheckBox.Checked) {
225        if (itemsListView.SelectedItems.Count == 1) {
226          T item = (T)itemsListView.SelectedItems[0].Tag;
227          detailsGroupBox.Enabled = true;
228          viewHost.Content = item;
229        } else {
230          viewHost.Content = null;
231          detailsGroupBox.Enabled = false;
232        }
233      }
234    }
235    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
236      if (e.KeyCode == Keys.Delete) {
237        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
238          if (ItemCollection != null) ItemCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (T)i.Tag));
239          else {
240            foreach (ListViewItem item in itemsListView.SelectedItems)
241              Content.Remove((T)item.Tag);
242          }
243        }
244      }
245    }
246    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
247      if (itemsListView.SelectedItems.Count == 1) {
248        T item = itemsListView.SelectedItems[0].Tag as T;
249        if (item != null) {
250          IContentView view = MainFormManager.MainForm.ShowContent(item);
251          if (view != null) {
252            view.ReadOnly = ReadOnly;
253            view.Locked = Locked;
254          }
255        }
256      }
257    }
258    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
259      if (!Locked) {
260        List<T> items = new List<T>();
261        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
262          T item = listViewItem.Tag as T;
263          if (item != null) items.Add(item);
264        }
265
266        if (items.Count > 0) {
267          DataObject data = new DataObject();
268          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
269          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
270          if (Content.IsReadOnly || ReadOnly) {
271            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
272          } else {
273            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
274            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
275              foreach (T item in items) Content.Remove(item);
276            }
277          }
278        }
279      }
280    }
281    protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
282      validDragOperation = false;
283      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
284        validDragOperation = true;
285      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
286        validDragOperation = true;
287        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
288        foreach (object item in items)
289          validDragOperation = validDragOperation && (item is T);
290      }
291    }
292    protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
293      e.Effect = DragDropEffects.None;
294      if (validDragOperation) {
295        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
296        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
297        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
298        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
299        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
300      }
301    }
302    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
303      if (e.Effect != DragDropEffects.None) {
304        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
305          T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
306          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
307        } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
308          IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
309          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
310            Cloner cloner = new Cloner();
311            items = items.Select(x => cloner.Clone(x));
312          }
313          if (ItemCollection != null) ItemCollection.AddRange(items);
314          else {
315            foreach (T item in items)
316              Content.Add(item);
317          }
318        }
319      }
320    }
321    #endregion
322
323    #region Button Events
324    protected virtual void addButton_Click(object sender, EventArgs e) {
325      T item = CreateItem();
326      if (item != null)
327        Content.Add(item);
328    }
329    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
330      SortItemsListView(SortOrder.Ascending);
331    }
332    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
333      SortItemsListView(SortOrder.Descending);
334    }
335    protected virtual void removeButton_Click(object sender, EventArgs e) {
336      if (itemsListView.SelectedItems.Count > 0) {
337        if (ItemCollection != null) {
338          ItemCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (T)i.Tag));
339        } else {
340          foreach (ListViewItem item in itemsListView.SelectedItems)
341            Content.Remove((T)item.Tag);
342        }
343        itemsListView.SelectedItems.Clear();
344      }
345    }
346    #endregion
347
348    #region CheckBox Events
349    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
350      if (showDetailsCheckBox.Checked) {
351        splitContainer.Panel2Collapsed = false;
352        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
353        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
354      } else {
355        splitContainer.Panel2Collapsed = true;
356        viewHost.Content = null;
357      }
358    }
359    #endregion
360
361    #region Content Events
362    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
363      if (InvokeRequired)
364        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
365      else {
366        foreach (T item in e.Items)
367          AddListViewItem(CreateListViewItem(item));
368        AdjustListViewColumnSizes();
369      }
370
371    }
372    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
373      if (InvokeRequired)
374        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
375      else {
376        foreach (T item in e.Items) {
377          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
378          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
379          if (listviewItem != null) RemoveListViewItem(listviewItem);
380        }
381        RebuildImageList();
382      }
383    }
384    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
385      if (InvokeRequired)
386        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
387      else {
388        foreach (T item in e.OldItems) {
389          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
390          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
391          if (listviewItem != null) RemoveListViewItem(listviewItem);
392        }
393        RebuildImageList();
394        foreach (T item in e.Items)
395          AddListViewItem(CreateListViewItem(item));
396      }
397    }
398    #endregion
399
400    #region Item Events
401    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
402      if (InvokeRequired)
403        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
404      else {
405        T item = (T)sender;
406        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
407          UpdateListViewItemImage(listViewItem);
408      }
409    }
410    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
411      if (InvokeRequired)
412        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
413      else {
414        T item = (T)sender;
415        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
416          UpdateListViewItemText(listViewItem);
417        AdjustListViewColumnSizes();
418      }
419    }
420    #endregion
421
422    #region Helpers
423    protected virtual void SortItemsListView(SortOrder sortOrder) {
424      itemsListView.Sorting = SortOrder.None;
425      itemsListView.Sorting = sortOrder;
426      itemsListView.Sorting = SortOrder.None;
427    }
428    protected virtual void AdjustListViewColumnSizes() {
429      if (itemsListView.Items.Count > 0) {
430        for (int i = 0; i < itemsListView.Columns.Count; i++)
431          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
432      }
433    }
434    protected virtual void RebuildImageList() {
435      itemsListView.SmallImageList.Images.Clear();
436      foreach (ListViewItem listViewItem in itemsListView.Items) {
437        T item = listViewItem.Tag as T;
438        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
439        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
440      }
441    }
442    #endregion
443  }
444}
Note: See TracBrowser for help on using the repository browser.