Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5533 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 19.0 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) {
265        ListViewItem listViewItem = (ListViewItem)e.Item;
[5237]266        IRun item = listViewItem.Tag as IRun;
267        if (item != null) {
268          DataObject data = new DataObject();
269          data.SetData("Type", item.GetType());
270          data.SetData("Value", item);
271          if (Content.IsReadOnly || ReadOnly) {
272            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
273          } else {
274            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
275            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
276              Content.Remove(item);
277          }
[3432]278        }
[3277]279      }
280    }
[4883]281    private void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
[3277]282      e.Effect = DragDropEffects.None;
283      Type type = e.Data.GetData("Type") as Type;
[3435]284      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
[3694]285        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3277]286        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
287        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
288        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[3526]289        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3277]290      }
291    }
[4883]292    private void itemsListView_DragDrop(object sender, DragEventArgs e) {
[3277]293      if (e.Effect != DragDropEffects.None) {
[3280]294        IRun item = e.Data.GetData("Value") as IRun;
295        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
[3277]296        Content.Add(item);
297      }
298    }
299    #endregion
300
301    #region Button Events
[4883]302    private void menuItem_Click(object sender, EventArgs e) {
[3507]303      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
[3796]304      Type viewType = (Type)menuItem.Tag;
305      IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
[3507]306      if (view != null) {
307        view.Locked = Locked;
308        view.ReadOnly = ReadOnly;
309      }
310    }
[4883]311    private void removeButton_Click(object sender, EventArgs e) {
[3277]312      if (itemsListView.SelectedItems.Count > 0) {
313        foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]314          Content.Remove((IRun)item.Tag);
[3277]315        itemsListView.SelectedItems.Clear();
316      }
317    }
[4883]318    private void clearButton_Click(object sender, EventArgs e) {
[3716]319      Content.Clear();
320    }
[3277]321    #endregion
322
[4096]323    #region CheckBox Events
[4883]324    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
[4096]325      if (showDetailsCheckBox.Checked) {
326        splitContainer.Panel2Collapsed = false;
327        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
328        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (IRun)itemsListView.SelectedItems[0].Tag : null;
329      } else {
330        splitContainer.Panel2Collapsed = true;
331        viewHost.Content = null;
332      }
333    }
334    #endregion
335
[3277]336    #region Content Events
[4883]337    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]338      if (InvokeRequired)
[3280]339        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
[3449]340      else {
[4200]341        foreach (IRun item in e.Items)
[3277]342          AddListViewItem(CreateListViewItem(item));
[4200]343
344        AdjustListViewColumnSizes();
[3507]345        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]346        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]347        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3449]348      }
[3277]349    }
[4883]350    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]351      if (InvokeRequired)
[3280]352        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
[3277]353      else {
[3280]354        foreach (IRun item in e.Items) {
[4203]355          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]356          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
357          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]358        }
[5237]359        RebuildImageList();
[3507]360        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]361        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]362        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]363      }
364    }
[4883]365    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]366      if (InvokeRequired)
[3280]367        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
[3277]368      else {
[3280]369        foreach (IRun item in e.OldItems) {
[5237]370          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]371          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
372          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]373        }
[5237]374        RebuildImageList();
[4200]375        foreach (IRun item in e.Items)
[3277]376          AddListViewItem(CreateListViewItem(item));
[4200]377
378        AdjustListViewColumnSizes();
[3507]379        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]380        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]381        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]382      }
383    }
384    #endregion
385
386    #region Item Events
[4883]387    private void Item_ItemImageChanged(object sender, EventArgs e) {
[3341]388      if (InvokeRequired)
389        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
390      else {
391        IRun item = (IRun)sender;
392        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
393          UpdateListViewItemImage(listViewItem);
394      }
395    }
[4883]396    private void Item_ToStringChanged(object sender, EventArgs e) {
[3277]397      if (InvokeRequired)
398        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
399      else {
[3280]400        IRun item = (IRun)sender;
[3277]401        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3341]402          UpdateListViewItemText(listViewItem);
[3829]403        AdjustListViewColumnSizes();
[3277]404      }
405    }
[5237]406    private void Item_Changed(object sender, EventArgs e) {
[3632]407      if (InvokeRequired)
[5237]408        this.Invoke(new EventHandler(Item_Changed), sender, e);
[3632]409      else {
410        IRun run = (IRun)sender;
411        UpdateRun(run);
412      }
[3614]413    }
414
[4883]415    private void UpdateRun(IRun run) {
[3507]416      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
417        if (run.Visible) {
418          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
419          listViewItem.ForeColor = run.Color;
420        } else {
421          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
422          listViewItem.ForeColor = Color.LightGray;
423        }
424      }
425    }
[3277]426    #endregion
[3716]427
[3829]428    #region Helpers
[4883]429    private void AdjustListViewColumnSizes() {
[3829]430      if (itemsListView.Items.Count > 0) {
431        for (int i = 0; i < itemsListView.Columns.Count; i++) {
432          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
433        }
434      }
435    }
[5237]436    private void RebuildImageList() {
437      itemsListView.SmallImageList.Images.Clear();
[5239]438      foreach (ListViewItem listViewItem in itemsListView.Items) {
439        IRun item = listViewItem.Tag as IRun;
[5287]440        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]441        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5237]442      }
443    }
[3829]444    #endregion
[3260]445  }
446}
Note: See TracBrowser for help on using the repository browser.