Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionView.cs @ 15115

Last change on this file since 15115 was 15115, checked in by mkommend, 7 years ago

#2758: Merged r14793 into stable.

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