Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented reviewers' comments (#893)

File size: 12.6 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;
24using HeuristicLab.Core.Views;
25using HeuristicLab.MainForm;
26using HeuristicLab.MainForm.WindowsForms;
27using System.Windows.Forms;
28using System;
29using System.Drawing;
30using System.Collections.Generic;
31
32namespace HeuristicLab.Optimization.Views {
33  [View("RunCollection View")]
34  [Content(typeof(RunCollection), true)]
35  [Content(typeof(IItemCollection<IRun>), false)]
36  public partial class RunCollectionView : ItemView {
37    public new IItemCollection<IRun> Content {
38      get { return (IItemCollection<IRun>)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public ListView ItemsListView {
43      get { return itemsListView; }
44    }
45
46    public RunCollectionView() {
47      InitializeComponent();
48      Caption = "Run Collection";
49      itemsGroupBox.Text = "Runs";
50    }
51
52    public RunCollectionView(IItemCollection<IRun> content)
53      : this() {
54      Content = content;
55    }
56
57    protected override void DeregisterContentEvents() {
58      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
59      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
60      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
61      DeregisterRunEvents(Content);
62      base.DeregisterContentEvents();
63    }
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
67      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
68      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
69      RegisterRunEvents(Content);
70    }
71    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
72      foreach (IRun run in runs)
73        run.Changed += new EventHandler(run_Changed);
74    }
75    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
76      foreach (IRun run in runs)
77        run.Changed -= new EventHandler(run_Changed);
78    }
79
80    protected override void OnContentChanged() {
81      base.OnContentChanged();
82      Caption = "Run Collection";
83      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
84      viewHost.Content = null;
85
86      if (Content != null) {
87        Caption += " (" + Content.GetType().Name + ")";
88        foreach (IRun item in Content)
89          AddListViewItem(CreateListViewItem(item));
90      }
91      SetEnabledStateOfControls();
92    }
93
94    private void run_Changed(object sender, EventArgs e) {
95      IRun run = (IRun)sender;
96      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
97        if (run.Visible) {
98          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
99          listViewItem.ForeColor = run.Color;
100        } else {
101          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
102          listViewItem.ForeColor = Color.LightGray;
103        }
104      }
105    }
106
107    protected override void OnReadOnlyChanged() {
108      base.OnReadOnlyChanged();
109      SetEnabledStateOfControls();
110    }
111    private void SetEnabledStateOfControls() {
112      if (Content == null) {
113        itemsListView.Enabled = false;
114        detailsGroupBox.Enabled = false;
115        viewHost.Enabled = false;
116        removeButton.Enabled = false;
117      } else {
118        itemsListView.Enabled = true;
119        detailsGroupBox.Enabled = true;
120        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
121        viewHost.Enabled = true;
122      }
123    }
124
125    protected virtual ListViewItem CreateListViewItem(IRun item) {
126      ListViewItem listViewItem = new ListViewItem();
127      listViewItem.Text = item.ToString();
128      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
129      itemsListView.SmallImageList.Images.Add(item.ItemImage);
130      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
131      listViewItem.Tag = item;
132      return listViewItem;
133    }
134    protected virtual void AddListViewItem(ListViewItem listViewItem) {
135      itemsListView.Items.Add(listViewItem);
136      ((IRun)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
137      ((IRun)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
138    }
139    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
140      ((IRun)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
141      ((IRun)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
142      listViewItem.Remove();
143      foreach (ListViewItem other in itemsListView.Items)
144        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
145      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
146    }
147    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
148      int i = listViewItem.ImageIndex;
149      listViewItem.ImageList.Images[i] = ((IRun)listViewItem.Tag).ItemImage;
150      listViewItem.ImageIndex = -1;
151      listViewItem.ImageIndex = i;
152    }
153    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
154      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
155        listViewItem.Text = listViewItem.Tag.ToString();
156    }
157    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
158      foreach (ListViewItem listViewItem in itemsListView.Items) {
159        if (((IRun)listViewItem.Tag) == item)
160          yield return listViewItem;
161      }
162    }
163
164    #region ListView Events
165    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
166      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
167      if (itemsListView.SelectedItems.Count == 1) {
168        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
169        detailsGroupBox.Enabled = true;
170        viewHost.ViewType = null;
171        viewHost.Content = item;
172      } else {
173        viewHost.Content = null;
174        detailsGroupBox.Enabled = false;
175      }
176    }
177    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
178      if (itemsListView.Columns.Count > 0)
179        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
180    }
181    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
182      if (e.KeyCode == Keys.Delete) {
183        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
184          foreach (ListViewItem item in itemsListView.SelectedItems)
185            Content.Remove((IRun)item.Tag);
186        }
187      }
188    }
189    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
190      if (itemsListView.SelectedItems.Count == 1) {
191        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
192        IContentView view = MainFormManager.CreateDefaultView(item);
193        if (view != null) {
194          view.ReadOnly = ReadOnly;
195          view.Locked = Locked;
196          view.Show();
197        }
198      }
199    }
200    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
201      if (!Locked) {
202        ListViewItem listViewItem = (ListViewItem)e.Item;
203        IRun item = (IRun)listViewItem.Tag;
204        DataObject data = new DataObject();
205        data.SetData("Type", item.GetType());
206        data.SetData("Value", item);
207        if (Content.IsReadOnly || ReadOnly) {
208          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
209        } else {
210          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
211          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
212            Content.Remove(item);
213        }
214      }
215    }
216    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
217      e.Effect = DragDropEffects.None;
218      Type type = e.Data.GetData("Type") as Type;
219      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
220        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
221        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
222        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
223        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
224        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
225      }
226    }
227    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
228      if (e.Effect != DragDropEffects.None) {
229        IRun item = e.Data.GetData("Value") as IRun;
230        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
231        Content.Add(item);
232      }
233    }
234    #endregion
235
236    #region Button Events
237    protected virtual void removeButton_Click(object sender, EventArgs e) {
238      if (itemsListView.SelectedItems.Count > 0) {
239        foreach (ListViewItem item in itemsListView.SelectedItems)
240          Content.Remove((IRun)item.Tag);
241        itemsListView.SelectedItems.Clear();
242      }
243    }
244    #endregion
245
246    #region Content Events
247    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
248      if (InvokeRequired)
249        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
250      else {
251        RegisterRunEvents(e.Items);
252        foreach (IRun item in e.Items)
253          AddListViewItem(CreateListViewItem(item));
254      }
255    }
256    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
257      if (InvokeRequired)
258        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
259      else {
260        DeregisterRunEvents(e.Items);
261        foreach (IRun item in e.Items) {
262          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
263            RemoveListViewItem(listViewItem);
264            break;
265          }
266        }
267      }
268    }
269    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
270      if (InvokeRequired)
271        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
272      else {
273        DeregisterRunEvents(e.OldItems);
274        foreach (IRun item in e.OldItems) {
275          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
276            RemoveListViewItem(listViewItem);
277            break;
278          }
279        }
280        RegisterRunEvents(e.Items);
281        foreach (IRun item in e.Items)
282          AddListViewItem(CreateListViewItem(item));
283      }
284    }
285    #endregion
286
287    #region Item Events
288    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
289      if (InvokeRequired)
290        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
291      else {
292        IRun item = (IRun)sender;
293        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
294          UpdateListViewItemImage(listViewItem);
295      }
296    }
297    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
298      if (InvokeRequired)
299        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
300      else {
301        IRun item = (IRun)sender;
302        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
303          UpdateListViewItemText(listViewItem);
304      }
305    }
306    #endregion
307  }
308}
309
Note: See TracBrowser for help on using the repository browser.