Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Core.Views/3.3/ItemCollectionView.cs @ 11194

Last change on this file since 11194 was 10103, checked in by jkarder, 11 years ago

#2116:

  • fixed issues within the outermost control detection
  • fixed visible property assignment of the breadcrumb control in ViewHost
File size: 19.2 KB
RevLine 
[2655]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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
[9613]47    public ItemCollection<T> ItemCollection {
48      get { return Content as ItemCollection<T>; }
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();
[10042]142        } catch (Exception ex) {
[3758]143          ErrorHandling.ShowErrorDialog(this, ex);
[3407]144        }
145      }
146      return null;
[2655]147    }
148    protected virtual ListViewItem CreateListViewItem(T item) {
149      ListViewItem listViewItem = new ListViewItem();
[5237]150      if (item == null) {
151        listViewItem.Text = "null";
[5287]152        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
[5237]153        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
154      } else {
155        listViewItem.Text = item.ToString();
156        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
157        itemsListView.SmallImageList.Images.Add(item.ItemImage);
158        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
159        listViewItem.Tag = item;
160      }
[2655]161      return listViewItem;
162    }
163    protected virtual void AddListViewItem(ListViewItem listViewItem) {
[5371]164      if (listViewItem == null) throw new ArgumentNullException();
[5057]165      T item = (listViewItem.Tag as T);
[2655]166      itemsListView.Items.Add(listViewItem);
[5237]167      if (item != null) {
168        if (!itemListViewItemMapping.ContainsKey(item)) {
169          RegisterItemEvents(item);
170          itemListViewItemMapping.Add(item, new List<ListViewItem>());
171        }
172        itemListViewItemMapping[item].Add(listViewItem);
[5068]173      }
[3362]174      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
175      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
[2655]176    }
177    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
[5371]178      if (listViewItem == null) throw new ArgumentNullException();
[5057]179      T item = (listViewItem.Tag as T);
[5237]180      if (item != null) {
181        itemListViewItemMapping[item].Remove(listViewItem);
182        if (itemListViewItemMapping[item].Count == 0) {
183          itemListViewItemMapping.Remove(item);
184          DeregisterItemEvents(item);
185        }
[5057]186      }
[2655]187      listViewItem.Remove();
[3362]188      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
189      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
[2655]190    }
[3327]191    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
[5371]192      if (listViewItem == null) throw new ArgumentNullException();
[5237]193      T item = listViewItem.Tag as T;
[3327]194      int i = listViewItem.ImageIndex;
[5839]195      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
[3327]196      listViewItem.ImageIndex = -1;
197      listViewItem.ImageIndex = i;
198    }
199    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
[5371]200      if (listViewItem == null) throw new ArgumentNullException();
[5237]201      T item = listViewItem.Tag as T;
202      listViewItem.Text = item == null ? "null" : item.ToString();
203      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
[2655]204    }
205    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
[5237]206      if (item == null) {
207        List<ListViewItem> listViewItems = new List<ListViewItem>();
208        foreach (ListViewItem listViewItem in itemsListView.Items) {
209          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
210        }
211        return listViewItems;
212      } else {
[5302]213        List<ListViewItem> listViewItems = null;
214        itemListViewItemMapping.TryGetValue(item, out listViewItems);
215        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
[5237]216      }
[2655]217    }
218
219    #region ListView Events
220    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[10089]221      removeButton.Enabled = Content != null && !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
[3829]222      AdjustListViewColumnSizes();
[10089]223      if (itemsListView.SelectedItems.Count == 1) {
224        var item = (T)itemsListView.SelectedItems[0].Tag;
225        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
[10093]226        var outermostViewHost = mainForm.ShowContentInOutermostViewHost(item, this, true);
[10103]227        if (outermostViewHost == null || !outermostViewHost.HotlinkingEnabled) {
[10089]228          if (showDetailsCheckBox.Checked) {
229            detailsGroupBox.Enabled = true;
230            viewHost.Content = item;
231          } else {
232            viewHost.Content = null;
233            detailsGroupBox.Enabled = false;
234          }
[4096]235        }
[2655]236      }
237    }
238    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
239      if (e.KeyCode == Keys.Delete) {
[3435]240        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[9613]241          if (ItemCollection != null) ItemCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (T)i.Tag));
242          else {
243            foreach (ListViewItem item in itemsListView.SelectedItems)
244              Content.Remove((T)item.Tag);
245          }
[2655]246        }
247      }
248    }
249    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
250      if (itemsListView.SelectedItems.Count == 1) {
[10089]251        var item = (T)itemsListView.SelectedItems[0].Tag;
252        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
[10093]253        var outermostViewHost = mainForm.ShowContentInOutermostViewHost(item, this);
[10103]254        if (outermostViewHost != null) {
255          outermostViewHost.ActiveView.ReadOnly = ReadOnly;
256          outermostViewHost.ActiveView.Locked = Locked;
257        }
[2655]258      }
259    }
260    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]261      if (!Locked) {
[5744]262        List<T> items = new List<T>();
263        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
264          T item = listViewItem.Tag as T;
265          if (item != null) items.Add(item);
266        }
267
268        if (items.Count > 0) {
[5237]269          DataObject data = new DataObject();
[5837]270          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
271          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
[5237]272          if (Content.IsReadOnly || ReadOnly) {
273            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
274          } else {
275            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
[5744]276            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
277              foreach (T item in items) Content.Remove(item);
278            }
[5237]279          }
[3432]280        }
[2655]281      }
282    }
[5744]283    protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
284      validDragOperation = false;
[5837]285      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
[5744]286        validDragOperation = true;
[5837]287      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
[5744]288        validDragOperation = true;
[5837]289        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]290        foreach (object item in items)
291          validDragOperation = validDragOperation && (item is T);
292      }
293    }
294    protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
[2655]295      e.Effect = DragDropEffects.None;
[5744]296      if (validDragOperation) {
[3694]297        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2694]298        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]299        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
300        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
301        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[2655]302      }
303    }
304    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
305      if (e.Effect != DragDropEffects.None) {
[5837]306        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
307          T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]308          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
[5837]309        } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
310          IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
[6472]311          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
312            Cloner cloner = new Cloner();
[6527]313            items = items.Select(x => cloner.Clone(x));
[6472]314          }
[9613]315          if (ItemCollection != null) ItemCollection.AddRange(items);
316          else {
317            foreach (T item in items)
318              Content.Add(item);
319          }
[5744]320        }
[2655]321      }
322    }
323    #endregion
324
325    #region Button Events
326    protected virtual void addButton_Click(object sender, EventArgs e) {
327      T item = CreateItem();
328      if (item != null)
[2713]329        Content.Add(item);
[2655]330    }
[2676]331    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
[2655]332      SortItemsListView(SortOrder.Ascending);
333    }
[2676]334    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
[2655]335      SortItemsListView(SortOrder.Descending);
336    }
337    protected virtual void removeButton_Click(object sender, EventArgs e) {
338      if (itemsListView.SelectedItems.Count > 0) {
[9613]339        if (ItemCollection != null) {
340          ItemCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (T)i.Tag));
341        } else {
342          foreach (ListViewItem item in itemsListView.SelectedItems)
343            Content.Remove((T)item.Tag);
344        }
[2655]345        itemsListView.SelectedItems.Clear();
346      }
347    }
348    #endregion
349
[4096]350    #region CheckBox Events
351    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
352      if (showDetailsCheckBox.Checked) {
353        splitContainer.Panel2Collapsed = false;
354        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
355        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
356      } else {
357        splitContainer.Panel2Collapsed = true;
358        viewHost.Content = null;
359      }
360    }
361    #endregion
362
[2713]363    #region Content Events
364    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]365      if (InvokeRequired)
[2713]366        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
[4240]367      else {
[2655]368        foreach (T item in e.Items)
369          AddListViewItem(CreateListViewItem(item));
[4240]370        AdjustListViewColumnSizes();
371      }
372
[2655]373    }
[2713]374    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]375      if (InvokeRequired)
[2713]376        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
[2655]377      else {
378        foreach (T item in e.Items) {
[4203]379          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
380          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
[4240]381          if (listviewItem != null) RemoveListViewItem(listviewItem);
[2655]382        }
[5057]383        RebuildImageList();
[2655]384      }
385    }
[2713]386    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
[2655]387      if (InvokeRequired)
[2713]388        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
[2655]389      else {
390        foreach (T item in e.OldItems) {
[4203]391          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
392          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
[4240]393          if (listviewItem != null) RemoveListViewItem(listviewItem);
[2655]394        }
[5057]395        RebuildImageList();
[2655]396        foreach (T item in e.Items)
397          AddListViewItem(CreateListViewItem(item));
398      }
399    }
400    #endregion
401
402    #region Item Events
[3306]403    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
404      if (InvokeRequired)
405        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
406      else {
407        T item = (T)sender;
408        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3327]409          UpdateListViewItemImage(listViewItem);
[3306]410      }
411    }
[2932]412    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
[2655]413      if (InvokeRequired)
[2932]414        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
[2655]415      else {
416        T item = (T)sender;
417        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3327]418          UpdateListViewItemText(listViewItem);
[9755]419        AdjustListViewColumnSizes();
[2655]420      }
421    }
422    #endregion
423
424    #region Helpers
[2676]425    protected virtual void SortItemsListView(SortOrder sortOrder) {
[9540]426      itemsListView.Sorting = SortOrder.None;
[2655]427      itemsListView.Sorting = sortOrder;
[9540]428      itemsListView.Sorting = SortOrder.None;
[2655]429    }
[3362]430    protected virtual void AdjustListViewColumnSizes() {
431      if (itemsListView.Items.Count > 0) {
432        for (int i = 0; i < itemsListView.Columns.Count; i++)
433          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
434      }
435    }
[5239]436    protected virtual void RebuildImageList() {
[5057]437      itemsListView.SmallImageList.Images.Clear();
[5239]438      foreach (ListViewItem listViewItem in itemsListView.Items) {
439        T item = listViewItem.Tag as T;
[5287]440        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]441        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5057]442      }
443    }
[2655]444    #endregion
445  }
446}
Note: See TracBrowser for help on using the repository browser.