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, 13 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
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      base.ReadOnly = true;
48    }
49
50    public RunCollectionView(IObservableCollection<IRun> content)
51      : this() {
52      Content = content;
53    }
54    public override bool ReadOnly {
55      get { return base.ReadOnly; }
56      set { /*not needed because results are always readonly */}
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      base.DeregisterContentEvents();
64    }
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
67      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
68      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
69      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
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 + ")";
80        foreach (IRun item in Content)
81          AddListViewItem(CreateListViewItem(item));
82      }
83      SetEnabledStateOfControls();
84    }
85
86    protected override void OnReadOnlyChanged() {
87      base.OnReadOnlyChanged();
88      SetEnabledStateOfControls();
89    }
90    private void SetEnabledStateOfControls() {
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
105    protected virtual ListViewItem CreateListViewItem(IRun item) {
106      ListViewItem listViewItem = new ListViewItem();
107      listViewItem.Text = item.ToString();
108      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
109      itemsListView.SmallImageList.Images.Add(item.ItemImage);
110      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
111      listViewItem.Tag = item;
112      return listViewItem;
113    }
114    protected virtual void AddListViewItem(ListViewItem listViewItem) {
115      itemsListView.Items.Add(listViewItem);
116      ((IRun)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
117      ((IRun)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
118    }
119    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
120      ((IRun)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
121      ((IRun)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
122      listViewItem.Remove();
123      foreach (ListViewItem other in itemsListView.Items)
124        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
125      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
126    }
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()))
135        listViewItem.Text = listViewItem.Tag.ToString();
136    }
137    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
138      foreach (ListViewItem listViewItem in itemsListView.Items) {
139        if (((IRun)listViewItem.Tag) == item)
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) {
148        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
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)
165            Content.Remove((IRun)item.Tag);
166        }
167      }
168    }
169    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
170      if (itemsListView.SelectedItems.Count == 1) {
171        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
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;
178      IRun item = (IRun)listViewItem.Tag;
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;
193      if ((!Content.IsReadOnly) && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
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) {
203        IRun item = e.Data.GetData("Value") as IRun;
204        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
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)
214          Content.Remove((IRun)item.Tag);
215        itemsListView.SelectedItems.Clear();
216      }
217    }
218    #endregion
219
220    #region Content Events
221    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
222      if (InvokeRequired)
223        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
224      else
225        foreach (IRun item in e.Items)
226          AddListViewItem(CreateListViewItem(item));
227    }
228    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
229      if (InvokeRequired)
230        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
231      else {
232        foreach (IRun item in e.Items) {
233          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
234            RemoveListViewItem(listViewItem);
235            break;
236          }
237        }
238      }
239    }
240    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
241      if (InvokeRequired)
242        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
243      else {
244        foreach (IRun item in e.OldItems) {
245          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
246            RemoveListViewItem(listViewItem);
247            break;
248          }
249        }
250        foreach (IRun item in e.Items)
251          AddListViewItem(CreateListViewItem(item));
252      }
253    }
254    #endregion
255
256    #region Item Events
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    }
266    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
267      if (InvokeRequired)
268        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
269      else {
270        IRun item = (IRun)sender;
271        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
272          UpdateListViewItemText(listViewItem);
273      }
274    }
275    #endregion
276  }
277}
278
Note: See TracBrowser for help on using the repository browser.