Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed null reference exception in collection views (#973)

File size: 12.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Collections;
23using HeuristicLab.Core;
24using HeuristicLab.Core.Views;
25using HeuristicLab.MainForm;
26using HeuristicLab.MainForm.WindowsForms;
27using System.Windows.Forms;
28using System;
29using System.Drawing;
30using System.Collections.Generic;
31
32namespace HeuristicLab.Optimization.Views {
33  [View("RunCollection View")]
34  [Content(typeof(RunCollection), true)]
35  [Content(typeof(IItemCollection<IRun>), false)]
36  public partial class RunCollectionView : AsynchronousContentView {
37    public new IItemCollection<IRun> Content {
38      get { return (IItemCollection<IRun>)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public ListView ItemsListView {
43      get { return itemsListView; }
44    }
45
46    public RunCollectionView() {
47      InitializeComponent();
48      Caption = "Run Collection";
49    }
50
51    public RunCollectionView(IItemCollection<IRun> content)
52      : this() {
53      Content = content;
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
58      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
59      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
60      DeregisterRunEvents(Content);
61      base.DeregisterContentEvents();
62    }
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
66      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
67      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
68      RegisterRunEvents(Content);
69    }
70    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
71      foreach (IRun run in runs)
72        run.Changed += new EventHandler(run_Changed);
73    }
74    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
75      foreach (IRun run in runs)
76        run.Changed -= new EventHandler(run_Changed);
77    }
78
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      Caption = "Run Collection";
82      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
83      viewHost.Content = null;
84
85      if (Content != null) {
86        Caption += " (" + Content.GetType().Name + ")";
87        foreach (IRun item in Content)
88          AddListViewItem(CreateListViewItem(item));
89      }
90      SetEnabledStateOfControls();
91    }
92
93    private void run_Changed(object sender, EventArgs e) {
94      IRun run = (IRun)sender;
95      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
96        if (run.Visible) {
97          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
98          listViewItem.ForeColor = run.Color;
99        } else {
100          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
101          listViewItem.ForeColor = Color.LightGray;
102        }
103      }
104    }
105
106    protected override void OnReadOnlyChanged() {
107      base.OnReadOnlyChanged();
108      SetEnabledStateOfControls();
109    }
110    private void SetEnabledStateOfControls() {
111      if (Content == null) {
112        itemsListView.Enabled = false;
113        detailsGroupBox.Enabled = false;
114        viewHost.Enabled = false;
115        removeButton.Enabled = false;
116      } else {
117        itemsListView.Enabled = true;
118        detailsGroupBox.Enabled = true;
119        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
120        viewHost.Enabled = true;
121      }
122    }
123
124    protected virtual ListViewItem CreateListViewItem(IRun item) {
125      ListViewItem listViewItem = new ListViewItem();
126      listViewItem.Text = item.ToString();
127      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
128      itemsListView.SmallImageList.Images.Add(item.ItemImage);
129      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
130      listViewItem.Tag = item;
131      return listViewItem;
132    }
133    protected virtual void AddListViewItem(ListViewItem listViewItem) {
134      itemsListView.Items.Add(listViewItem);
135      ((IRun)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
136      ((IRun)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
137    }
138    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
139      ((IRun)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
140      ((IRun)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
141      listViewItem.Remove();
142      foreach (ListViewItem other in itemsListView.Items)
143        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
144      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
145    }
146    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
147      int i = listViewItem.ImageIndex;
148      listViewItem.ImageList.Images[i] = ((IRun)listViewItem.Tag).ItemImage;
149      listViewItem.ImageIndex = -1;
150      listViewItem.ImageIndex = i;
151    }
152    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
153      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
154        listViewItem.Text = listViewItem.Tag.ToString();
155    }
156    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
157      foreach (ListViewItem listViewItem in itemsListView.Items) {
158        if (((IRun)listViewItem.Tag) == item)
159          yield return listViewItem;
160      }
161    }
162
163    #region ListView Events
164    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
165      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
166      if (itemsListView.SelectedItems.Count == 1) {
167        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
168        detailsGroupBox.Enabled = true;
169        viewHost.ViewType = null;
170        viewHost.Content = item;
171      } else {
172        viewHost.Content = null;
173        detailsGroupBox.Enabled = false;
174      }
175    }
176    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
177      if (itemsListView.Columns.Count > 0)
178        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
179    }
180    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
181      if (e.KeyCode == Keys.Delete) {
182        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
183          foreach (ListViewItem item in itemsListView.SelectedItems)
184            Content.Remove((IRun)item.Tag);
185        }
186      }
187    }
188    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
189      if (itemsListView.SelectedItems.Count == 1) {
190        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
191        IContentView view = MainFormManager.CreateDefaultView(item);
192        if (view != null) {
193          view.ReadOnly = ReadOnly;
194          view.Locked = Locked;
195          view.Show();
196        }
197      }
198    }
199    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
200      if (!Locked) {
201        ListViewItem listViewItem = (ListViewItem)e.Item;
202        IRun item = (IRun)listViewItem.Tag;
203        DataObject data = new DataObject();
204        data.SetData("Type", item.GetType());
205        data.SetData("Value", item);
206        if (Content.IsReadOnly || ReadOnly) {
207          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
208        } else {
209          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
210          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
211            Content.Remove(item);
212        }
213      }
214    }
215    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
216      e.Effect = DragDropEffects.None;
217      Type type = e.Data.GetData("Type") as Type;
218      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
219        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
220        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
221        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
222        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
223        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
224      }
225    }
226    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
227      if (e.Effect != DragDropEffects.None) {
228        IRun item = e.Data.GetData("Value") as IRun;
229        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
230        Content.Add(item);
231      }
232    }
233    #endregion
234
235    #region Button Events
236    protected virtual void removeButton_Click(object sender, EventArgs e) {
237      if (itemsListView.SelectedItems.Count > 0) {
238        foreach (ListViewItem item in itemsListView.SelectedItems)
239          Content.Remove((IRun)item.Tag);
240        itemsListView.SelectedItems.Clear();
241      }
242    }
243    #endregion
244
245    #region Content Events
246    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
247      if (InvokeRequired)
248        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
249      else {
250        RegisterRunEvents(e.Items);
251        foreach (IRun item in e.Items)
252          AddListViewItem(CreateListViewItem(item));
253      }
254    }
255    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
256      if (InvokeRequired)
257        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
258      else {
259        DeregisterRunEvents(e.Items);
260        foreach (IRun item in e.Items) {
261          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
262            RemoveListViewItem(listViewItem);
263            break;
264          }
265        }
266      }
267    }
268    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
269      if (InvokeRequired)
270        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
271      else {
272        DeregisterRunEvents(e.OldItems);
273        foreach (IRun item in e.OldItems) {
274          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
275            RemoveListViewItem(listViewItem);
276            break;
277          }
278        }
279        RegisterRunEvents(e.Items);
280        foreach (IRun item in e.Items)
281          AddListViewItem(CreateListViewItem(item));
282      }
283    }
284    #endregion
285
286    #region Item Events
287    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
288      if (InvokeRequired)
289        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
290      else {
291        IRun item = (IRun)sender;
292        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
293          UpdateListViewItemImage(listViewItem);
294      }
295    }
296    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
297      if (InvokeRequired)
298        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
299      else {
300        IRun item = (IRun)sender;
301        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
302          UpdateListViewItemText(listViewItem);
303      }
304    }
305    #endregion
306  }
307}
308
Note: See TracBrowser for help on using the repository browser.