Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionView.cs @ 11185

Last change on this file since 11185 was 11185, checked in by pfleck, 10 years ago

#2208 merged trunk and updated version info

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