Free cookie consent management tool by TermsFeed Policy Generator

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

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

Improved performance of all RunCollectionViews (ticket #1284).

File size: 17.3 KB
Line 
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
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>> runListViewItemMapping;
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      runListViewItemMapping = 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      DeregisterRunEvents(Content);
64      base.DeregisterContentEvents();
65    }
66    protected override void RegisterContentEvents() {
67      base.RegisterContentEvents();
68      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
69      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
70      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
71      RegisterRunEvents(Content);
72    }
73    private void RegisterRunEvents(IEnumerable<IRun> runs) {
74      foreach (IRun run in runs)
75        run.Changed += new EventHandler(Run_Changed);
76    }
77    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
78      foreach (IRun run in runs)
79        run.Changed -= new EventHandler(Run_Changed);
80    }
81
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
96    protected override void OnContentChanged() {
97      base.OnContentChanged();
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
103      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
104      viewHost.Content = null;
105
106      if (Content != null) {
107        if (RunCollection != null) {
108          if (!tabControl.TabPages.Contains(constraintPage))
109            tabControl.TabPages.Add(constraintPage);
110          runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
111          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
112        }
113        foreach (IRun item in Content) {
114          ListViewItem listViewItem = CreateListViewItem(item);
115          AddListViewItem(listViewItem);
116          if ((selectedName != null) && item.Name.Equals(selectedName))
117            listViewItem.Selected = true;
118        }
119        AdjustListViewColumnSizes();
120      } else {
121        runCollectionConstraintCollectionView.Content = null;
122        if (tabControl.TabPages.Contains(constraintPage))
123          tabControl.TabPages.Remove(constraintPage);
124      }
125    }
126
127    protected override void SetEnabledStateOfControls() {
128      base.SetEnabledStateOfControls();
129      if (Content == null) {
130        analyzeRunsToolStripDropDownButton.Enabled = false;
131        runCollectionConstraintCollectionView.ReadOnly = true;
132        itemsListView.Enabled = false;
133        detailsGroupBox.Enabled = false;
134        viewHost.Enabled = false;
135        removeButton.Enabled = false;
136        clearButton.Enabled = false;
137      } else {
138        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
139        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
140        itemsListView.Enabled = true;
141        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
142        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
143        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
144        viewHost.Enabled = true;
145      }
146    }
147
148    private ListViewItem CreateListViewItem(IRun item) {
149      ListViewItem listViewItem = new ListViewItem();
150      listViewItem.Text = item.ToString();
151      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
152      itemsListView.SmallImageList.Images.Add(item.ItemImage);
153      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
154      listViewItem.Tag = item;
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      }
163      return listViewItem;
164    }
165    private void AddListViewItem(ListViewItem listViewItem) {
166      itemsListView.Items.Add(listViewItem);
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      }
175    }
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      }
186      listViewItem.Remove();
187      foreach (ListViewItem other in itemsListView.Items)
188        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
189      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
190    }
191    private void UpdateListViewItemImage(ListViewItem listViewItem) {
192      int i = listViewItem.ImageIndex;
193      listViewItem.ImageList.Images[i] = ((IRun)listViewItem.Tag).ItemImage;
194      listViewItem.ImageIndex = -1;
195      listViewItem.ImageIndex = i;
196    }
197    private void UpdateListViewItemText(ListViewItem listViewItem) {
198      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
199        listViewItem.Text = listViewItem.Tag.ToString();
200    }
201    private IEnumerable<ListViewItem> GetListViewItemsForItem(IRun run) {
202      return runListViewItemMapping[run];
203    }
204
205    #region ListView Events
206    private void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
207      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
208      AdjustListViewColumnSizes();
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        }
218      }
219    }
220    private void itemsListView_KeyDown(object sender, KeyEventArgs e) {
221      if (e.KeyCode == Keys.Delete) {
222        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
223          foreach (ListViewItem item in itemsListView.SelectedItems)
224            Content.Remove((IRun)item.Tag);
225        }
226      }
227    }
228    private void itemsListView_DoubleClick(object sender, EventArgs e) {
229      if (itemsListView.SelectedItems.Count == 1) {
230        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
231        IContentView view = MainFormManager.MainForm.ShowContent(item);
232        if (view != null) {
233          view.ReadOnly = ReadOnly;
234          view.Locked = Locked;
235        }
236      }
237    }
238    private void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
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);
245        if (Content.IsReadOnly || ReadOnly) {
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        }
252      }
253    }
254    private void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
255      e.Effect = DragDropEffects.None;
256      Type type = e.Data.GetData("Type") as Type;
257      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
258        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
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;
262        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
263      }
264    }
265    private void itemsListView_DragDrop(object sender, DragEventArgs e) {
266      if (e.Effect != DragDropEffects.None) {
267        IRun item = e.Data.GetData("Value") as IRun;
268        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
269        Content.Add(item);
270      }
271    }
272    #endregion
273
274    #region Button Events
275    private void menuItem_Click(object sender, EventArgs e) {
276      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
277      Type viewType = (Type)menuItem.Tag;
278      IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
279      if (view != null) {
280        view.Locked = Locked;
281        view.ReadOnly = ReadOnly;
282      }
283    }
284    private void removeButton_Click(object sender, EventArgs e) {
285      if (itemsListView.SelectedItems.Count > 0) {
286        foreach (ListViewItem item in itemsListView.SelectedItems)
287          Content.Remove((IRun)item.Tag);
288        itemsListView.SelectedItems.Clear();
289      }
290    }
291    private void clearButton_Click(object sender, EventArgs e) {
292      Content.Clear();
293    }
294    #endregion
295
296    #region CheckBox Events
297    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
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
309    #region Content Events
310    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
311      if (InvokeRequired)
312        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
313      else {
314        RegisterRunEvents(e.Items);
315        foreach (IRun item in e.Items)
316          AddListViewItem(CreateListViewItem(item));
317
318        AdjustListViewColumnSizes();
319        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
320        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
321        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
322      }
323    }
324    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
325      if (InvokeRequired)
326        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
327      else {
328        DeregisterRunEvents(e.Items);
329        foreach (IRun item in e.Items) {
330          //remove only the first matching ListViewItem, because the IRun could be contained multiple times in the ItemCollection
331          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
332          if (listViewItem != null) RemoveListViewItem(listViewItem);
333        }
334        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
335        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
336        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
337      }
338    }
339    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
340      if (InvokeRequired)
341        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
342      else {
343        DeregisterRunEvents(e.OldItems);
344        foreach (IRun item in e.OldItems) {
345          ListViewItem listViewItem = GetListViewItemsForItem(item).FirstOrDefault();
346          if (listViewItem != null) RemoveListViewItem(listViewItem);
347        }
348        RegisterRunEvents(e.Items);
349        foreach (IRun item in e.Items)
350          AddListViewItem(CreateListViewItem(item));
351
352        AdjustListViewColumnSizes();
353        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
354        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
355        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
356      }
357    }
358    #endregion
359
360    #region Item Events
361    private void Item_ItemImageChanged(object sender, EventArgs e) {
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    }
370    private void Item_ToStringChanged(object sender, EventArgs e) {
371      if (InvokeRequired)
372        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
373      else {
374        IRun item = (IRun)sender;
375        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
376          UpdateListViewItemText(listViewItem);
377        AdjustListViewColumnSizes();
378      }
379    }
380    private void Run_Changed(object sender, EventArgs e) {
381      if (InvokeRequired)
382        this.Invoke(new EventHandler(Run_Changed), sender, e);
383      else {
384        IRun run = (IRun)sender;
385        UpdateRun(run);
386      }
387    }
388
389    private void UpdateRun(IRun run) {
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    }
400    #endregion
401
402    #region Helpers
403    private void AdjustListViewColumnSizes() {
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
411  }
412}
Note: See TracBrowser for help on using the repository browser.