Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4893 was 4883, checked in by mkommend, 14 years ago

Improved performance of all RunCollectionViews (ticket #1284).

File size: 17.3 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
[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 {
38    private Dictionary<IRun, List<ListViewItem>> runListViewItemMapping;
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";
[4883]56      runListViewItemMapping = 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);
[3449]63      DeregisterRunEvents(Content);
[3277]64      base.DeregisterContentEvents();
[3260]65    }
[3277]66    protected override void RegisterContentEvents() {
67      base.RegisterContentEvents();
[3280]68      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
69      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
70      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[3449]71      RegisterRunEvents(Content);
[3277]72    }
[4883]73    private void RegisterRunEvents(IEnumerable<IRun> runs) {
[3449]74      foreach (IRun run in runs)
[3507]75        run.Changed += new EventHandler(Run_Changed);
[3449]76    }
[4883]77    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
[3449]78      foreach (IRun run in runs)
[3507]79        run.Changed -= new EventHandler(Run_Changed);
[3449]80    }
[3277]81
[3507]82    protected override void OnInitialized(EventArgs e) {
83      base.OnInitialized(e);
84      var viewTypes = MainFormManager.GetViewTypes(typeof(RunCollection), true);
85      foreach (Type viewType in viewTypes.OrderBy(x => ViewAttribute.GetViewName(x))) {
86        if ((viewType != typeof(ItemCollectionView<IRun>)) && (viewType != typeof(ViewHost))) {
87          ToolStripMenuItem menuItem = new ToolStripMenuItem();
88          menuItem.Text = ViewAttribute.GetViewName(viewType);
89          menuItem.Tag = viewType;
90          menuItem.Click += new EventHandler(menuItem_Click);
91          analyzeRunsToolStripDropDownButton.DropDownItems.Add(menuItem);
92        }
93      }
94    }
95
[3277]96    protected override void OnContentChanged() {
97      base.OnContentChanged();
[3775]98
99      string selectedName = null;
100      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null))
101        selectedName = ((IRun)itemsListView.SelectedItems[0].Tag).Name;
102
[3277]103      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
104      viewHost.Content = null;
105
106      if (Content != null) {
[3614]107        if (RunCollection != null) {
[3709]108          if (!tabControl.TabPages.Contains(constraintPage))
109            tabControl.TabPages.Add(constraintPage);
[3614]110          runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
111          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
112        }
113        foreach (IRun item in Content) {
[3775]114          ListViewItem listViewItem = CreateListViewItem(item);
115          AddListViewItem(listViewItem);
116          if ((selectedName != null) && item.Name.Equals(selectedName))
117            listViewItem.Selected = true;
[3614]118        }
[4200]119        AdjustListViewColumnSizes();
[3709]120      } else {
121        runCollectionConstraintCollectionView.Content = null;
122        if (tabControl.TabPages.Contains(constraintPage))
123          tabControl.TabPages.Remove(constraintPage);
[3277]124      }
125    }
126
[3904]127    protected override void SetEnabledStateOfControls() {
128      base.SetEnabledStateOfControls();
[3350]129      if (Content == null) {
[3507]130        analyzeRunsToolStripDropDownButton.Enabled = false;
[3614]131        runCollectionConstraintCollectionView.ReadOnly = true;
[3350]132        itemsListView.Enabled = false;
133        detailsGroupBox.Enabled = false;
134        viewHost.Enabled = false;
135        removeButton.Enabled = false;
[3716]136        clearButton.Enabled = false;
[3350]137      } else {
[3507]138        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3614]139        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3350]140        itemsListView.Enabled = true;
[4099]141        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[3435]142        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3716]143        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3350]144        viewHost.Enabled = true;
145      }
146    }
147
[4883]148    private ListViewItem CreateListViewItem(IRun item) {
[3277]149      ListViewItem listViewItem = new ListViewItem();
150      listViewItem.Text = item.ToString();
151      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
[3341]152      itemsListView.SmallImageList.Images.Add(item.ItemImage);
153      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[3277]154      listViewItem.Tag = item;
[4200]155
156      if (item.Visible) {
157        listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
158        listViewItem.ForeColor = item.Color;
159      } else {
160        listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
161        listViewItem.ForeColor = Color.LightGray;
162      }
[3277]163      return listViewItem;
164    }
[4883]165    private void AddListViewItem(ListViewItem listViewItem) {
[3277]166      itemsListView.Items.Add(listViewItem);
[4883]167      IRun run = listViewItem.Tag as IRun;
168      if (run != null) {
169        if (!runListViewItemMapping.ContainsKey(run))
170          runListViewItemMapping.Add(run, new List<ListViewItem>());
171        runListViewItemMapping[run].Add(listViewItem);
172        run.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
173        run.ToStringChanged += new EventHandler(Item_ToStringChanged);
174      }
[3277]175    }
[4883]176    private void RemoveListViewItem(ListViewItem listViewItem) {
177      IRun run = listViewItem.Tag as IRun;
178      if (run != null) {
179        runListViewItemMapping[run].Remove(listViewItem);
180        if (runListViewItemMapping[run].Count == 0) {
181          runListViewItemMapping.Remove(run);
182          run.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
183          run.ToStringChanged -= new EventHandler(Item_ToStringChanged);
184        }
185      }
[3277]186      listViewItem.Remove();
[3341]187      foreach (ListViewItem other in itemsListView.Items)
188        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
189      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
[3277]190    }
[4883]191    private void UpdateListViewItemImage(ListViewItem listViewItem) {
[3341]192      int i = listViewItem.ImageIndex;
193      listViewItem.ImageList.Images[i] = ((IRun)listViewItem.Tag).ItemImage;
194      listViewItem.ImageIndex = -1;
195      listViewItem.ImageIndex = i;
196    }
[4883]197    private void UpdateListViewItemText(ListViewItem listViewItem) {
[3341]198      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
[3277]199        listViewItem.Text = listViewItem.Tag.ToString();
200    }
[4883]201    private IEnumerable<ListViewItem> GetListViewItemsForItem(IRun run) {
202      return runListViewItemMapping[run];
[3277]203    }
204
205    #region ListView Events
[4883]206    private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[3456]207      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
[3829]208      AdjustListViewColumnSizes();
[4096]209      if (showDetailsCheckBox.Checked) {
210        if (itemsListView.SelectedItems.Count == 1) {
211          IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
212          detailsGroupBox.Enabled = true;
213          viewHost.Content = item;
214        } else {
215          viewHost.Content = null;
216          detailsGroupBox.Enabled = false;
217        }
[3277]218      }
219    }
[4883]220    private void itemsListView_KeyDown(object sender, KeyEventArgs e) {
[3277]221      if (e.KeyCode == Keys.Delete) {
[3435]222        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[3277]223          foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]224            Content.Remove((IRun)item.Tag);
[3277]225        }
226      }
227    }
[4883]228    private void itemsListView_DoubleClick(object sender, EventArgs e) {
[3277]229      if (itemsListView.SelectedItems.Count == 1) {
[3280]230        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
[3557]231        IContentView view = MainFormManager.MainForm.ShowContent(item);
[3416]232        if (view != null) {
233          view.ReadOnly = ReadOnly;
[3423]234          view.Locked = Locked;
[3416]235        }
[3277]236      }
237    }
[4883]238    private void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]239      if (!Locked) {
240        ListViewItem listViewItem = (ListViewItem)e.Item;
241        IRun item = (IRun)listViewItem.Tag;
242        DataObject data = new DataObject();
243        data.SetData("Type", item.GetType());
244        data.SetData("Value", item);
[3435]245        if (Content.IsReadOnly || ReadOnly) {
[3432]246          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
247        } else {
248          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
249          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
250            Content.Remove(item);
251        }
[3277]252      }
253    }
[4883]254    private void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
[3277]255      e.Effect = DragDropEffects.None;
256      Type type = e.Data.GetData("Type") as Type;
[3435]257      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
[3694]258        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3277]259        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
260        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
261        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[3526]262        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3277]263      }
264    }
[4883]265    private void itemsListView_DragDrop(object sender, DragEventArgs e) {
[3277]266      if (e.Effect != DragDropEffects.None) {
[3280]267        IRun item = e.Data.GetData("Value") as IRun;
268        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
[3277]269        Content.Add(item);
270      }
271    }
272    #endregion
273
274    #region Button Events
[4883]275    private void menuItem_Click(object sender, EventArgs e) {
[3507]276      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
[3796]277      Type viewType = (Type)menuItem.Tag;
278      IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
[3507]279      if (view != null) {
280        view.Locked = Locked;
281        view.ReadOnly = ReadOnly;
282      }
283    }
[4883]284    private void removeButton_Click(object sender, EventArgs e) {
[3277]285      if (itemsListView.SelectedItems.Count > 0) {
286        foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]287          Content.Remove((IRun)item.Tag);
[3277]288        itemsListView.SelectedItems.Clear();
289      }
290    }
[4883]291    private void clearButton_Click(object sender, EventArgs e) {
[3716]292      Content.Clear();
293    }
[3277]294    #endregion
295
[4096]296    #region CheckBox Events
[4883]297    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
[4096]298      if (showDetailsCheckBox.Checked) {
299        splitContainer.Panel2Collapsed = false;
300        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
301        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (IRun)itemsListView.SelectedItems[0].Tag : null;
302      } else {
303        splitContainer.Panel2Collapsed = true;
304        viewHost.Content = null;
305      }
306    }
307    #endregion
308
[3277]309    #region Content Events
[4883]310    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]311      if (InvokeRequired)
[3280]312        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
[3449]313      else {
314        RegisterRunEvents(e.Items);
[4200]315        foreach (IRun item in e.Items)
[3277]316          AddListViewItem(CreateListViewItem(item));
[4200]317
318        AdjustListViewColumnSizes();
[3507]319        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]320        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]321        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3449]322      }
[3277]323    }
[4883]324    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]325      if (InvokeRequired)
[3280]326        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
[3277]327      else {
[3449]328        DeregisterRunEvents(e.Items);
[3280]329        foreach (IRun item in e.Items) {
[4203]330          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
[4883]331          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
332          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]333        }
[3507]334        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]335        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]336        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]337      }
338    }
[4883]339    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]340      if (InvokeRequired)
[3280]341        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
[3277]342      else {
[3449]343        DeregisterRunEvents(e.OldItems);
[3280]344        foreach (IRun item in e.OldItems) {
[4883]345          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
346          if (listViewItem != null) RemoveListViewItem(listViewItem);
[3277]347        }
[3449]348        RegisterRunEvents(e.Items);
[4200]349        foreach (IRun item in e.Items)
[3277]350          AddListViewItem(CreateListViewItem(item));
[4200]351
352        AdjustListViewColumnSizes();
[3507]353        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3716]354        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3614]355        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
[3277]356      }
357    }
358    #endregion
359
360    #region Item Events
[4883]361    private void Item_ItemImageChanged(object sender, EventArgs e) {
[3341]362      if (InvokeRequired)
363        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
364      else {
365        IRun item = (IRun)sender;
366        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
367          UpdateListViewItemImage(listViewItem);
368      }
369    }
[4883]370    private void Item_ToStringChanged(object sender, EventArgs e) {
[3277]371      if (InvokeRequired)
372        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
373      else {
[3280]374        IRun item = (IRun)sender;
[3277]375        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3341]376          UpdateListViewItemText(listViewItem);
[3829]377        AdjustListViewColumnSizes();
[3277]378      }
379    }
[4883]380    private void Run_Changed(object sender, EventArgs e) {
[3632]381      if (InvokeRequired)
382        this.Invoke(new EventHandler(Run_Changed), sender, e);
383      else {
384        IRun run = (IRun)sender;
385        UpdateRun(run);
386      }
[3614]387    }
388
[4883]389    private void UpdateRun(IRun run) {
[3507]390      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
391        if (run.Visible) {
392          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
393          listViewItem.ForeColor = run.Color;
394        } else {
395          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
396          listViewItem.ForeColor = Color.LightGray;
397        }
398      }
399    }
[3277]400    #endregion
[3716]401
[3829]402    #region Helpers
[4883]403    private void AdjustListViewColumnSizes() {
[3829]404      if (itemsListView.Items.Count > 0) {
405        for (int i = 0; i < itemsListView.Columns.Count; i++) {
406          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
407        }
408      }
409    }
410    #endregion
[3260]411  }
412}
Note: See TracBrowser for help on using the repository browser.