Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemListView.cs @ 12011

Last change on this file since 12011 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 22.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Common;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Core.Views {
35  [View("ItemList View")]
36  [Content(typeof(ItemList<>), true)]
37  [Content(typeof(IItemList<>), false)]
38  public partial class ItemListView<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 IItemList<T> Content {
44      get { return (IItemList<T>)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public ListView ItemsListView {
49      get { return itemsListView; }
50    }
51
52    public ItemListView() {
53      InitializeComponent();
54      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
55    }
56
57    protected override void Dispose(bool disposing) {
58      if (disposing) {
59        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
60        if (components != null) components.Dispose();
61      }
62      base.Dispose(disposing);
63    }
64
65    protected override void DeregisterContentEvents() {
66      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
67      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
68      Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
69      Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
70      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
71      foreach (T item in itemListViewItemMapping.Keys) {
72        DeregisterItemEvents(item);
73      }
74      base.DeregisterContentEvents();
75    }
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
79      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
80      Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
81      Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
82      Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
83    }
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    }
92
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
95
96      int selectedIndex = -1;
97      if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
98
99      itemsListView.Items.Clear();
100      itemListViewItemMapping.Clear();
101      RebuildImageList();
102      viewHost.Content = null;
103      if (Content != null) {
104        foreach (T item in Content)
105          AddListViewItem(CreateListViewItem(item));
106        AdjustListViewColumnSizes();
107        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
108          itemsListView.Items[selectedIndex].Selected = true;
109      }
110      SetEnabledStateOfControls();
111    }
112
113    protected override void SetEnabledStateOfControls() {
114      base.SetEnabledStateOfControls();
115      if (Content == null) {
116        addButton.Enabled = false;
117        moveUpButton.Enabled = false;
118        moveDownButton.Enabled = false;
119        removeButton.Enabled = false;
120        itemsListView.Enabled = false;
121        detailsGroupBox.Enabled = false;
122      } else {
123        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
124        moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
125                               itemsListView.SelectedIndices[0] != 0 &&
126                               !Content.IsReadOnly && !ReadOnly;
127        moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
128                                 itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
129                                 !Content.IsReadOnly && !ReadOnly;
130        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
131                               !Content.IsReadOnly && !ReadOnly;
132        itemsListView.Enabled = true;
133        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
134      }
135    }
136
137    protected virtual T CreateItem() {
138      if (typeSelectorDialog == null) {
139        typeSelectorDialog = new TypeSelectorDialog();
140        typeSelectorDialog.Caption = "Select Item";
141        typeSelectorDialog.TypeSelector.Caption = "Available Items";
142        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
143      }
144
145      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
146        try {
147          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
148        }
149        catch (Exception ex) {
150          ErrorHandling.ShowErrorDialog(this, ex);
151        }
152      }
153      return null;
154    }
155    protected virtual ListViewItem CreateListViewItem(T item) {
156      ListViewItem listViewItem = new ListViewItem();
157      if (item == null) {
158        listViewItem.Text = "null";
159        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
160        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
161      } else {
162        listViewItem.Text = item.ToString();
163        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
164        itemsListView.SmallImageList.Images.Add(item.ItemImage);
165        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
166        listViewItem.Tag = item;
167      }
168      return listViewItem;
169    }
170    protected virtual void AddListViewItem(ListViewItem listViewItem) {
171      if (listViewItem == null) throw new ArgumentNullException();
172      T item = (listViewItem.Tag as T);
173      itemsListView.Items.Add(listViewItem);
174      if (item != null) {
175        if (!itemListViewItemMapping.ContainsKey(item)) {
176          RegisterItemEvents(item);
177          itemListViewItemMapping.Add(item, new List<ListViewItem>());
178        }
179        itemListViewItemMapping[item].Add(listViewItem);
180      }
181    }
182    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
183      if (listViewItem == null) throw new ArgumentNullException();
184      T item = (listViewItem.Tag as T);
185      itemsListView.Items.Insert(index, listViewItem);
186      if (item != null) {
187        if (!itemListViewItemMapping.ContainsKey(item)) {
188          RegisterItemEvents(item);
189          itemListViewItemMapping.Add(item, new List<ListViewItem>());
190        }
191        itemListViewItemMapping[item].Add(listViewItem);
192      }
193    }
194    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
195      if (listViewItem == null) throw new ArgumentNullException();
196      T item = (listViewItem.Tag as T);
197      if (item != null) {
198        itemListViewItemMapping[item].Remove(listViewItem);
199        if (itemListViewItemMapping[item].Count == 0) {
200          itemListViewItemMapping.Remove(item);
201          DeregisterItemEvents(item);
202        }
203      }
204      listViewItem.Remove();
205    }
206    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
207      if (listViewItem == null) throw new ArgumentNullException();
208      T item = listViewItem.Tag as T;
209      int i = listViewItem.ImageIndex;
210      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
211      listViewItem.ImageIndex = -1;
212      listViewItem.ImageIndex = i;
213    }
214    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
215      if (listViewItem == null) throw new ArgumentNullException();
216      T item = listViewItem.Tag as T;
217      listViewItem.Text = item == null ? "null" : item.ToString();
218      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
219    }
220    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
221      if (item == null) {
222        List<ListViewItem> listViewItems = new List<ListViewItem>();
223        foreach (ListViewItem listViewItem in itemsListView.Items) {
224          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
225        }
226        return listViewItems;
227      } else {
228        List<ListViewItem> listViewItems = null;
229        itemListViewItemMapping.TryGetValue(item, out listViewItems);
230        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
231      }
232    }
233
234    #region ListView Events
235    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
236      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
237                             itemsListView.SelectedIndices[0] != 0 &&
238                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
239      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
240                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
241                               (Content != null) && !Content.IsReadOnly && !ReadOnly;
242      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
243                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
244      AdjustListViewColumnSizes();
245
246      if (showDetailsCheckBox.Checked) {
247        if (itemsListView.SelectedItems.Count == 1) {
248          T item = (T)itemsListView.SelectedItems[0].Tag;
249          detailsGroupBox.Enabled = true;
250          viewHost.Content = item;
251        } else {
252          viewHost.Content = null;
253          detailsGroupBox.Enabled = false;
254        }
255      }
256    }
257
258    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
259      if (e.KeyCode == Keys.Delete) {
260        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
261          foreach (ListViewItem item in itemsListView.SelectedItems)
262            Content.RemoveAt(item.Index);
263        }
264      }
265    }
266
267    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
268      if (itemsListView.SelectedItems.Count == 1) {
269        T item = itemsListView.SelectedItems[0].Tag as T;
270        if (item != null) {
271          IContentView view = MainFormManager.MainForm.ShowContent(item);
272          if (view != null) {
273            view.ReadOnly = ReadOnly;
274            view.Locked = Locked;
275          }
276        }
277      }
278    }
279
280    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
281      if (!Locked) {
282        List<T> items = new List<T>();
283        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
284          T item = listViewItem.Tag as T;
285          if (item != null) items.Add(item);
286        }
287
288        if (items.Count > 0) {
289          DataObject data = new DataObject();
290          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
291          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
292          if (Content.IsReadOnly || ReadOnly) {
293            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
294          } else {
295            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
296            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
297              foreach (ListViewItem listViewItem in itemsListView.SelectedItems.Cast<ListViewItem>().ToArray()) {
298                if (listViewItem.Tag != null) Content.RemoveAt(listViewItem.Index);
299              }
300            }
301          }
302        }
303      }
304    }
305    protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
306      validDragOperation = false;
307      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
308        validDragOperation = true;
309      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
310        validDragOperation = true;
311        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
312        foreach (object item in items)
313          validDragOperation = validDragOperation && (item is T);
314      }
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.Common.Constants.DragDropDataFormat) is T) {
332          T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
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.Common.Constants.DragDropDataFormat) is IEnumerable) {
336          IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
337          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
338            Cloner cloner = new Cloner();
339            items = items.Select(x => cloner.Clone(x));
340          }
341          foreach (T item in items) {
342            if (listViewItem != null) Content.Insert(listViewItem.Index, item);
343            else Content.Add(item);
344          }
345        }
346      }
347    }
348    #endregion
349
350    #region Button Events
351    protected virtual void addButton_Click(object sender, EventArgs e) {
352      T item = CreateItem();
353      if (item != null)
354        Content.Add(item);
355    }
356    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
357      if (itemsListView.SelectedItems.Count == 1) {
358        int index = itemsListView.SelectedIndices[0];
359        Content.Reverse(index - 1, 2);
360        itemsListView.Items[index].Selected = false;
361        itemsListView.Items[index - 1].Selected = true;
362      }
363    }
364    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
365      if (itemsListView.SelectedItems.Count == 1) {
366        int index = itemsListView.SelectedIndices[0];
367        Content.Reverse(index, 2);
368        itemsListView.Items[index].Selected = false;
369        itemsListView.Items[index + 1].Selected = true;
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          if (listViewItem.Tag != null)
444            itemListViewItemMapping[(T)listViewItem.Tag].Remove(listViewItem);
445          listViewItem.Tag = item.Value;
446          if (listViewItem.Tag != null)
447            itemListViewItemMapping[item.Value].Add(listViewItem);
448          UpdateListViewItemImage(listViewItem);
449          UpdateListViewItemText(listViewItem);
450        }
451      }
452    }
453    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
454      if (InvokeRequired)
455        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
456      else {
457        List<ListViewItem> listViewItems = new List<ListViewItem>();
458        foreach (IndexedItem<T> item in e.OldItems)
459          listViewItems.Add(itemsListView.Items[item.Index]);
460        foreach (ListViewItem listViewItem in listViewItems)
461          RemoveListViewItem(listViewItem);
462        RebuildImageList();
463
464        foreach (IndexedItem<T> item in e.Items)
465          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
466        AdjustListViewColumnSizes();
467      }
468    }
469    #endregion
470
471    #region Item Events
472    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
473      if (InvokeRequired)
474        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
475      else {
476        T item = (T)sender;
477        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
478          UpdateListViewItemImage(listViewItem);
479      }
480    }
481    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
482      if (InvokeRequired)
483        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
484      else {
485        T item = (T)sender;
486        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
487          UpdateListViewItemText(listViewItem);
488        AdjustListViewColumnSizes();
489      }
490    }
491    #endregion
492
493    #region Helpers
494    protected virtual void AdjustListViewColumnSizes() {
495      if (itemsListView.Items.Count > 0) {
496        for (int i = 0; i < itemsListView.Columns.Count; i++)
497          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
498      }
499    }
500    protected virtual void RebuildImageList() {
501      itemsListView.SmallImageList.Images.Clear();
502      foreach (ListViewItem listViewItem in itemsListView.Items) {
503        T item = listViewItem.Tag as T;
504        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
505        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
506      }
507    }
508    #endregion
509  }
510}
Note: See TracBrowser for help on using the repository browser.