Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Core.Views/3.3/ItemCollectionView.cs @ 5991

Last change on this file since 5991 was 5744, checked in by swagner, 14 years ago

Implemented dragging of multiple items in all collection views and refactored drag & drop code (#1112)

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