Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs @ 5615

Last change on this file since 5615 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
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 partial class RunCollectionView : ItemView {
38    public new IItemCollection<IRun> Content {
39      get { return (IItemCollection<IRun>)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public RunCollection RunCollection {
44      get { return Content as RunCollection; }
45    }
46
47    public ListView ItemsListView {
48      get { return itemsListView; }
49    }
50
51    public RunCollectionView() {
52      InitializeComponent();
53      itemsGroupBox.Text = "Runs";
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
58      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
59      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
60      DeregisterRunEvents(Content);
61      base.DeregisterContentEvents();
62    }
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
66      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
67      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
68      RegisterRunEvents(Content);
69    }
70    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
71      foreach (IRun run in runs)
72        run.Changed += new EventHandler(Run_Changed);
73    }
74    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
75      foreach (IRun run in runs)
76        run.Changed -= new EventHandler(Run_Changed);
77    }
78
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
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
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
100      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
101      viewHost.Content = null;
102
103      if (Content != null) {
104        if (RunCollection != null) {
105          if (!tabControl.TabPages.Contains(constraintPage))
106            tabControl.TabPages.Add(constraintPage);
107          runCollectionConstraintCollectionView.Content = RunCollection.Constraints;
108          runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
109        }
110        foreach (IRun item in Content) {
111          ListViewItem listViewItem = CreateListViewItem(item);
112          AddListViewItem(listViewItem);
113          if ((selectedName != null) && item.Name.Equals(selectedName))
114            listViewItem.Selected = true;
115        }
116        AdjustListViewColumnSizes();
117      } else {
118        runCollectionConstraintCollectionView.Content = null;
119        if (tabControl.TabPages.Contains(constraintPage))
120          tabControl.TabPages.Remove(constraintPage);
121      }
122    }
123
124    protected override void SetEnabledStateOfControls() {
125      base.SetEnabledStateOfControls();
126      if (Content == null) {
127        analyzeRunsToolStripDropDownButton.Enabled = false;
128        runCollectionConstraintCollectionView.ReadOnly = true;
129        itemsListView.Enabled = false;
130        detailsGroupBox.Enabled = false;
131        viewHost.Enabled = false;
132        removeButton.Enabled = false;
133        clearButton.Enabled = false;
134      } else {
135        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
136        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
137        itemsListView.Enabled = true;
138        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
139        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
140        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
141        viewHost.Enabled = true;
142      }
143    }
144
145    protected virtual ListViewItem CreateListViewItem(IRun item) {
146      ListViewItem listViewItem = new ListViewItem();
147      listViewItem.Text = item.ToString();
148      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
149      itemsListView.SmallImageList.Images.Add(item.ItemImage);
150      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
151      listViewItem.Tag = item;
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      }
160      return listViewItem;
161    }
162    protected virtual void AddListViewItem(ListViewItem listViewItem) {
163      itemsListView.Items.Add(listViewItem);
164      ((IRun)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
165      ((IRun)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
166    }
167    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
168      ((IRun)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
169      ((IRun)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
170      listViewItem.Remove();
171      foreach (ListViewItem other in itemsListView.Items)
172        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
173      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
174    }
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()))
183        listViewItem.Text = listViewItem.Tag.ToString();
184    }
185    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
186      foreach (ListViewItem listViewItem in itemsListView.Items) {
187        if (((IRun)listViewItem.Tag) == item)
188          yield return listViewItem;
189      }
190    }
191
192    #region ListView Events
193    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
194      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
195      AdjustListViewColumnSizes();
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        }
205      }
206    }
207    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
208      if (e.KeyCode == Keys.Delete) {
209        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
210          foreach (ListViewItem item in itemsListView.SelectedItems)
211            Content.Remove((IRun)item.Tag);
212        }
213      }
214    }
215    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
216      if (itemsListView.SelectedItems.Count == 1) {
217        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
218        IContentView view = MainFormManager.MainForm.ShowContent(item);
219        if (view != null) {
220          view.ReadOnly = ReadOnly;
221          view.Locked = Locked;
222        }
223      }
224    }
225    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
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);
232        if (Content.IsReadOnly || ReadOnly) {
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        }
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;
244      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
245        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
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;
249        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
250      }
251    }
252    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
253      if (e.Effect != DragDropEffects.None) {
254        IRun item = e.Data.GetData("Value") as IRun;
255        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
256        Content.Add(item);
257      }
258    }
259    #endregion
260
261    #region Button Events
262    protected virtual void menuItem_Click(object sender, EventArgs e) {
263      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
264      Type viewType = (Type)menuItem.Tag;
265      IContentView view = MainFormManager.MainForm.ShowContent(Content, viewType);
266      if (view != null) {
267        view.Locked = Locked;
268        view.ReadOnly = ReadOnly;
269      }
270    }
271    protected virtual void removeButton_Click(object sender, EventArgs e) {
272      if (itemsListView.SelectedItems.Count > 0) {
273        foreach (ListViewItem item in itemsListView.SelectedItems)
274          Content.Remove((IRun)item.Tag);
275        itemsListView.SelectedItems.Clear();
276      }
277    }
278    protected virtual void clearButton_Click(object sender, EventArgs e) {
279      Content.Clear();
280    }
281    #endregion
282
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
296    #region Content Events
297    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
298      if (InvokeRequired)
299        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
300      else {
301        RegisterRunEvents(e.Items);
302        foreach (IRun item in e.Items)
303          AddListViewItem(CreateListViewItem(item));
304
305        AdjustListViewColumnSizes();
306        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
307        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
308        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
309      }
310    }
311    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
312      if (InvokeRequired)
313        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
314      else {
315        DeregisterRunEvents(e.Items);
316        foreach (IRun item in e.Items) {
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);
321        }
322        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
323        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
324        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
325      }
326    }
327    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
328      if (InvokeRequired)
329        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
330      else {
331        DeregisterRunEvents(e.OldItems);
332        foreach (IRun item in e.OldItems) {
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);
337        }
338        RegisterRunEvents(e.Items);
339        foreach (IRun item in e.Items)
340          AddListViewItem(CreateListViewItem(item));
341
342        AdjustListViewColumnSizes();
343        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
344        clearButton.Enabled = itemsListView.Items.Count > 0 && !Content.IsReadOnly && !ReadOnly;
345        runCollectionConstraintCollectionView.ReadOnly = itemsListView.Items.Count == 0;
346      }
347    }
348    #endregion
349
350    #region Item Events
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    }
360    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
361      if (InvokeRequired)
362        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
363      else {
364        IRun item = (IRun)sender;
365        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
366          UpdateListViewItemText(listViewItem);
367        AdjustListViewColumnSizes();
368      }
369    }
370    protected virtual void Run_Changed(object sender, EventArgs e) {
371      if (InvokeRequired)
372        this.Invoke(new EventHandler(Run_Changed), sender, e);
373      else {
374        IRun run = (IRun)sender;
375        UpdateRun(run);
376      }
377    }
378
379    protected virtual void UpdateRun(IRun run) {
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    }
390    #endregion
391
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
401  }
402}
Note: See TracBrowser for help on using the repository browser.