Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs @ 5711

Last change on this file since 5711 was 5702, checked in by swagner, 14 years ago

Implemented dragging of multiple items in RunCollectionView (#1112)

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