Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs @ 6955

Last change on this file since 6955 was 6527, checked in by swagner, 13 years ago

Corrected cloning of multiple dropped items (#1564)

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