Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs @ 5681

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

Updated year of copyrights (#1406)

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