Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on the refactoring of saving and loading items (#990)

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