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
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3260]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
[4068]22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
[3274]27using HeuristicLab.Collections;
[3393]28using HeuristicLab.Core;
[3260]29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
[3277]31using HeuristicLab.MainForm.WindowsForms;
[3260]32
33namespace HeuristicLab.Optimization.Views {
34  [View("RunCollection View")]
35  [Content(typeof(RunCollection), true)]
[3393]36  [Content(typeof(IItemCollection<IRun>), false)]
[4883]37  public sealed partial class RunCollectionView : ItemView {
[5237]38    private Dictionary<IRun, List<ListViewItem>> itemListViewItemMapping;
[4883]39
[3393]40    public new IItemCollection<IRun> Content {
41      get { return (IItemCollection<IRun>)base.Content; }
[3277]42      set { base.Content = value; }
43    }
44
[3614]45    public RunCollection RunCollection {
46      get { return Content as RunCollection; }
47    }
48
[3277]49    public ListView ItemsListView {
50      get { return itemsListView; }
51    }
52
[3260]53    public RunCollectionView() {
54      InitializeComponent();
[3505]55      itemsGroupBox.Text = "Runs";
[5237]56      itemListViewItemMapping = new Dictionary<IRun, List<ListViewItem>>();
[3260]57    }
[3277]58
59    protected override void DeregisterContentEvents() {
[3280]60      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
61      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
62      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[5237]63      foreach (IRun run in itemListViewItemMapping.Keys) {
64        DeregisterItemEvents(run);
65      }
[3277]66      base.DeregisterContentEvents();
[3260]67    }
[3277]68    protected override void RegisterContentEvents() {
69      base.RegisterContentEvents();
[3280]70      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
71      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
72      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[3277]73    }
[5237]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);
[3449]78    }
[5237]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);
[3449]83    }
[3277]84
[3507]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
[3277]99    protected override void OnContentChanged() {
100      base.OnContentChanged();
[3775]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
[5237]106      itemsListView.Items.Clear();
107      itemListViewItemMapping.Clear();
108      RebuildImageList();
[3277]109      viewHost.Content = null;
110
111      if (Content != null) {
[3614]112        if (RunCollection != null) {
[3709]113          if (!tabControl.TabPages.Contains(constraintPage))
114            tabControl.TabPages.Add(constraintPage);
[3614]115          runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
116          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
117        }
118        foreach (IRun item in Content) {
[3775]119          ListViewItem listViewItem = CreateListViewItem(item);
120          AddListViewItem(listViewItem);
121          if ((selectedName != null) && item.Name.Equals(selectedName))
122            listViewItem.Selected = true;
[3614]123        }
[4200]124        AdjustListViewColumnSizes();
[3709]125      } else {
126        runCollectionConstraintCollectionView.Content = null;
127        if (tabControl.TabPages.Contains(constraintPage))
128          tabControl.TabPages.Remove(constraintPage);
[3277]129      }
130    }
131
[3904]132    protected override void SetEnabledStateOfControls() {
133      base.SetEnabledStateOfControls();
[3350]134      if (Content == null) {
[3507]135        analyzeRunsToolStripDropDownButton.Enabled = false;
[3614]136        runCollectionConstraintCollectionView.ReadOnly = true;
[3350]137        itemsListView.Enabled = false;
138        detailsGroupBox.Enabled = false;
139        viewHost.Enabled = false;
140        removeButton.Enabled = false;
[3716]141        clearButton.Enabled = false;
[3350]142      } else {
[3507]143        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3614]144        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3350]145        itemsListView.Enabled = true;
[4099]146        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[3435]147        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3716]148        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3350]149        viewHost.Enabled = true;
150      }
151    }
152
[4883]153    private ListViewItem CreateListViewItem(IRun item) {
[3277]154      ListViewItem listViewItem = new ListViewItem();
[5237]155      if (item == null) {
156        listViewItem.Text = "null";
[5287]157        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
[5237]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;
[4200]165
[5237]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        }
[4200]173      }
[3277]174      return listViewItem;
175    }
[4883]176    private void AddListViewItem(ListViewItem listViewItem) {
[5371]177      if (listViewItem == null) throw new ArgumentNullException();
[3277]178      itemsListView.Items.Add(listViewItem);
[4883]179      IRun run = listViewItem.Tag as IRun;
180      if (run != null) {
[5237]181        if (!itemListViewItemMapping.ContainsKey(run)) {
182          itemListViewItemMapping.Add(run, new List<ListViewItem>());
183          RegisterItemEvents(run);
184        }
185        itemListViewItemMapping[run].Add(listViewItem);
[4883]186      }
[3277]187    }
[4883]188    private void RemoveListViewItem(ListViewItem listViewItem) {
[5371]189      if (listViewItem == null) throw new ArgumentNullException();
[4883]190      IRun run = listViewItem.Tag as IRun;
191      if (run != null) {
[5237]192        itemListViewItemMapping[run].Remove(listViewItem);
193        if (itemListViewItemMapping[run].Count == 0) {
194          DeregisterItemEvents(run);
195          itemListViewItemMapping.Remove(run);
[4883]196        }
197      }
[3277]198      listViewItem.Remove();
199    }
[4883]200    private void UpdateListViewItemImage(ListViewItem listViewItem) {
[5371]201      if (listViewItem == null) throw new ArgumentNullException();
[5237]202      IRun item = listViewItem.Tag as IRun;
[3341]203      int i = listViewItem.ImageIndex;
[5287]204      listViewItem.ImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
[3341]205      listViewItem.ImageIndex = -1;
206      listViewItem.ImageIndex = i;
207    }
[4883]208    private void UpdateListViewItemText(ListViewItem listViewItem) {
[5371]209      if (listViewItem == null) throw new ArgumentNullException();
[5237]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;
[3277]213    }
[5237]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 {
[5302]222        List<ListViewItem> listViewItems = null;
223        itemListViewItemMapping.TryGetValue(item, out listViewItems);
224        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
[5237]225      }
[3277]226    }
227
228    #region ListView Events
[4883]229    private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[3456]230      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
[3829]231      AdjustListViewColumnSizes();
[4096]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        }
[3277]241      }
242    }
[4883]243    private void itemsListView_KeyDown(object sender, KeyEventArgs e) {
[3277]244      if (e.KeyCode == Keys.Delete) {
[3435]245        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[3277]246          foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]247            Content.Remove((IRun)item.Tag);
[3277]248        }
249      }
250    }
[4883]251    private void itemsListView_DoubleClick(object sender, EventArgs e) {
[3277]252      if (itemsListView.SelectedItems.Count == 1) {
[5237]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          }
[3416]260        }
[3277]261      }
262    }
[4883]263    private void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]264      if (!Locked) {
[5702]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) {
[5237]272          DataObject data = new DataObject();
[5702]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          }
[5237]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);
[5702]284            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
285              foreach (IRun item in items) Content.Remove(item);
286            }
[5237]287          }
[3432]288        }
[3277]289      }
290    }
[4883]291    private void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
[3277]292      e.Effect = DragDropEffects.None;
293      Type type = e.Data.GetData("Type") as Type;
[5702]294      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type) || typeof(IEnumerable<IRun>).IsAssignableFrom(type))) {
[3694]295        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3277]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;
[3526]299        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3277]300      }
301    }
[4883]302    private void itemsListView_DragDrop(object sender, DragEventArgs e) {
[3277]303      if (e.Effect != DragDropEffects.None) {
[5702]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        }
[3277]315      }
316    }
317    #endregion
318
319    #region Button Events
[4883]320    private void menuItem_Click(object sender, EventArgs e) {
[3507]321      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
[3796]322      Type viewType = (Type)menuItem.Tag;
323      IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
[3507]324      if (view != null) {
325        view.Locked = Locked;
326        view.ReadOnly = ReadOnly;
327      }
328    }
[4883]329    private void removeButton_Click(object sender, EventArgs e) {
[3277]330      if (itemsListView.SelectedItems.Count > 0) {
331        foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]332          Content.Remove((IRun)item.Tag);
[3277]333        itemsListView.SelectedItems.Clear();
334      }
335    }
[4883]336    private void clearButton_Click(object sender, EventArgs e) {
[3716]337      Content.Clear();
338    }
[3277]339    #endregion
340
[4096]341    #region CheckBox Events
[4883]342    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
[4096]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
[3277]354    #region Content Events
[4883]355    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]356      if (InvokeRequired)
[3280]357        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
[3449]358      else {
[4200]359        foreach (IRun item in e.Items)
[3277]360          AddListViewItem(CreateListViewItem(item));
[4200]361
362        AdjustListViewColumnSizes();
[3507]363        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]364        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]365        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3449]366      }
[3277]367    }
[4883]368    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]369      if (InvokeRequired)
[3280]370        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
[3277]371      else {
[3280]372        foreach (IRun item in e.Items) {
[4203]373          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]374          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
375          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]376        }
[5237]377        RebuildImageList();
[3507]378        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]379        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]380        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]381      }
382    }
[4883]383    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]384      if (InvokeRequired)
[3280]385        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
[3277]386      else {
[3280]387        foreach (IRun item in e.OldItems) {
[5237]388          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]389          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
390          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]391        }
[5237]392        RebuildImageList();
[4200]393        foreach (IRun item in e.Items)
[3277]394          AddListViewItem(CreateListViewItem(item));
[4200]395
396        AdjustListViewColumnSizes();
[3507]397        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]398        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]399        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]400      }
401    }
402    #endregion
403
404    #region Item Events
[4883]405    private void Item_ItemImageChanged(object sender, EventArgs e) {
[3341]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    }
[4883]414    private void Item_ToStringChanged(object sender, EventArgs e) {
[3277]415      if (InvokeRequired)
416        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
417      else {
[3280]418        IRun item = (IRun)sender;
[3277]419        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3341]420          UpdateListViewItemText(listViewItem);
[3829]421        AdjustListViewColumnSizes();
[3277]422      }
423    }
[5237]424    private void Item_Changed(object sender, EventArgs e) {
[3632]425      if (InvokeRequired)
[5237]426        this.Invoke(new EventHandler(Item_Changed), sender, e);
[3632]427      else {
428        IRun run = (IRun)sender;
429        UpdateRun(run);
430      }
[3614]431    }
432
[4883]433    private void UpdateRun(IRun run) {
[3507]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    }
[3277]444    #endregion
[3716]445
[3829]446    #region Helpers
[4883]447    private void AdjustListViewColumnSizes() {
[3829]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    }
[5237]454    private void RebuildImageList() {
455      itemsListView.SmallImageList.Images.Clear();
[5239]456      foreach (ListViewItem listViewItem in itemsListView.Items) {
457        IRun item = listViewItem.Tag as IRun;
[5287]458        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]459        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5237]460      }
461    }
[3829]462    #endregion
[3260]463  }
464}
Note: See TracBrowser for help on using the repository browser.