Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Core.Views/3.3/ItemCollectionView.cs @ 7268

Last change on this file since 7268 was 7268, checked in by gkronber, 13 years ago

#1081: merged r7214:7266 from trunk into time series branch.

File size: 18.2 KB
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[7268]3 * Copyright (C) 2002-2012 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
[6674]47    public bool ShowDetails {
48      get { return showDetailsCheckBox.Checked; }
49      set { showDetailsCheckBox.Checked = value; }
50    }
51
[2655]52    public ListView ItemsListView {
53      get { return itemsListView; }
54    }
55
56    public ItemCollectionView() {
57      InitializeComponent();
[5237]58      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
[2655]59    }
60
[5237]61    protected override void Dispose(bool disposing) {
62      if (disposing) {
63        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
64        if (components != null) components.Dispose();
65      }
66      base.Dispose(disposing);
67    }
68
[2713]69    protected override void DeregisterContentEvents() {
70      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
71      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
72      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
[5237]73      foreach (T item in itemListViewItemMapping.Keys) {
74        DeregisterItemEvents(item);
[5068]75      }
[2713]76      base.DeregisterContentEvents();
[2655]77    }
[2713]78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
81      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
82      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
[2655]83    }
[5237]84    protected virtual void DeregisterItemEvents(T item) {
85      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
86      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
87    }
88    protected virtual void RegisterItemEvents(T item) {
89      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
90      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
91    }
[2655]92
[2713]93    protected override void OnContentChanged() {
94      base.OnContentChanged();
[5237]95      itemsListView.Items.Clear();
96      itemListViewItemMapping.Clear();
[5057]97      RebuildImageList();
[2713]98      viewHost.Content = null;
99      if (Content != null) {
100        Caption += " (" + Content.GetType().Name + ")";
101        foreach (T item in Content)
[2655]102          AddListViewItem(CreateListViewItem(item));
[4240]103        AdjustListViewColumnSizes();
[3350]104        SortItemsListView(SortOrder.Ascending);
105      }
106    }
107
[3904]108    protected override void SetEnabledStateOfControls() {
109      base.SetEnabledStateOfControls();
[3350]110      if (Content == null) {
[3362]111        addButton.Enabled = false;
112        sortAscendingButton.Enabled = false;
113        sortDescendingButton.Enabled = false;
114        removeButton.Enabled = false;
[3350]115        itemsListView.Enabled = false;
116        detailsGroupBox.Enabled = false;
117      } else {
[3435]118        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
[3362]119        sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
120        sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
[3435]121        removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
[3350]122        itemsListView.Enabled = true;
[4099]123        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[2655]124      }
125    }
126
127    protected virtual T CreateItem() {
[3407]128      if (typeSelectorDialog == null) {
129        typeSelectorDialog = new TypeSelectorDialog();
130        typeSelectorDialog.Caption = "Select Item";
131        typeSelectorDialog.TypeSelector.Caption = "Available Items";
[3588]132        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
[2655]133      }
[3407]134
135      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
136        try {
137          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[5302]138        }
139        catch (Exception ex) {
[3758]140          ErrorHandling.ShowErrorDialog(this, ex);
[3407]141        }
142      }
143      return null;
[2655]144    }
145    protected virtual ListViewItem CreateListViewItem(T item) {
146      ListViewItem listViewItem = new ListViewItem();
[5237]147      if (item == null) {
148        listViewItem.Text = "null";
[5287]149        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
[5237]150        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
151      } else {
152        listViewItem.Text = item.ToString();
153        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
154        itemsListView.SmallImageList.Images.Add(item.ItemImage);
155        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
156        listViewItem.Tag = item;
157      }
[2655]158      return listViewItem;
159    }
160    protected virtual void AddListViewItem(ListViewItem listViewItem) {
[5371]161      if (listViewItem == null) throw new ArgumentNullException();
[5057]162      T item = (listViewItem.Tag as T);
[2655]163      itemsListView.Items.Add(listViewItem);
[5237]164      if (item != null) {
165        if (!itemListViewItemMapping.ContainsKey(item)) {
166          RegisterItemEvents(item);
167          itemListViewItemMapping.Add(item, new List<ListViewItem>());
168        }
169        itemListViewItemMapping[item].Add(listViewItem);
[5068]170      }
[3362]171      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
172      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
[2655]173    }
174    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
[5371]175      if (listViewItem == null) throw new ArgumentNullException();
[5057]176      T item = (listViewItem.Tag as T);
[5237]177      if (item != null) {
178        itemListViewItemMapping[item].Remove(listViewItem);
179        if (itemListViewItemMapping[item].Count == 0) {
180          itemListViewItemMapping.Remove(item);
181          DeregisterItemEvents(item);
182        }
[5057]183      }
[2655]184      listViewItem.Remove();
[3362]185      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
186      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
[2655]187    }
[3327]188    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
[5371]189      if (listViewItem == null) throw new ArgumentNullException();
[5237]190      T item = listViewItem.Tag as T;
[3327]191      int i = listViewItem.ImageIndex;
[5839]192      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
[3327]193      listViewItem.ImageIndex = -1;
194      listViewItem.ImageIndex = i;
195    }
196    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
[5371]197      if (listViewItem == null) throw new ArgumentNullException();
[5237]198      T item = listViewItem.Tag as T;
199      listViewItem.Text = item == null ? "null" : item.ToString();
200      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
[2655]201    }
202    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
[5237]203      if (item == null) {
204        List<ListViewItem> listViewItems = new List<ListViewItem>();
205        foreach (ListViewItem listViewItem in itemsListView.Items) {
206          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
207        }
208        return listViewItems;
209      } else {
[5302]210        List<ListViewItem> listViewItems = null;
211        itemListViewItemMapping.TryGetValue(item, out listViewItems);
212        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
[5237]213      }
[2655]214    }
215
216    #region ListView Events
217    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[3456]218      removeButton.Enabled = (Content != null) && !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
[3829]219      AdjustListViewColumnSizes();
[4096]220      if (showDetailsCheckBox.Checked) {
221        if (itemsListView.SelectedItems.Count == 1) {
222          T item = (T)itemsListView.SelectedItems[0].Tag;
223          detailsGroupBox.Enabled = true;
224          viewHost.Content = item;
225        } else {
226          viewHost.Content = null;
227          detailsGroupBox.Enabled = false;
228        }
[2655]229      }
230    }
231    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
232      if (e.KeyCode == Keys.Delete) {
[3435]233        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[2655]234          foreach (ListViewItem item in itemsListView.SelectedItems)
[2713]235            Content.Remove((T)item.Tag);
[2655]236        }
237      }
238    }
239    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
240      if (itemsListView.SelectedItems.Count == 1) {
[5237]241        T item = itemsListView.SelectedItems[0].Tag as T;
242        if (item != null) {
243          IContentView view = MainFormManager.MainForm.ShowContent(item);
244          if (view != null) {
245            view.ReadOnly = ReadOnly;
246            view.Locked = Locked;
247          }
[3416]248        }
[2655]249      }
250    }
251    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]252      if (!Locked) {
[5744]253        List<T> items = new List<T>();
254        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
255          T item = listViewItem.Tag as T;
256          if (item != null) items.Add(item);
257        }
258
259        if (items.Count > 0) {
[5237]260          DataObject data = new DataObject();
[5837]261          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
262          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
[5237]263          if (Content.IsReadOnly || ReadOnly) {
264            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
265          } else {
266            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
[5744]267            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
268              foreach (T item in items) Content.Remove(item);
269            }
[5237]270          }
[3432]271        }
[2655]272      }
273    }
[5744]274    protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
275      validDragOperation = false;
[5837]276      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
[5744]277        validDragOperation = true;
[5837]278      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
[5744]279        validDragOperation = true;
[5837]280        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]281        foreach (object item in items)
282          validDragOperation = validDragOperation && (item is T);
283      }
284    }
285    protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
[2655]286      e.Effect = DragDropEffects.None;
[5744]287      if (validDragOperation) {
[3694]288        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2694]289        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]290        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
291        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
292        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[2655]293      }
294    }
295    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
296      if (e.Effect != DragDropEffects.None) {
[5837]297        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
298          T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]299          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
[5837]300        } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
301          IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
[6472]302          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
303            Cloner cloner = new Cloner();
[6527]304            items = items.Select(x => cloner.Clone(x));
[6472]305          }
[5744]306          foreach (T item in items)
[6472]307            Content.Add(item);
[5744]308        }
[2655]309      }
310    }
311    #endregion
312
313    #region Button Events
314    protected virtual void addButton_Click(object sender, EventArgs e) {
315      T item = CreateItem();
316      if (item != null)
[2713]317        Content.Add(item);
[2655]318    }
[2676]319    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
[2655]320      SortItemsListView(SortOrder.Ascending);
321    }
[2676]322    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
[2655]323      SortItemsListView(SortOrder.Descending);
324    }
325    protected virtual void removeButton_Click(object sender, EventArgs e) {
326      if (itemsListView.SelectedItems.Count > 0) {
327        foreach (ListViewItem item in itemsListView.SelectedItems)
[2713]328          Content.Remove((T)item.Tag);
[2655]329        itemsListView.SelectedItems.Clear();
330      }
331    }
332    #endregion
333
[4096]334    #region CheckBox Events
335    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
336      if (showDetailsCheckBox.Checked) {
337        splitContainer.Panel2Collapsed = false;
338        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
339        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
340      } else {
341        splitContainer.Panel2Collapsed = true;
342        viewHost.Content = null;
343      }
344    }
345    #endregion
346
[2713]347    #region Content Events
348    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]349      if (InvokeRequired)
[2713]350        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
[4240]351      else {
[2655]352        foreach (T item in e.Items)
353          AddListViewItem(CreateListViewItem(item));
[4240]354        AdjustListViewColumnSizes();
355      }
356
[2655]357    }
[2713]358    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]359      if (InvokeRequired)
[2713]360        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
[2655]361      else {
362        foreach (T item in e.Items) {
[4203]363          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
364          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
[4240]365          if (listviewItem != null) RemoveListViewItem(listviewItem);
[2655]366        }
[5057]367        RebuildImageList();
[2655]368      }
369    }
[2713]370    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]371      if (InvokeRequired)
[2713]372        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
[2655]373      else {
374        foreach (T item in e.OldItems) {
[4203]375          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
376          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
[4240]377          if (listviewItem != null) RemoveListViewItem(listviewItem);
[2655]378        }
[5057]379        RebuildImageList();
[2655]380        foreach (T item in e.Items)
381          AddListViewItem(CreateListViewItem(item));
382      }
383    }
384    #endregion
385
386    #region Item Events
[3306]387    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
388      if (InvokeRequired)
389        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
390      else {
391        T item = (T)sender;
392        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3327]393          UpdateListViewItemImage(listViewItem);
[3306]394      }
395    }
[2932]396    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
[2655]397      if (InvokeRequired)
[2932]398        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
[2655]399      else {
400        T item = (T)sender;
401        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3327]402          UpdateListViewItemText(listViewItem);
[2655]403      }
404    }
405    #endregion
406
407    #region Helpers
[2676]408    protected virtual void SortItemsListView(SortOrder sortOrder) {
[3298]409      itemsListView.Sorting = SortOrder.None;
[2655]410      itemsListView.Sorting = sortOrder;
411      itemsListView.Sorting = SortOrder.None;
412    }
[3362]413    protected virtual void AdjustListViewColumnSizes() {
414      if (itemsListView.Items.Count > 0) {
415        for (int i = 0; i < itemsListView.Columns.Count; i++)
416          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
417      }
418    }
[5239]419    protected virtual void RebuildImageList() {
[5057]420      itemsListView.SmallImageList.Images.Clear();
[5239]421      foreach (ListViewItem listViewItem in itemsListView.Items) {
422        T item = listViewItem.Tag as T;
[5287]423        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]424        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5057]425      }
426    }
[2655]427    #endregion
428  }
429}
Note: See TracBrowser for help on using the repository browser.