Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core.Views/3.3/ItemArrayView.cs @ 17213

Last change on this file since 17213 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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