Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4434 was 4300, checked in by mkommend, 14 years ago

Removed calls to the ClearCache method of the ViewHost (ticket #1133).

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