Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionView.cs @ 11896

Last change on this file since 11896 was 11594, checked in by jkarder, 10 years ago

#2116: merged r10041-r11593 from trunk into branch

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