Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs @ 5991

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

Fixed NullReferenceException in collection views when updating the item image of a hidden item (#1377)

File size: 20.2 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3260]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
[4068]22using System;
[5744]23using System.Collections;
[4068]24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Windows.Forms;
[3274]28using HeuristicLab.Collections;
[3393]29using HeuristicLab.Core;
[3260]30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
[3277]32using HeuristicLab.MainForm.WindowsForms;
[3260]33
34namespace HeuristicLab.Optimization.Views {
35  [View("RunCollection View")]
36  [Content(typeof(RunCollection), true)]
[3393]37  [Content(typeof(IItemCollection<IRun>), false)]
[4883]38  public sealed partial class RunCollectionView : ItemView {
[5237]39    private Dictionary<IRun, List<ListViewItem>> itemListViewItemMapping;
[5744]40    private bool validDragOperation;
[4883]41
[3393]42    public new IItemCollection<IRun> Content {
43      get { return (IItemCollection<IRun>)base.Content; }
[3277]44      set { base.Content = value; }
45    }
46
[3614]47    public RunCollection RunCollection {
48      get { return Content as RunCollection; }
49    }
50
[3277]51    public ListView ItemsListView {
52      get { return itemsListView; }
53    }
54
[3260]55    public RunCollectionView() {
56      InitializeComponent();
[3505]57      itemsGroupBox.Text = "Runs";
[5237]58      itemListViewItemMapping = new Dictionary<IRun, List<ListViewItem>>();
[3260]59    }
[3277]60
61    protected override void DeregisterContentEvents() {
[3280]62      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
63      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
64      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[5237]65      foreach (IRun run in itemListViewItemMapping.Keys) {
66        DeregisterItemEvents(run);
67      }
[3277]68      base.DeregisterContentEvents();
[3260]69    }
[3277]70    protected override void RegisterContentEvents() {
71      base.RegisterContentEvents();
[3280]72      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
73      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
74      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[3277]75    }
[5237]76    private void DeregisterItemEvents(IRun item) {
77      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
78      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
79      item.Changed -= new EventHandler(Item_Changed);
[3449]80    }
[5237]81    private void RegisterItemEvents(IRun item) {
82      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
83      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
84      item.Changed += new EventHandler(Item_Changed);
[3449]85    }
[3277]86
[3507]87    protected override void OnInitialized(EventArgs e) {
88      base.OnInitialized(e);
89      var viewTypes = MainFormManager.GetViewTypes(typeof(RunCollection), true);
90      foreach (Type viewType in viewTypes.OrderBy(x => ViewAttribute.GetViewName(x))) {
91        if ((viewType != typeof(ItemCollectionView<IRun>)) && (viewType != typeof(ViewHost))) {
92          ToolStripMenuItem menuItem = new ToolStripMenuItem();
93          menuItem.Text = ViewAttribute.GetViewName(viewType);
94          menuItem.Tag = viewType;
95          menuItem.Click += new EventHandler(menuItem_Click);
96          analyzeRunsToolStripDropDownButton.DropDownItems.Add(menuItem);
97        }
98      }
99    }
100
[3277]101    protected override void OnContentChanged() {
102      base.OnContentChanged();
[3775]103
104      string selectedName = null;
105      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null))
106        selectedName = ((IRun)itemsListView.SelectedItems[0].Tag).Name;
107
[5237]108      itemsListView.Items.Clear();
109      itemListViewItemMapping.Clear();
110      RebuildImageList();
[3277]111      viewHost.Content = null;
112
113      if (Content != null) {
[3614]114        if (RunCollection != null) {
[3709]115          if (!tabControl.TabPages.Contains(constraintPage))
116            tabControl.TabPages.Add(constraintPage);
[3614]117          runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
118          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
119        }
120        foreach (IRun item in Content) {
[3775]121          ListViewItem listViewItem = CreateListViewItem(item);
122          AddListViewItem(listViewItem);
123          if ((selectedName != null) && item.Name.Equals(selectedName))
124            listViewItem.Selected = true;
[3614]125        }
[4200]126        AdjustListViewColumnSizes();
[3709]127      } else {
128        runCollectionConstraintCollectionView.Content = null;
129        if (tabControl.TabPages.Contains(constraintPage))
130          tabControl.TabPages.Remove(constraintPage);
[3277]131      }
132    }
133
[3904]134    protected override void SetEnabledStateOfControls() {
135      base.SetEnabledStateOfControls();
[3350]136      if (Content == null) {
[3507]137        analyzeRunsToolStripDropDownButton.Enabled = false;
[3614]138        runCollectionConstraintCollectionView.ReadOnly = true;
[3350]139        itemsListView.Enabled = false;
140        detailsGroupBox.Enabled = false;
141        viewHost.Enabled = false;
142        removeButton.Enabled = false;
[3716]143        clearButton.Enabled = false;
[3350]144      } else {
[3507]145        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3614]146        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3350]147        itemsListView.Enabled = true;
[4099]148        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[3435]149        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3716]150        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3350]151        viewHost.Enabled = true;
152      }
153    }
154
[4883]155    private ListViewItem CreateListViewItem(IRun item) {
[3277]156      ListViewItem listViewItem = new ListViewItem();
[5237]157      if (item == null) {
158        listViewItem.Text = "null";
[5287]159        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
[5237]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;
[4200]167
[5237]168        if (item.Visible) {
169          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
170          listViewItem.ForeColor = item.Color;
171        } else {
172          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
173          listViewItem.ForeColor = Color.LightGray;
174        }
[4200]175      }
[3277]176      return listViewItem;
177    }
[4883]178    private void AddListViewItem(ListViewItem listViewItem) {
[5371]179      if (listViewItem == null) throw new ArgumentNullException();
[3277]180      itemsListView.Items.Add(listViewItem);
[4883]181      IRun run = listViewItem.Tag as IRun;
182      if (run != null) {
[5237]183        if (!itemListViewItemMapping.ContainsKey(run)) {
184          itemListViewItemMapping.Add(run, new List<ListViewItem>());
185          RegisterItemEvents(run);
186        }
187        itemListViewItemMapping[run].Add(listViewItem);
[4883]188      }
[3277]189    }
[4883]190    private void RemoveListViewItem(ListViewItem listViewItem) {
[5371]191      if (listViewItem == null) throw new ArgumentNullException();
[4883]192      IRun run = listViewItem.Tag as IRun;
193      if (run != null) {
[5237]194        itemListViewItemMapping[run].Remove(listViewItem);
195        if (itemListViewItemMapping[run].Count == 0) {
196          DeregisterItemEvents(run);
197          itemListViewItemMapping.Remove(run);
[4883]198        }
199      }
[3277]200      listViewItem.Remove();
201    }
[4883]202    private void UpdateListViewItemImage(ListViewItem listViewItem) {
[5371]203      if (listViewItem == null) throw new ArgumentNullException();
[5237]204      IRun item = listViewItem.Tag as IRun;
[3341]205      int i = listViewItem.ImageIndex;
[5839]206      itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
[3341]207      listViewItem.ImageIndex = -1;
208      listViewItem.ImageIndex = i;
209    }
[4883]210    private void UpdateListViewItemText(ListViewItem listViewItem) {
[5371]211      if (listViewItem == null) throw new ArgumentNullException();
[5237]212      IRun item = listViewItem.Tag as IRun;
213      listViewItem.Text = item == null ? "null" : item.ToString();
214      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
[3277]215    }
[5237]216    private IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
217      if (item == null) {
218        List<ListViewItem> listViewItems = new List<ListViewItem>();
219        foreach (ListViewItem listViewItem in itemsListView.Items) {
220          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
221        }
222        return listViewItems;
223      } else {
[5302]224        List<ListViewItem> listViewItems = null;
225        itemListViewItemMapping.TryGetValue(item, out listViewItems);
226        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
[5237]227      }
[3277]228    }
229
230    #region ListView Events
[4883]231    private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[3456]232      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
[3829]233      AdjustListViewColumnSizes();
[4096]234      if (showDetailsCheckBox.Checked) {
235        if (itemsListView.SelectedItems.Count == 1) {
236          IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
237          detailsGroupBox.Enabled = true;
238          viewHost.Content = item;
239        } else {
240          viewHost.Content = null;
241          detailsGroupBox.Enabled = false;
242        }
[3277]243      }
244    }
[4883]245    private void itemsListView_KeyDown(object sender, KeyEventArgs e) {
[3277]246      if (e.KeyCode == Keys.Delete) {
[3435]247        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[3277]248          foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]249            Content.Remove((IRun)item.Tag);
[3277]250        }
251      }
252    }
[4883]253    private void itemsListView_DoubleClick(object sender, EventArgs e) {
[3277]254      if (itemsListView.SelectedItems.Count == 1) {
[5237]255        IRun item = itemsListView.SelectedItems[0].Tag as IRun;
256        if (item != null) {
257          IContentView view = MainFormManager.MainForm.ShowContent(item);
258          if (view != null) {
259            view.ReadOnly = ReadOnly;
260            view.Locked = Locked;
261          }
[3416]262        }
[3277]263      }
264    }
[4883]265    private void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]266      if (!Locked) {
[5702]267        List<IRun> items = new List<IRun>();
268        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
269          IRun item = listViewItem.Tag as IRun;
270          if (item != null) items.Add(item);
271        }
272
273        if (items.Count > 0) {
[5237]274          DataObject data = new DataObject();
[5837]275          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
276          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
[5237]277          if (Content.IsReadOnly || ReadOnly) {
278            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
279          } else {
280            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
[5744]281            if (result.HasFlag(DragDropEffects.Move)) {
[5702]282              foreach (IRun item in items) Content.Remove(item);
283            }
[5237]284          }
[3432]285        }
[3277]286      }
287    }
[5744]288    private void itemsListView_DragEnter(object sender, DragEventArgs e) {
289      validDragOperation = false;
[5837]290      if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun)) {
[5744]291        validDragOperation = true;
[5837]292      } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
[5744]293        validDragOperation = true;
[5837]294        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]295        foreach (object item in items)
296          validDragOperation = validDragOperation && (item is IRun);
297      }
298    }
299    private void itemsListView_DragOver(object sender, DragEventArgs e) {
[3277]300      e.Effect = DragDropEffects.None;
[5744]301      if (validDragOperation) {
[3694]302        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3277]303        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]304        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
305        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
306        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[3277]307      }
308    }
[4883]309    private void itemsListView_DragDrop(object sender, DragEventArgs e) {
[3277]310      if (e.Effect != DragDropEffects.None) {
[5837]311        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun) {
312          IRun item = (IRun)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]313          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item);
[5837]314        } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
315          IEnumerable<IRun> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<IRun>();
[5744]316          foreach (IRun item in items)
317            Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item);
[5702]318        }
[3277]319      }
320    }
321    #endregion
322
323    #region Button Events
[4883]324    private void menuItem_Click(object sender, EventArgs e) {
[3507]325      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
[3796]326      Type viewType = (Type)menuItem.Tag;
327      IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
[3507]328      if (view != null) {
329        view.Locked = Locked;
330        view.ReadOnly = ReadOnly;
331      }
332    }
[4883]333    private void removeButton_Click(object sender, EventArgs e) {
[3277]334      if (itemsListView.SelectedItems.Count > 0) {
335        foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]336          Content.Remove((IRun)item.Tag);
[3277]337        itemsListView.SelectedItems.Clear();
338      }
339    }
[4883]340    private void clearButton_Click(object sender, EventArgs e) {
[3716]341      Content.Clear();
342    }
[3277]343    #endregion
344
[4096]345    #region CheckBox Events
[4883]346    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
[4096]347      if (showDetailsCheckBox.Checked) {
348        splitContainer.Panel2Collapsed = false;
349        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
350        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (IRun)itemsListView.SelectedItems[0].Tag : null;
351      } else {
352        splitContainer.Panel2Collapsed = true;
353        viewHost.Content = null;
354      }
355    }
356    #endregion
357
[3277]358    #region Content Events
[4883]359    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]360      if (InvokeRequired)
[3280]361        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
[3449]362      else {
[4200]363        foreach (IRun item in e.Items)
[3277]364          AddListViewItem(CreateListViewItem(item));
[4200]365
366        AdjustListViewColumnSizes();
[3507]367        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]368        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]369        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3449]370      }
[3277]371    }
[4883]372    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]373      if (InvokeRequired)
[3280]374        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
[3277]375      else {
[3280]376        foreach (IRun item in e.Items) {
[4203]377          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]378          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
379          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]380        }
[5237]381        RebuildImageList();
[3507]382        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]383        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]384        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]385      }
386    }
[4883]387    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]388      if (InvokeRequired)
[3280]389        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
[3277]390      else {
[3280]391        foreach (IRun item in e.OldItems) {
[5237]392          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]393          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
394          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]395        }
[5237]396        RebuildImageList();
[4200]397        foreach (IRun item in e.Items)
[3277]398          AddListViewItem(CreateListViewItem(item));
[4200]399
400        AdjustListViewColumnSizes();
[3507]401        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]402        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]403        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]404      }
405    }
406    #endregion
407
408    #region Item Events
[4883]409    private void Item_ItemImageChanged(object sender, EventArgs e) {
[3341]410      if (InvokeRequired)
411        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
412      else {
413        IRun item = (IRun)sender;
414        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
415          UpdateListViewItemImage(listViewItem);
416      }
417    }
[4883]418    private void Item_ToStringChanged(object sender, EventArgs e) {
[3277]419      if (InvokeRequired)
420        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
421      else {
[3280]422        IRun item = (IRun)sender;
[3277]423        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3341]424          UpdateListViewItemText(listViewItem);
[3829]425        AdjustListViewColumnSizes();
[3277]426      }
427    }
[5237]428    private void Item_Changed(object sender, EventArgs e) {
[3632]429      if (InvokeRequired)
[5237]430        this.Invoke(new EventHandler(Item_Changed), sender, e);
[3632]431      else {
432        IRun run = (IRun)sender;
433        UpdateRun(run);
434      }
[3614]435    }
436
[4883]437    private void UpdateRun(IRun run) {
[3507]438      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
439        if (run.Visible) {
440          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
441          listViewItem.ForeColor = run.Color;
442        } else {
443          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
444          listViewItem.ForeColor = Color.LightGray;
445        }
446      }
447    }
[3277]448    #endregion
[3716]449
[3829]450    #region Helpers
[4883]451    private void AdjustListViewColumnSizes() {
[3829]452      if (itemsListView.Items.Count > 0) {
453        for (int i = 0; i < itemsListView.Columns.Count; i++) {
454          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
455        }
456      }
457    }
[5237]458    private void RebuildImageList() {
459      itemsListView.SmallImageList.Images.Clear();
[5239]460      foreach (ListViewItem listViewItem in itemsListView.Items) {
461        IRun item = listViewItem.Tag as IRun;
[5287]462        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]463        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5237]464      }
465    }
[3829]466    #endregion
[3260]467  }
468}
Note: See TracBrowser for help on using the repository browser.