Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11290 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 18.8 KB
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2655]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;
[5744]23using System.Collections;
[2655]24using System.Collections.Generic;
[4203]25using System.Linq;
[2655]26using System.Windows.Forms;
27using HeuristicLab.Collections;
[6472]28using HeuristicLab.Common;
[2655]29using HeuristicLab.MainForm;
[2713]30using HeuristicLab.MainForm.WindowsForms;
[3758]31using HeuristicLab.PluginInfrastructure;
[2655]32
33namespace HeuristicLab.Core.Views {
[2917]34  [View("ItemCollection View")]
[2727]35  [Content(typeof(ItemCollection<>), true)]
[3393]36  [Content(typeof(IItemCollection<>), false)]
[3483]37  public partial class ItemCollectionView<T> : ItemView where T : class, IItem {
[5237]38    protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
[3407]39    protected TypeSelectorDialog typeSelectorDialog;
[5744]40    protected bool validDragOperation;
[3407]41
[3393]42    public new IItemCollection<T> Content {
43      get { return (IItemCollection<T>)base.Content; }
[2713]44      set { base.Content = value; }
[2655]45    }
46
[10495]47    public ObservableCollection<T> ItemCollection {
48      get { return Content as ObservableCollection<T>; }
[9613]49    }
50
[6674]51    public bool ShowDetails {
52      get { return showDetailsCheckBox.Checked; }
53      set { showDetailsCheckBox.Checked = value; }
54    }
55
[2655]56    public ListView ItemsListView {
57      get { return itemsListView; }
58    }
59
60    public ItemCollectionView() {
61      InitializeComponent();
[5237]62      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
[2655]63    }
64
[5237]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
[2713]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);
[5237]77      foreach (T item in itemListViewItemMapping.Keys) {
78        DeregisterItemEvents(item);
[5068]79      }
[2713]80      base.DeregisterContentEvents();
[2655]81    }
[2713]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);
[2655]87    }
[5237]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    }
[2655]96
[2713]97    protected override void OnContentChanged() {
98      base.OnContentChanged();
[5237]99      itemsListView.Items.Clear();
100      itemListViewItemMapping.Clear();
[5057]101      RebuildImageList();
[2713]102      viewHost.Content = null;
103      if (Content != null) {
104        Caption += " (" + Content.GetType().Name + ")";
105        foreach (T item in Content)
[2655]106          AddListViewItem(CreateListViewItem(item));
[4240]107        AdjustListViewColumnSizes();
[3350]108        SortItemsListView(SortOrder.Ascending);
109      }
110    }
111
[3904]112    protected override void SetEnabledStateOfControls() {
113      base.SetEnabledStateOfControls();
[3350]114      if (Content == null) {
[3362]115        addButton.Enabled = false;
116        sortAscendingButton.Enabled = false;
117        sortDescendingButton.Enabled = false;
118        removeButton.Enabled = false;
[3350]119        itemsListView.Enabled = false;
120        detailsGroupBox.Enabled = false;
121      } else {
[3435]122        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
[3362]123        sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
124        sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
[3435]125        removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
[3350]126        itemsListView.Enabled = true;
[4099]127        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[2655]128      }
129    }
130
131    protected virtual T CreateItem() {
[3407]132      if (typeSelectorDialog == null) {
133        typeSelectorDialog = new TypeSelectorDialog();
134        typeSelectorDialog.Caption = "Select Item";
135        typeSelectorDialog.TypeSelector.Caption = "Available Items";
[3588]136        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
[2655]137      }
[3407]138
139      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
140        try {
141          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[5302]142        }
143        catch (Exception ex) {
[3758]144          ErrorHandling.ShowErrorDialog(this, ex);
[3407]145        }
146      }
147      return null;
[2655]148    }
149    protected virtual ListViewItem CreateListViewItem(T item) {
150      ListViewItem listViewItem = new ListViewItem();
[5237]151      if (item == null) {
152        listViewItem.Text = "null";
[5287]153        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
[5237]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      }
[2655]162      return listViewItem;
163    }
164    protected virtual void AddListViewItem(ListViewItem listViewItem) {
[5371]165      if (listViewItem == null) throw new ArgumentNullException();
[5057]166      T item = (listViewItem.Tag as T);
[2655]167      itemsListView.Items.Add(listViewItem);
[5237]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);
[5068]174      }
[3362]175      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
176      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
[2655]177    }
178    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
[5371]179      if (listViewItem == null) throw new ArgumentNullException();
[5057]180      T item = (listViewItem.Tag as T);
[5237]181      if (item != null) {
182        itemListViewItemMapping[item].Remove(listViewItem);
183        if (itemListViewItemMapping[item].Count == 0) {
184          itemListViewItemMapping.Remove(item);
185          DeregisterItemEvents(item);
186        }
[5057]187      }
[2655]188      listViewItem.Remove();
[3362]189      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
190      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
[2655]191    }
[3327]192    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
[5371]193      if (listViewItem == null) throw new ArgumentNullException();
[5237]194      T item = listViewItem.Tag as T;
[3327]195      int i = listViewItem.ImageIndex;
[5839]196      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
[3327]197      listViewItem.ImageIndex = -1;
198      listViewItem.ImageIndex = i;
199    }
200    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
[5371]201      if (listViewItem == null) throw new ArgumentNullException();
[5237]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;
[2655]205    }
206    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
[5237]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 {
[5302]214        List<ListViewItem> listViewItems = null;
215        itemListViewItemMapping.TryGetValue(item, out listViewItems);
216        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
[5237]217      }
[2655]218    }
219
220    #region ListView Events
221    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[3456]222      removeButton.Enabled = (Content != null) && !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
[3829]223      AdjustListViewColumnSizes();
[4096]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        }
[2655]233      }
234    }
235    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
236      if (e.KeyCode == Keys.Delete) {
[3435]237        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[9613]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          }
[2655]243        }
244      }
245    }
246    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
247      if (itemsListView.SelectedItems.Count == 1) {
[5237]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          }
[3416]255        }
[2655]256      }
257    }
258    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]259      if (!Locked) {
[5744]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) {
[5237]267          DataObject data = new DataObject();
[5837]268          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
269          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
[5237]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);
[5744]274            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
275              foreach (T item in items) Content.Remove(item);
276            }
[5237]277          }
[3432]278        }
[2655]279      }
280    }
[5744]281    protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
282      validDragOperation = false;
[5837]283      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
[5744]284        validDragOperation = true;
[5837]285      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
[5744]286        validDragOperation = true;
[5837]287        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]288        foreach (object item in items)
289          validDragOperation = validDragOperation && (item is T);
290      }
291    }
292    protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
[2655]293      e.Effect = DragDropEffects.None;
[5744]294      if (validDragOperation) {
[3694]295        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2694]296        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]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;
[2655]300      }
301    }
302    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
303      if (e.Effect != DragDropEffects.None) {
[5837]304        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
305          T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]306          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
[5837]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>();
[6472]309          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
310            Cloner cloner = new Cloner();
[6527]311            items = items.Select(x => cloner.Clone(x));
[6472]312          }
[9613]313          if (ItemCollection != null) ItemCollection.AddRange(items);
314          else {
315            foreach (T item in items)
316              Content.Add(item);
317          }
[5744]318        }
[2655]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)
[2713]327        Content.Add(item);
[2655]328    }
[2676]329    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
[2655]330      SortItemsListView(SortOrder.Ascending);
331    }
[2676]332    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
[2655]333      SortItemsListView(SortOrder.Descending);
334    }
335    protected virtual void removeButton_Click(object sender, EventArgs e) {
336      if (itemsListView.SelectedItems.Count > 0) {
[9613]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        }
[2655]343        itemsListView.SelectedItems.Clear();
344      }
345    }
346    #endregion
347
[4096]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
[2713]361    #region Content Events
362    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]363      if (InvokeRequired)
[2713]364        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
[4240]365      else {
[2655]366        foreach (T item in e.Items)
367          AddListViewItem(CreateListViewItem(item));
[4240]368        AdjustListViewColumnSizes();
369      }
370
[2655]371    }
[2713]372    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]373      if (InvokeRequired)
[2713]374        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
[2655]375      else {
376        foreach (T item in e.Items) {
[4203]377          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
378          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
[4240]379          if (listviewItem != null) RemoveListViewItem(listviewItem);
[2655]380        }
[5057]381        RebuildImageList();
[2655]382      }
383    }
[2713]384    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]385      if (InvokeRequired)
[2713]386        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
[2655]387      else {
388        foreach (T item in e.OldItems) {
[4203]389          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
390          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
[4240]391          if (listviewItem != null) RemoveListViewItem(listviewItem);
[2655]392        }
[5057]393        RebuildImageList();
[2655]394        foreach (T item in e.Items)
395          AddListViewItem(CreateListViewItem(item));
396      }
397    }
398    #endregion
399
400    #region Item Events
[3306]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))
[3327]407          UpdateListViewItemImage(listViewItem);
[3306]408      }
409    }
[2932]410    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
[2655]411      if (InvokeRequired)
[2932]412        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
[2655]413      else {
414        T item = (T)sender;
415        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3327]416          UpdateListViewItemText(listViewItem);
[9755]417        AdjustListViewColumnSizes();
[2655]418      }
419    }
420    #endregion
421
422    #region Helpers
[2676]423    protected virtual void SortItemsListView(SortOrder sortOrder) {
[9540]424      itemsListView.Sorting = SortOrder.None;
[2655]425      itemsListView.Sorting = sortOrder;
[9540]426      itemsListView.Sorting = SortOrder.None;
[2655]427    }
[3362]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    }
[5239]434    protected virtual void RebuildImageList() {
[5057]435      itemsListView.SmallImageList.Images.Clear();
[5239]436      foreach (ListViewItem listViewItem in itemsListView.Items) {
437        T item = listViewItem.Tag as T;
[5287]438        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]439        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5057]440      }
441    }
[2655]442    #endregion
443  }
444}
Note: See TracBrowser for help on using the repository browser.