Free cookie consent management tool by TermsFeed Policy Generator

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

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

Adapted views of HeuristicLab.Core.Views according the new read-only property and renamed method SetEnableStateOfControls into SetEnabledStateOfControls (#973).

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