Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3519 was 3507, checked in by swagner, 15 years ago

Continued work on algorithm batch processing (#947)

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