Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Core.Views/3.3/ItemListView.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: 21.9 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.Drawing;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Collections;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Core.Views {
34  [View("ItemList View")]
35  [Content(typeof(ItemList<>), true)]
36  [Content(typeof(IItemList<>), false)]
37  public partial class ItemListView<T> : ItemView where T : class, IItem {
38    protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
39    protected TypeSelectorDialog typeSelectorDialog;
40    protected bool validDragOperation;
41
42    public new IItemList<T> Content {
43      get { return (IItemList<T>)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public ListView ItemsListView {
48      get { return itemsListView; }
49    }
50
51    public ItemListView() {
52      InitializeComponent();
53      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
54    }
55
56    protected override void Dispose(bool disposing) {
57      if (disposing) {
58        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
59        if (components != null) components.Dispose();
60      }
61      base.Dispose(disposing);
62    }
63
64    protected override void DeregisterContentEvents() {
65      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
66      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
67      Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
68      Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
69      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
70      foreach (T item in itemListViewItemMapping.Keys) {
71        DeregisterItemEvents(item);
72      }
73      base.DeregisterContentEvents();
74    }
75    protected override void RegisterContentEvents() {
76      base.RegisterContentEvents();
77      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
78      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
79      Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
80      Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
81      Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
82    }
83    protected virtual void DeregisterItemEvents(T item) {
84      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
85      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
86    }
87    protected virtual void RegisterItemEvents(T item) {
88      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
89      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
90    }
91
92    protected override void OnContentChanged() {
93      base.OnContentChanged();
94
95      int selectedIndex = -1;
96      if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
97
98      itemsListView.Items.Clear();
99      itemListViewItemMapping.Clear();
100      RebuildImageList();
101      viewHost.Content = null;
102      if (Content != null) {
103        foreach (T item in Content)
104          AddListViewItem(CreateListViewItem(item));
105        AdjustListViewColumnSizes();
106        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
107          itemsListView.Items[selectedIndex].Selected = true;
108      }
109      SetEnabledStateOfControls();
110    }
111
112    protected override void SetEnabledStateOfControls() {
113      base.SetEnabledStateOfControls();
114      if (Content == null) {
115        addButton.Enabled = false;
116        moveUpButton.Enabled = false;
117        moveDownButton.Enabled = false;
118        removeButton.Enabled = false;
119        itemsListView.Enabled = false;
120        detailsGroupBox.Enabled = false;
121      } else {
122        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
123        moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
124                               itemsListView.SelectedIndices[0] != 0 &&
125                               !Content.IsReadOnly && !ReadOnly;
126        moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
127                                 itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
128                                 !Content.IsReadOnly && !ReadOnly;
129        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
130                               !Content.IsReadOnly && !ReadOnly;
131        itemsListView.Enabled = true;
132        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
133      }
134    }
135
136    protected virtual T CreateItem() {
137      if (typeSelectorDialog == null) {
138        typeSelectorDialog = new TypeSelectorDialog();
139        typeSelectorDialog.Caption = "Select Item";
140        typeSelectorDialog.TypeSelector.Caption = "Available Items";
141        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
142      }
143
144      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
145        try {
146          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
147        }
148        catch (Exception ex) {
149          ErrorHandling.ShowErrorDialog(this, ex);
150        }
151      }
152      return null;
153    }
154    protected virtual ListViewItem CreateListViewItem(T item) {
155      ListViewItem listViewItem = new ListViewItem();
156      if (item == null) {
157        listViewItem.Text = "null";
158        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
159        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
160      } else {
161        listViewItem.Text = item.ToString();
162        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
163        itemsListView.SmallImageList.Images.Add(item.ItemImage);
164        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
165        listViewItem.Tag = item;
166      }
167      return listViewItem;
168    }
169    protected virtual void AddListViewItem(ListViewItem listViewItem) {
170      if (listViewItem == null) throw new ArgumentNullException();
171      T item = (listViewItem.Tag as T);
172      itemsListView.Items.Add(listViewItem);
173      if (item != null) {
174        if (!itemListViewItemMapping.ContainsKey(item)) {
175          RegisterItemEvents(item);
176          itemListViewItemMapping.Add(item, new List<ListViewItem>());
177        }
178        itemListViewItemMapping[item].Add(listViewItem);
179      }
180    }
181    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
182      if (listViewItem == null) throw new ArgumentNullException();
183      T item = (listViewItem.Tag as T);
184      itemsListView.Items.Insert(index, listViewItem);
185      if (item != null) {
186        if (!itemListViewItemMapping.ContainsKey(item)) {
187          RegisterItemEvents(item);
188          itemListViewItemMapping.Add(item, new List<ListViewItem>());
189        }
190        itemListViewItemMapping[item].Add(listViewItem);
191      }
192    }
193    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
194      if (listViewItem == null) throw new ArgumentNullException();
195      T item = (listViewItem.Tag as T);
196      if (item != null) {
197        itemListViewItemMapping[item].Remove(listViewItem);
198        if (itemListViewItemMapping[item].Count == 0) {
199          itemListViewItemMapping.Remove(item);
200          DeregisterItemEvents(item);
201        }
202      }
203      listViewItem.Remove();
204    }
205    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
206      if (listViewItem == null) throw new ArgumentNullException();
207      T item = listViewItem.Tag as T;
208      int i = listViewItem.ImageIndex;
209      listViewItem.ImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
210      listViewItem.ImageIndex = -1;
211      listViewItem.ImageIndex = i;
212    }
213    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
214      if (listViewItem == null) throw new ArgumentNullException();
215      T item = listViewItem.Tag as T;
216      listViewItem.Text = item == null ? "null" : item.ToString();
217      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
218    }
219    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
220      if (item == null) {
221        List<ListViewItem> listViewItems = new List<ListViewItem>();
222        foreach (ListViewItem listViewItem in itemsListView.Items) {
223          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
224        }
225        return listViewItems;
226      } else {
227        List<ListViewItem> listViewItems = null;
228        itemListViewItemMapping.TryGetValue(item, out listViewItems);
229        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
230      }
231    }
232
233    #region ListView Events
234    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
235      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
236                             itemsListView.SelectedIndices[0] != 0 &&
237                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
238      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
239                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
240                               (Content != null) && !Content.IsReadOnly && !ReadOnly;
241      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
242                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
243      AdjustListViewColumnSizes();
244
245      if (showDetailsCheckBox.Checked) {
246        if (itemsListView.SelectedItems.Count == 1) {
247          T item = (T)itemsListView.SelectedItems[0].Tag;
248          detailsGroupBox.Enabled = true;
249          viewHost.Content = item;
250        } else {
251          viewHost.Content = null;
252          detailsGroupBox.Enabled = false;
253        }
254      }
255    }
256
257    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
258      if (e.KeyCode == Keys.Delete) {
259        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
260          foreach (ListViewItem item in itemsListView.SelectedItems)
261            Content.RemoveAt(item.Index);
262        }
263      }
264    }
265
266    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
267      if (itemsListView.SelectedItems.Count == 1) {
268        T item = itemsListView.SelectedItems[0].Tag as T;
269        if (item != null) {
270          IContentView view = MainFormManager.MainForm.ShowContent(item);
271          if (view != null) {
272            view.ReadOnly = ReadOnly;
273            view.Locked = Locked;
274          }
275        }
276      }
277    }
278
279    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
280      if (!Locked) {
281        List<T> items = new List<T>();
282        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
283          T item = listViewItem.Tag as T;
284          if (item != null) items.Add(item);
285        }
286
287        if (items.Count > 0) {
288          DataObject data = new DataObject();
289          if (items.Count == 1) data.SetData("HeuristicLab", items[0]);
290          else data.SetData("HeuristicLab", items);
291          if (Content.IsReadOnly || ReadOnly) {
292            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
293          } else {
294            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
295            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
296              foreach (ListViewItem listViewItem in itemsListView.SelectedItems.Cast<ListViewItem>().ToArray()) {
297                if (listViewItem.Tag != null) Content.RemoveAt(listViewItem.Index);
298              }
299            }
300          }
301        }
302      }
303    }
304    protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
305      validDragOperation = false;
306      if (e.Data.GetData("HeuristicLab") is T) {
307        validDragOperation = true;
308      } else if (e.Data.GetData("HeuristicLab") is IEnumerable) {
309        validDragOperation = true;
310        IEnumerable items = (IEnumerable)e.Data.GetData("HeuristicLab");
311        foreach (object item in items)
312          validDragOperation = validDragOperation && (item is T);
313      }
314      validDragOperation = validDragOperation && !Content.IsReadOnly && !ReadOnly;
315    }
316    protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
317      e.Effect = DragDropEffects.None;
318      if (validDragOperation) {
319        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
320        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
321        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
322        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
323        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
324      }
325    }
326    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
327      if (e.Effect != DragDropEffects.None) {
328        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
329        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
330
331        if (e.Data.GetData("HeuristicLab") is T) {
332          T item = (T)e.Data.GetData("HeuristicLab");
333          if (listViewItem != null) Content.Insert(listViewItem.Index, e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
334          else Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
335        } else if (e.Data.GetData("HeuristicLab") is IEnumerable) {
336          IEnumerable<T> items = ((IEnumerable)e.Data.GetData("HeuristicLab")).Cast<T>();
337          foreach (T item in items) {
338            if (listViewItem != null) Content.Insert(listViewItem.Index, e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
339            else Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
340          }
341        }
342      }
343    }
344    #endregion
345
346    #region Button Events
347    protected virtual void addButton_Click(object sender, EventArgs e) {
348      T item = CreateItem();
349      if (item != null)
350        Content.Add(item);
351    }
352    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
353      if (itemsListView.SelectedItems.Count == 1) {
354        int index = itemsListView.SelectedIndices[0];
355        T item = Content[index - 1];
356        Content[index - 1] = Content[index];
357        itemsListView.Items[index].Selected = false;
358        itemsListView.Items[index - 1].Selected = true;
359        Content[index] = item;
360      }
361    }
362    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
363      if (itemsListView.SelectedItems.Count == 1) {
364        int index = itemsListView.SelectedIndices[0];
365        T item = Content[index + 1];
366        Content[index + 1] = Content[index];
367        itemsListView.Items[index].Selected = false;
368        itemsListView.Items[index + 1].Selected = true;
369        Content[index] = item;
370      }
371    }
372    protected virtual void removeButton_Click(object sender, EventArgs e) {
373      if (itemsListView.SelectedItems.Count > 0) {
374        foreach (ListViewItem item in itemsListView.SelectedItems)
375          Content.RemoveAt(item.Index);
376      }
377    }
378    #endregion
379
380    #region CheckBox Events
381    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
382      if (showDetailsCheckBox.Checked) {
383        splitContainer.Panel2Collapsed = false;
384        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
385        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
386      } else {
387        splitContainer.Panel2Collapsed = true;
388        viewHost.Content = null;
389      }
390    }
391    #endregion
392
393    #region Content Events
394    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
395      if (InvokeRequired)
396        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
397      else {
398        foreach (IndexedItem<T> item in e.Items)
399          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
400        AdjustListViewColumnSizes();
401      }
402    }
403    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
404      if (InvokeRequired)
405        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
406      else {
407        List<ListViewItem> listViewItems = new List<ListViewItem>();
408        foreach (IndexedItem<T> item in e.Items)
409          listViewItems.Add(itemsListView.Items[item.Index]);
410        foreach (ListViewItem listViewItem in listViewItems)
411          RemoveListViewItem(listViewItem);
412        RebuildImageList();
413      }
414    }
415    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
416      if (InvokeRequired)
417        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
418      else {
419        int[] selected = new int[itemsListView.SelectedIndices.Count];
420        itemsListView.SelectedIndices.CopyTo(selected, 0);
421
422        List<ListViewItem> listViewItems = new List<ListViewItem>();
423        foreach (IndexedItem<T> item in e.OldItems)
424          listViewItems.Add(itemsListView.Items[item.Index]);
425        foreach (ListViewItem listViewItem in listViewItems)
426          RemoveListViewItem(listViewItem);
427        RebuildImageList();
428
429        foreach (IndexedItem<T> item in e.Items)
430          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
431        AdjustListViewColumnSizes();
432
433        for (int i = 0; i < selected.Length; i++)
434          itemsListView.Items[selected[i]].Selected = true;
435      }
436    }
437    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
438      if (InvokeRequired)
439        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
440      else {
441        foreach (IndexedItem<T> item in e.Items) {
442          ListViewItem listViewItem = itemsListView.Items[item.Index];
443          listViewItem.Tag = item.Value;
444          UpdateListViewItemImage(listViewItem);
445          UpdateListViewItemText(listViewItem);
446        }
447      }
448    }
449    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
450      if (InvokeRequired)
451        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
452      else {
453        List<ListViewItem> listViewItems = new List<ListViewItem>();
454        foreach (IndexedItem<T> item in e.OldItems)
455          listViewItems.Add(itemsListView.Items[item.Index]);
456        foreach (ListViewItem listViewItem in listViewItems)
457          RemoveListViewItem(listViewItem);
458        RebuildImageList();
459
460        foreach (IndexedItem<T> item in e.Items)
461          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
462        AdjustListViewColumnSizes();
463      }
464    }
465    #endregion
466
467    #region Item Events
468    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
469      if (InvokeRequired)
470        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
471      else {
472        T item = (T)sender;
473        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
474          UpdateListViewItemImage(listViewItem);
475      }
476    }
477    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
478      if (InvokeRequired)
479        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
480      else {
481        T item = (T)sender;
482        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
483          UpdateListViewItemText(listViewItem);
484      }
485    }
486    #endregion
487
488    #region Helpers
489    protected virtual void AdjustListViewColumnSizes() {
490      if (itemsListView.Items.Count > 0) {
491        for (int i = 0; i < itemsListView.Columns.Count; i++)
492          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
493      }
494    }
495    protected virtual void RebuildImageList() {
496      itemsListView.SmallImageList.Images.Clear();
497      foreach (ListViewItem listViewItem in itemsListView.Items) {
498        T item = listViewItem.Tag as T;
499        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
500        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
501      }
502    }
503    #endregion
504  }
505}
Note: See TracBrowser for help on using the repository browser.