Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13259 was 12618, checked in by abeham, 9 years ago

#2338: Added readonly-collection as default view

File size: 19.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Text;
27using System.Windows.Forms;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.MainForm;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Core.Views {
34  [View("ItemCollection View")]
35  [Content(typeof(ItemCollection<>), true)]
36  [Content(typeof(IItemCollection<>), false)]
37  [Content(typeof(ReadOnlyItemCollection<>), true)]
38  public partial class ItemCollectionView<T> : ItemView where T : class, IItem {
39    protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
40    protected TypeSelectorDialog typeSelectorDialog;
41    protected bool validDragOperation;
42
43    public new IItemCollection<T> Content {
44      get { return (IItemCollection<T>)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public ObservableCollection<T> ItemCollection {
49      get { return Content as ObservableCollection<T>; }
50    }
51
52    public bool ShowDetails {
53      get { return showDetailsCheckBox.Checked; }
54      set { showDetailsCheckBox.Checked = value; }
55    }
56
57    public ListView ItemsListView {
58      get { return itemsListView; }
59    }
60
61    public ItemCollectionView() {
62      InitializeComponent();
63      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
64    }
65
66    protected override void Dispose(bool disposing) {
67      if (disposing) {
68        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
69        if (components != null) components.Dispose();
70      }
71      base.Dispose(disposing);
72    }
73
74    protected override void DeregisterContentEvents() {
75      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
76      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
77      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
78      foreach (T item in itemListViewItemMapping.Keys) {
79        DeregisterItemEvents(item);
80      }
81      base.DeregisterContentEvents();
82    }
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
86      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
87      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
88    }
89    protected virtual void DeregisterItemEvents(T item) {
90      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
91      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
92    }
93    protected virtual void RegisterItemEvents(T item) {
94      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
95      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
96    }
97
98    protected override void OnContentChanged() {
99      base.OnContentChanged();
100      itemsListView.Items.Clear();
101      itemListViewItemMapping.Clear();
102      RebuildImageList();
103      viewHost.Content = null;
104      if (Content != null) {
105        Caption += " (" + Content.GetType().Name + ")";
106        foreach (T item in Content)
107          AddListViewItem(CreateListViewItem(item));
108        AdjustListViewColumnSizes();
109        SortItemsListView(SortOrder.Ascending);
110      }
111    }
112
113    protected override void SetEnabledStateOfControls() {
114      base.SetEnabledStateOfControls();
115      if (Content == null) {
116        addButton.Enabled = false;
117        sortAscendingButton.Enabled = false;
118        sortDescendingButton.Enabled = false;
119        removeButton.Enabled = false;
120        itemsListView.Enabled = false;
121        detailsGroupBox.Enabled = false;
122      } else {
123        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
124        sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
125        sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
126        removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
127        itemsListView.Enabled = true;
128        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
129      }
130    }
131
132    protected virtual T CreateItem() {
133      if (typeSelectorDialog == null) {
134        typeSelectorDialog = new TypeSelectorDialog();
135        typeSelectorDialog.Caption = "Select Item";
136        typeSelectorDialog.TypeSelector.Caption = "Available Items";
137        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
138      }
139
140      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
141        try {
142          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
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      } else if (e.KeyData == (Keys.Control | Keys.C)) {
245        if (itemsListView.SelectedItems.Count > 0) {
246          var builder = new StringBuilder();
247          foreach (ListViewItem selected in itemsListView.SelectedItems) {
248            builder.AppendLine(selected.Text);
249          }
250          Clipboard.SetText(builder.ToString());
251        }
252      }
253    }
254    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
255      if (itemsListView.SelectedItems.Count == 1) {
256        T item = itemsListView.SelectedItems[0].Tag as T;
257        if (item != null) {
258          IContentView view = MainFormManager.MainForm.ShowContent(item);
259          if (view != null) {
260            view.ReadOnly = ReadOnly;
261            view.Locked = Locked;
262          }
263        }
264      }
265    }
266    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
267      if (!Locked) {
268        List<T> items = new List<T>();
269        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
270          T item = listViewItem.Tag as T;
271          if (item != null) items.Add(item);
272        }
273
274        if (items.Count > 0) {
275          DataObject data = new DataObject();
276          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
277          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
278          if (Content.IsReadOnly || ReadOnly) {
279            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
280          } else {
281            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
282            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
283              foreach (T item in items) Content.Remove(item);
284            }
285          }
286        }
287      }
288    }
289    protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
290      validDragOperation = false;
291      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
292        validDragOperation = true;
293      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
294        validDragOperation = true;
295        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
296        foreach (object item in items)
297          validDragOperation = validDragOperation && (item is T);
298      }
299    }
300    protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
301      e.Effect = DragDropEffects.None;
302      if (validDragOperation) {
303        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
304        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
305        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
306        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
307        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
308      }
309    }
310    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
311      if (e.Effect != DragDropEffects.None) {
312        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
313          T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
314          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
315        } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
316          IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
317          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
318            Cloner cloner = new Cloner();
319            items = items.Select(x => cloner.Clone(x));
320          }
321          if (ItemCollection != null) ItemCollection.AddRange(items);
322          else {
323            foreach (T item in items)
324              Content.Add(item);
325          }
326        }
327      }
328    }
329    #endregion
330
331    #region Button Events
332    protected virtual void addButton_Click(object sender, EventArgs e) {
333      T item = CreateItem();
334      if (item != null)
335        Content.Add(item);
336    }
337    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
338      SortItemsListView(SortOrder.Ascending);
339    }
340    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
341      SortItemsListView(SortOrder.Descending);
342    }
343    protected virtual void removeButton_Click(object sender, EventArgs e) {
344      if (itemsListView.SelectedItems.Count > 0) {
345        if (ItemCollection != null) {
346          ItemCollection.RemoveRange(itemsListView.SelectedItems.Cast<ListViewItem>().Select(i => (T)i.Tag));
347        } else {
348          foreach (ListViewItem item in itemsListView.SelectedItems)
349            Content.Remove((T)item.Tag);
350        }
351        itemsListView.SelectedItems.Clear();
352      }
353    }
354    #endregion
355
356    #region CheckBox Events
357    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
358      if (showDetailsCheckBox.Checked) {
359        splitContainer.Panel2Collapsed = false;
360        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
361        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
362      } else {
363        splitContainer.Panel2Collapsed = true;
364        viewHost.Content = null;
365      }
366    }
367    #endregion
368
369    #region Content Events
370    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
371      if (InvokeRequired)
372        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
373      else {
374        foreach (T item in e.Items)
375          AddListViewItem(CreateListViewItem(item));
376        AdjustListViewColumnSizes();
377      }
378
379    }
380    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
381      if (InvokeRequired)
382        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
383      else {
384        foreach (T item in e.Items) {
385          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
386          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
387          if (listviewItem != null) RemoveListViewItem(listviewItem);
388        }
389        RebuildImageList();
390      }
391    }
392    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
393      if (InvokeRequired)
394        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
395      else {
396        foreach (T item in e.OldItems) {
397          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
398          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
399          if (listviewItem != null) RemoveListViewItem(listviewItem);
400        }
401        RebuildImageList();
402        foreach (T item in e.Items)
403          AddListViewItem(CreateListViewItem(item));
404      }
405    }
406    #endregion
407
408    #region Item Events
409    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
410      if (InvokeRequired)
411        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
412      else {
413        T item = (T)sender;
414        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
415          UpdateListViewItemImage(listViewItem);
416      }
417    }
418    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
419      if (InvokeRequired)
420        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
421      else {
422        T item = (T)sender;
423        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
424          UpdateListViewItemText(listViewItem);
425        AdjustListViewColumnSizes();
426      }
427    }
428    #endregion
429
430    #region Helpers
431    protected virtual void SortItemsListView(SortOrder sortOrder) {
432      itemsListView.Sorting = SortOrder.None;
433      itemsListView.Sorting = sortOrder;
434      itemsListView.Sorting = SortOrder.None;
435    }
436    protected virtual void AdjustListViewColumnSizes() {
437      if (itemsListView.Items.Count > 0) {
438        for (int i = 0; i < itemsListView.Columns.Count; i++)
439          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
440      }
441    }
442    protected virtual void RebuildImageList() {
443      itemsListView.SmallImageList.Images.Clear();
444      foreach (ListViewItem listViewItem in itemsListView.Items) {
445        T item = listViewItem.Tag as T;
446        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
447        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
448      }
449    }
450    #endregion
451  }
452}
Note: See TracBrowser for help on using the repository browser.