Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3332 was 3280, checked in by swagner, 14 years ago

Continued work on algorithm batch processing (#947).

File size: 9.9 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 HeuristicLab.Collections;
23using HeuristicLab.Core.Views;
24using HeuristicLab.MainForm;
25using HeuristicLab.MainForm.WindowsForms;
26using System.Windows.Forms;
27using System;
28using System.Collections.Generic;
29
30namespace HeuristicLab.Optimization.Views {
31  [View("RunCollection View")]
32  [Content(typeof(RunCollection), true)]
33  [Content(typeof(IObservableCollection<IRun>), false)]
34  public partial class RunCollectionView : AsynchronousContentView {
35    public new IObservableCollection<IRun> Content {
36      get { return (IObservableCollection<IRun>)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public ListView ItemsListView {
41      get { return itemsListView; }
42    }
43
44    public RunCollectionView() {
45      InitializeComponent();
46      Caption = "Run Collection";
47    }
48
49    public RunCollectionView(IObservableCollection<IRun> content)
50      : this() {
51      Content = content;
52    }
53
54    protected override void DeregisterContentEvents() {
55      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
56      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
57      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
58      base.DeregisterContentEvents();
59    }
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
63      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
64      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
65    }
66
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      Caption = "Run Collection";
70      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
71      itemsListView.Enabled = false;
72      detailsGroupBox.Enabled = false;
73      viewHost.Content = null;
74      removeButton.Enabled = false;
75
76      if (Content != null) {
77        Caption += " (" + Content.GetType().Name + ")";
78        itemsListView.Enabled = true;
79        foreach (IRun item in Content)
80          AddListViewItem(CreateListViewItem(item));
81      }
82    }
83
84    protected virtual ListViewItem CreateListViewItem(IRun item) {
85      if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
86        itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
87
88      ListViewItem listViewItem = new ListViewItem();
89      listViewItem.Text = item.ToString();
90      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
91      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
92      listViewItem.Tag = item;
93      return listViewItem;
94    }
95    protected virtual void AddListViewItem(ListViewItem listViewItem) {
96      itemsListView.Items.Add(listViewItem);
97      ((IRun)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
98    }
99    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
100      ((IRun)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
101      listViewItem.Remove();
102    }
103    protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
104      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString())) {
105        listViewItem.Text = listViewItem.Tag.ToString();
106      }
107    }
108    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
109      foreach (ListViewItem listViewItem in itemsListView.Items) {
110        if (((IRun)listViewItem.Tag) == item)
111          yield return listViewItem;
112      }
113    }
114
115    #region ListView Events
116    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
117      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly;
118      if (itemsListView.SelectedItems.Count == 1) {
119        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
120        detailsGroupBox.Enabled = true;
121        viewHost.ViewType = null;
122        viewHost.Content = item;
123      } else {
124        viewHost.Content = null;
125        detailsGroupBox.Enabled = false;
126      }
127    }
128    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
129      if (itemsListView.Columns.Count > 0)
130        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
131    }
132    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
133      if (e.KeyCode == Keys.Delete) {
134        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly) {
135          foreach (ListViewItem item in itemsListView.SelectedItems)
136            Content.Remove((IRun)item.Tag);
137        }
138      }
139    }
140    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
141      if (itemsListView.SelectedItems.Count == 1) {
142        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
143        IView view = MainFormManager.CreateDefaultView(item);
144        if (view != null) view.Show();
145      }
146    }
147    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
148      ListViewItem listViewItem = (ListViewItem)e.Item;
149      IRun item = (IRun)listViewItem.Tag;
150      DataObject data = new DataObject();
151      data.SetData("Type", item.GetType());
152      data.SetData("Value", item);
153      if (Content.IsReadOnly) {
154        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
155      } else {
156        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
157        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
158          Content.Remove(item);
159      }
160    }
161    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
162      e.Effect = DragDropEffects.None;
163      Type type = e.Data.GetData("Type") as Type;
164      if ((!Content.IsReadOnly) && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
165        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
166        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
167        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
168        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
169        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
170      }
171    }
172    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
173      if (e.Effect != DragDropEffects.None) {
174        IRun item = e.Data.GetData("Value") as IRun;
175        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
176        Content.Add(item);
177      }
178    }
179    #endregion
180
181    #region Button Events
182    protected virtual void removeButton_Click(object sender, EventArgs e) {
183      if (itemsListView.SelectedItems.Count > 0) {
184        foreach (ListViewItem item in itemsListView.SelectedItems)
185          Content.Remove((IRun)item.Tag);
186        itemsListView.SelectedItems.Clear();
187      }
188    }
189    #endregion
190
191    #region Content Events
192    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
193      if (InvokeRequired)
194        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
195      else
196        foreach (IRun item in e.Items)
197          AddListViewItem(CreateListViewItem(item));
198    }
199    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
200      if (InvokeRequired)
201        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
202      else {
203        foreach (IRun item in e.Items) {
204          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
205            RemoveListViewItem(listViewItem);
206            break;
207          }
208        }
209      }
210    }
211    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
212      if (InvokeRequired)
213        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
214      else {
215        foreach (IRun item in e.OldItems) {
216          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
217            RemoveListViewItem(listViewItem);
218            break;
219          }
220        }
221        foreach (IRun item in e.Items)
222          AddListViewItem(CreateListViewItem(item));
223      }
224    }
225    #endregion
226
227    #region Item Events
228    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
229      if (InvokeRequired)
230        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
231      else {
232        IRun item = (IRun)sender;
233        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
234          UpdateListViewItem(listViewItem);
235      }
236    }
237    #endregion
238  }
239}
240
Note: See TracBrowser for help on using the repository browser.